Login Form problem (1 Viewer)

Local time
Today, 17:50
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

  • My_DB.accdb
    2.5 MB · Views: 161

Gasman

Enthusiastic Amateur
Local time
Today, 15:50
Joined
Sep 21, 2011
Messages
14,044
I am not even going to download your db, just post the code within code tags. :(
 
Local time
Today, 17:50
Joined
Dec 18, 2021
Messages
46
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:

Gasman

Enthusiastic Amateur
Local time
Today, 15:50
Joined
Sep 21, 2011
Messages
14,044
That is not within code tags. :(
Regardless, I do not see how that code can do what you say it does? :(
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:50
Joined
Sep 21, 2011
Messages
14,044
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?
 

oleronesoftwares

Passionate Learner
Local time
Today, 08:50
Joined
Sep 22, 2014
Messages
1,159
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.
 

Minty

AWF VIP
Local time
Today, 15:50
Joined
Jul 26, 2013
Messages
10,354
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.
 
Local time
Today, 17:50
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.
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
 
Local time
Today, 17:50
Joined
Dec 18, 2021
Messages
46
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:50
Joined
Sep 21, 2011
Messages
14,044
Well that is not going to work :(
I could get in with someone else's password
 

Minty

AWF VIP
Local time
Today, 15:50
Joined
Jul 26, 2013
Messages
10,354
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:50
Joined
Sep 21, 2011
Messages
14,044
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?
 

Minty

AWF VIP
Local time
Today, 15:50
Joined
Jul 26, 2013
Messages
10,354
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:50
Joined
Feb 19, 2002
Messages
42,970
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

Top Bottom