Login passwordin access database

I created the following code for a login form in access database

The part regarding the username is working fine BUT the control for the password is not asking for the password , user can just login if he put correct user name with blank password.

Private Sub btnLogin_Click()
Dim rs As Recordset

Set rs = CurrentDb.OpenRecordset("tbl1Employees", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserName & "'"
If rs.NoMatch = True Then
Me.lblWrongUser.Visible = True
Me.txtUserName.SetFocus
Exit Sub
End If
Me.lblWrongUser.Visible = False

If rs!Password <> Me.txtPassword Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub

End If
Me.lblWrongPass.Visible = False
DoCmd.OpenForm "ãÕÑæÝÇÊ"
DoCmd.Close acForm, Me.Name


End Sub
Dr.Abdulwahab Al-anesiProjects Information Technology ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PatHartmanCommented:
Telling the user that the password is invalid rather than the userID simply gives a hacker valuable information.  It let's him know that he guessed part of the credentials and now has Do concentrate on the other part.  A better solution is to use a DLookup() or a query that includes both the UserID and the password.
If DLookup("UserName", "tblEmployee", "UserID = '" & Me.txtUserID & "' AND Password = '" & Me.txtPassword & "'") Then
Else
    Msgbox "Invalid userID or Password.",vbokOnly
    Exit Sub
End If
  If the record is found, both are valid.  If the record is not found, you don't care which part is invalid and you don't want to give the user any help in that regard.

As to your code, they problem could be that Password is null in one or both fields.  When you compare a null value to any other value, the answer is ALWAYS null which translates in this case to false which you are thinking of as "true".  The point is that the true path of the expression will NEVER be taken when either or both fields are null.
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
I don't see anything wrong with your original code, but it could be structured better:


Private Sub btnLogin_Click()
Dim rs As Recordset

Set rs = CurrentDb.OpenRecordset("tbl1Employees", , dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserName & "'"
If rs.NoMatch = True Then
   Me.lblWrongUser.Visible = True
   Me.txtUserName.SetFocus
Else
  Me.lblWrongUser.Visible = False

  If rs!Password <> Me.txtPassword Then
    Me.lblWrongPass.Visible = True
    Me.txtPassword.SetFocus
  Else
     Me.lblWrongPass.Visible = False
     DoCmd.OpenForm "ãÕÑæÝÇÊ"
     DoCmd.Close acForm, Me.Name
  End If
End If

 If your still having problems, put a STOP at the start of the procedure, then try your form.  You'll hit the STOP at which point you can step through the code with F8 line by line.

  Hit F5 to continue.

 Also note that you typically don't want to use snapshot when opening a recordset.  While it sounds fast, your asking for a complete copy of every record to be made.

Jim.
Dr.Abdulwahab Al-anesiProjects Information Technology ManagerAuthor Commented:
Thanks

I tried the code provided using copy past replacing my code, but did not work , it is returning message (Invalid outside procedure)
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Dr.Abdulwahab Al-anesiProjects Information Technology ManagerAuthor Commented:
Dear Jim

I also tried the code provided using copy past replacing my codebut did not wok also , I need further help
Dr.Abdulwahab Al-anesiProjects Information Technology ManagerAuthor Commented:
Jim I like your videos do you have video on designing login Access 2013 forms , if you don't have do you recommend me for others vediod
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
As Pat said, it's probably the data.   So do what I suggested, put a STOP in the code at the top of the procedure, execute the form, and the code window will come up when you hit the stop.

Use F8 to step through the code line by line.  As you do, you can hover over things with the mouse to see their values, or in the immediate/debug window (Ctrl/G)  type things like:

Debug.Print rs!Password

with a return and you will see the value displayed.  Your can also use the ? as a shorthand for Debug.Print, so:

? rs!Password

If you do this, you'll quickly be able to see where the problem is  Sorry, no videos on form creation.
Dr.Abdulwahab Al-anesiProjects Information Technology ManagerAuthor Commented:
Dears Finally

I succeeded with this code
Private Sub Command1_Click()

If IsNull(Me.txtLoginID) Then
MsgBox "Please enter loginID", vbInformation, "LoginID Required"
Me.txtLoginID.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
'Process the job
If (IsNull(DLookup("[UserLogin]", "tblUser", "[UserLogin]='" & Me.txtLoginID.Value & "'AND Password='" & Me.txtPassword.Value & "'"))) Then

MsgBox "Incorrect LoginID or Password"
Else
MsgBox "LoginID and Password Correct"
DoCmd.OpenForm "ãÕÑæÝÇÊ"

End If
End If

End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dr.Abdulwahab Al-anesiProjects Information Technology ManagerAuthor Commented:
I worked hard by searching online
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.