Login Forms (1 Viewer)

tadgh

Registered User.
Local time
Today, 06:08
Joined
Dec 7, 2015
Messages
21
hi Im new to access, i have created a login form with the following code.

Option Compare Database
Option Explicit

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 <> Nz(Me.txtPassword, "") Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If
Me.lblWrongPass.Visible = False
DoCmd.OpenForm "UserInterface"
DoCmd.Close acForm, "frmLogin"
End Sub

I have a table called tbl1Employees with the following fields, Employee ID (primaryKey) Username, Password

The form has text box username and text box password. There are two invisible labels incorrect usernameand incorrect password and then a login button. Event procedure (on click) of login button is the above code but i just cannot get it to work any help appreciated in sorting this out
 

Minty

AWF VIP
Local time
Today, 14:08
Joined
Jul 26, 2013
Messages
10,382
When you say it doesn't work - what error are you getting?
Where in the code does it highlight / stop?

Also I think you need to refer to the password field as rs.Fields("Password") to refer to it by name?
 

tadgh

Registered User.
Local time
Today, 06:08
Joined
Dec 7, 2015
Messages
21
its not saying there is an error at any point nor giving an error code but the form does not work. its as though the code is unrelated to the form or something.
 

Minty

AWF VIP
Local time
Today, 14:08
Joined
Jul 26, 2013
Messages
10,382
Okay add either some message boxes to indicate progress through the code or add Debug.Print rs.Fields("Password") and Me.txtPassword so you can see in the immediate window what's happening and where it's going.
 

tadgh

Registered User.
Local time
Today, 06:08
Joined
Dec 7, 2015
Messages
21
Thanks but im new to this only learning. This code is copied and pasted from someone else but i will keep at it thanks
 

Minty

AWF VIP
Local time
Today, 14:08
Joined
Jul 26, 2013
Messages
10,382
Okay add the following to your code, it's very basic but should give you an idea of how to check things.

Code:
Option Compare Database
Option Explicit

Private Sub btnLogin_Click()
Dim rs As Recordset


Set rs = CurrentDb.OpenRecordset("tbl1Employees", dbOpenSnapshot, dbReadOnly)
[COLOR="Red"]
msgbox "Searching for " & me.txtUserName , vbExclamation, "First Check"[/COLOR]

rs.FindFirst "UserName='" & Me.txtUserName & "'"


If rs.NoMatch = True Then
[COLOR="Red"]
msgbox "No record found ", vbExclamation, "Second Check"[/COLOR]

Me.lblWrongUser.Visible = True
Me.txtUserName.SetFocus
Exit Sub
End If
Me.lblWrongUser.Visible = False

If rs!Password <> Nz(Me.txtPassword, "") Then
[COLOR="Red"]
msgbox "Password checked " & Me.txtPassword "Against " & rs!Password & " ;", vbExclamation, "Check 3" [/COLOR]

Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If

Me.lblWrongPass.Visible = False
DoCmd.OpenForm "UserInterface"
DoCmd.Close acForm, "frmLogin"
End Sub
 

tadgh

Registered User.
Local time
Today, 06:08
Joined
Dec 7, 2015
Messages
21
Thanks a million ill try that and see how it goes , really appreciate it dude
 

tadgh

Registered User.
Local time
Today, 06:08
Joined
Dec 7, 2015
Messages
21
Nope sorry that code is not working for me either, I have to be doing something simple in the wrong way.... i have noticed when i try to use the form it has a close button even though i selected no for close button......its as if when i save after entering code its not saving so the form does nothing,,,if i enter wrong user name incorrect user name does not appear and it allows me to tab on and enter password which again if entered incorrectly does not flash up incorrect password its as if the code never begins to run
 

Minty

AWF VIP
Local time
Today, 14:08
Joined
Jul 26, 2013
Messages
10,382
Okay - Right back to basics

Create a new button - click on event procedure in it's properties add one bit of code

Msgbox "Hello"

And see if it works. If it does add the rest of the code from Dim rs As Recordset onwards After it and try using that as your Login button.
 

tadgh

Registered User.
Local time
Today, 06:08
Joined
Dec 7, 2015
Messages
21
tried that it stil does not work, msgbox "hello" may as well not be there nothing shows on form not even word hello
i just cant understand whats wrong
 

Minty

AWF VIP
Local time
Today, 14:08
Joined
Jul 26, 2013
Messages
10,382
It sounds as if the whole form is corrupt.

Create a new form and just add the simple msbox. If that doesn't work I would post a stripped down copy of the database and let someone have a look.
 

tadgh

Registered User.
Local time
Today, 06:08
Joined
Dec 7, 2015
Messages
21
created new form, inserted button, event procedure,

Option Compare Database

Private Sub Command0_Click()
MsgBox "hello"
End Sub

save , open form in form view and click button not a thing happens lol ....
 

Minty

AWF VIP
Local time
Today, 14:08
Joined
Jul 26, 2013
Messages
10,382
Okay - it sounds as if you have somehow disabled any events / macros .

Have a look at your database properties.

Try the same in a brand new empty database.
 

tadgh

Registered User.
Local time
Today, 06:08
Joined
Dec 7, 2015
Messages
21
Found the problem minty and thanks a mill for your continued patient help and all those that helped on this thread, i have learned a lot .... the user that installed access set it up to disable all macros. Its now working away perfectly
 

Users who are viewing this thread

Top Bottom