Error if filter doesn't match

emad981

Registered User.
Local time
Today, 12:13
Joined
Aug 20, 2013
Messages
17
I have a login form that has :
"txtUsername"
"txtPassword"
It linked to table:"tblUsers" that contain a filed "UserName"

I set a form filter to filter the "UserName" like "txtUsername"

Everything works fine if the typed username already exists in "tblUsers" but if the typed username doesn't match "UserName" filed after filed, I get and error and even the form goes blank;
I want the form to set error Msg that username is not correct.


Here the code I use

Code:
Private Sub txtUsername_AfterUpdate()

Me.Form.Filter = "UserName like '*" & Me.txtUsername & "*'"
Me.Form.FilterOn = True
Me.txtPassword.SetFocus

End Sub
 
I want the form to set error Msg that username is not correct.
try using a domain function - something like

Code:
 Private Sub txtUsername_AfterUpdate()

if dcount("*","tblUsers","UserName = '" & Me.txtUsername & "'")>0 then
    Me.txtPassword.SetFocus
 else
     msgbox "User name does not exist"
 end if

End Sub

Also not much security in finding a user name 'like', you want an exact match, so use =

And your login form should not have a record source - very easily hacked
 
Last edited:
Thanks London , your code makes the trick, but i modified it to the following because I will make the error message as a "label" under Textboxes "username and password"
Here is the code I use now

Code:
Private Sub txtUsername_AfterUpdate()

Me.Form.Filter = "UserName = '*" & Me.txtUsername & "*'"
Me.Form.FilterOn = True
    If DCount("*", "tblUsers", "UserName = '" & Me.txtUsername & "'") > 0 Then
        Me.Form.Filter = "UserName like '*" & Me.txtUsername & "*'"
        Me.Form.FilterOn = True
        Me.txtPassword.SetFocus
     Else
        Me.Form.FilterOn = False
        Me.txtPassword.SetFocus
        
 End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom