Solved Login Form (1 Viewer)

Waka0212

New member
Local time
Today, 01:44
Joined
Oct 30, 2019
Messages
17
So, I've been putzing around with this code for a few days now for a login form. Everything seems to be working correctly except I cant get passed the strSQL. It pops up a "Run-time error '3061': Too few parameters. Expected 2." I'm using a thread from the following link.

wwwDOTdatanumen.com/blogs/how-to-protect-your-sensitive-data-with-a-login-form-in-access/

Any help would be appreciated. Please and thank you!


Private Sub cmd_login_Click()

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

If Trim(Me.txt_UserName.Value & vbNullString) = vbNullString Then
MsgBox prompt:="Username should not be left blank.", buttons:=vbInformation, title:="Username Required"
Me.txt_UserName.SetFocus
Exit Sub
End If

If Trim(Me.txt_Password.Value & vbNullString) = vbNullString Then
MsgBox prompt:="Password should not be left blank.", buttons:=vbInformation, title:="Password Required"
Me.txt_Password.SetFocus
Exit Sub
End If

'query to check if login details are correct
strSQL = "SELECT FirstName FROM tbl_login WHERE Username = """ & Me.txt_UserName.Value & """ AND Password = """ & Me.txt_Password.Value & """"

Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL)
If rst.EOF Then
MsgBox prompt:="Incorrect username/password. Try again.", buttons:=vbCritical, title:="Login Error"
Me.txt_UserName.SetFocus
Else
MsgBox prompt:="Hello, " & rst.Fields(0).Value & ".", buttons:=vbOKOnly, title:="Login Successful"
DoCmd.Close acForm, "frm_login", acSaveYes
End If

Set db = Nothing
Set rst = Nothing

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:44
Joined
Sep 21, 2011
Messages
14,048
Add a Debug.Print strSQL after creating strSQL and post that back.

I presume your fields are called Username and Password in the table.?

Also try
Code:
strSQL = "SELECT FirstName FROM tbl_login WHERE Username = '" & txt_UserName & "' AND Password = '" & txt_Password & "'"

as I was getting object required error when I tried your strSQL and that was just trying to create it.?
 
Last edited:

Waka0212

New member
Local time
Today, 01:44
Joined
Oct 30, 2019
Messages
17
OK, not sure if I did this right, but here is what I did and what I got.
Snip.PNG
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:44
Joined
Mar 14, 2017
Messages
8,738
As Gasman mentioned, I assume you have the exact same table name and column names as the code implies (tbl_login, FirstName, Password) as well as you have the identical form controls like the code (txt_UserName, txt_Password).

Personally, I always use single quotes when possible - I never mess around with the multitudinous double-quotes required to do it the other way - it's just making it more difficult to visualize than it has to be. Try

Code:
strSQL = "SELECT FirstName FROM tbl_login WHERE Username = '" & Me.txt_UserName.Value & "' AND Password = '" & Me.txt_Password.Value & "'

Please properly indent your code so that people can read, follow, and understand it's logic.
 

Waka0212

New member
Local time
Today, 01:44
Joined
Oct 30, 2019
Messages
17
Add a Debug.Print strSQL after creating strSQL and post that back.

I presume your fields are called Username and Password in the table.?

Also try
Code:
strSQL = "SELECT FirstName FROM tbl_login WHERE Username = '" & txt_UserName & "' AND Password = '" & txt_Password & "'"

as I was getting object required error when I tried your strSQL and that was just trying to create it.?

I tried your supplied code and still same result. The image above is with your supplied recommendation.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:44
Joined
Sep 21, 2011
Messages
14,048
OK, not sure if I did this right, but here is what I did and what I got. View attachment 85112
Yes, that was correct. Well done.
I must admit I am at a loss, as that looks perfectly good to me.:unsure:, so I am not sure why it would expect two parameters when they have been supplied correctly.

If you add another post, you will be able to upload the DB, just enough to replicate the problem.
I only have 2007, so there is a chance I will not be able to open it, but there are plenty of people here who could. :)
 

Waka0212

New member
Local time
Today, 01:44
Joined
Oct 30, 2019
Messages
17
Just for some clarification, the following are labeled on frm_login:
Username is txt_UserName
Password is txt_Password
Login button: cmd_login

The table is labeled tbl_login
Fields are:
UserID
LastName
txt_UserName
txt_Password

2.PNG

1.PNG

3.PNG
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:44
Joined
Sep 21, 2011
Messages
14,048
Yes, you pretty much copied verbatim from that link. Nothing wrong with that.

You now have 10 posts, upload your DB, zipped.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:44
Joined
Oct 29, 2018
Messages
21,358
Just for some clarification, the following are labeled on frm_login:
Username is txt_UserName
Password is txt_Password
Login button: cmd_login

The table is labeled tbl_login
Fields are:
UserID
LastName
txt_UserName
txt_Password

View attachment 85115

View attachment 85114

View attachment 85116
Hi. I think that answers your question (why the problem). Your SQL statement is looking for Username and Password, when your table doesn't have them (It has txt_UserName and txt_Password instead).
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:44
Joined
Sep 21, 2011
Messages
14,048
Hi. I think that answers your question (why the problem). Your SQL statement is looking for Username and Password, when your table doesn't have them (It has txt_UserName and txt_Password instead).
I stand corrected.
You did not copy that link verbatim, but changed two items. :(
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:44
Joined
Mar 14, 2017
Messages
8,738
Back to the original suggestions. Your code must match the columns and table name! When you copy and paste code you must make it match your reality in the DB.
 

Waka0212

New member
Local time
Today, 01:44
Joined
Oct 30, 2019
Messages
17
OMG! 😂 I feel so dumb, I can't believe I missed that... Well thank you all. After correcting those items, it worked perfectly! Thanks again.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:44
Joined
Oct 29, 2018
Messages
21,358
OMG! 😂 I feel so dumb, I can't believe I missed that... Well thank you all. After correcting those items, it worked perfectly! Thanks again.
Hi. Glad to hear you got it sorted out. Good luck with your project.
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:44
Joined
Mar 14, 2017
Messages
8,738
OMG! 😂 I feel so dumb, I can't believe I missed that... Well thank you all. After correcting those items, it worked perfectly! Thanks again.
Happens to everyone :)
Confusingly enough, that silly error message - Too few parameters, expected # - often refers to a simple typo in table, column names.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:44
Joined
Sep 21, 2011
Messages
14,048
Back to the original suggestions. Your code must match the columns and table name! When you copy and paste code you must make it match your reality in the DB.
@Isaac The problem here was if the o/p had done actually that, just left it as copy and paste and followed the instructions, it would have worked. :)
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:44
Joined
Mar 14, 2017
Messages
8,738
@Isaac The problem here was if the o/p had done actually that, just left it as copy and paste and followed the instructions, it would have worked. :)
When I look now at the originally referenced webpage I see what you mean.
 

Users who are viewing this thread

Top Bottom