Login Form problem

Local time
Today, 21:59
Joined
Dec 18, 2021
Messages
46
Hi,

When opening "frm_Login" and entering Username and Password, the code is working perfectly fine.

The problem is when entering "Username" without typing the "Password". It's also opening "My_DB" form.

I need to check it as in Username. How?

The DB file is attached.

Thanks for any help.
 

Attachments

I am not even going to download your db, just post the code within code tags. :(
 
I am not even going to download your db, just post the code within code tags. :(
Hi,
Merry Christmas!
Here is the code:

Code:
Option Compare Database
Option Explicit

Private Sub btnLogin_Click()

Dim rs As Recordset

Set rs = CurrentDb.OpenRecordset("tbl_Login", 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 "frm_HomeScreen"
DoCmd.Close acForm, Me.Name

   
End Sub

Code Tags Added by UG
Please use Code Tags when posting VBA Code
Please read this for further information:-
https://www.access-programmers.co.u...e-use-code-tags-when-posting-vba-code.240420/
Please feel free to Remove this Comment

Thanks for any help.
 
Last edited by a moderator:
That is not within code tags. :(
Regardless, I do not see how that code can do what you say it does? :(
 
hi,
its advisable you place the conditions that checks for user name and password inside the same if statement.

why is this area outside of an if block?
Because they exit the sub if any value is not valid?
 
Because they exit the sub if any value is not valid?
it is a cleaner approach to place it inside the if block statement, preferably under an else statement.

if you are comfortable with case statements, you can use it instead and place it under a case.
 
I would suggest it's the <> me.password not handling the null very well.

Much better to use = and switch the logic in the statement to process it as accepted.
 
Hi,

When opening "frm_Login" and entering Username and Password, the code is working perfectly fine.

The problem is when entering "Username" without typing the "Password". It's also opening "My_DB" form.

I need to check it as in Username. How?

The DB file is attached.

Thanks for any help.
Hello,

Thank you all for your suggestions and help.

At last I succeeded to solve this problem by repeating code of UserName but in Password and let the rest as it is

rs.FindFirst "Password='" & Me.txtPassword & "'"
If rs.NoMatch = True Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub

End If
Me.lblWrongPass.Visible = False
DoCmd.OpenForm "frm_HomeScreen"
DoCmd.Close acForm, Me.Name
 
There is no condition to check if isnull password
Yes. You are right. I changed the Password code to be the same as the UserName and it worked.

Thank you so much for your time and Suggestions.

Have a nice weekend and Merry Christmas.

rs.FindFirst "Password='" & Me.txtPassword & "'"
If rs.NoMatch = True Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub

End If
Me.lblWrongPass.Visible = False
DoCmd.OpenForm "frm_HomeScreen"
DoCmd.Close acForm, Me.Name
 
Check both values for the same record.
If the name matches then check the Password matches FOR THAT NAME.

Or lookup both at the same time
Name = Entry Name AND Password = Entered Password
 
Check both values for the same record.
If the name matches then check the Password matches FOR THAT NAME.

Or lookup both at the same time
Name = Entry Name AND Password = Entered Password
@Minty I thought the OP was doing that in their initial code?, same record that is?
 
Yes - you are correct - it was the use of <> that was the problem.
If they simply change the logic to = and exit if not then problem solved.
 
Do not open a recordset. Do not use FindFirst. Use a domain function for this simple query. All you need to do is use BOTH userID and password as the criteria. Doing it as you are with two separate "FindFirst" operations, you will allow a person to log in as someone else by using the other person's UserID and their own password.

Code:
If dCount("*"), "tblUsers", "UserID = '" & Me.UserID & "' AND Password = '" & Me.Password & "'") = 0 Then
    Msgbox "UserID or Password is invalid, please try again."vbONOnly
    Exit Sub
End If
 

Users who are viewing this thread

Back
Top Bottom