Testing for Password on a Form

R

Rillo

My objective is to

a. Open a form on which the user is asked to enter a password
b. check the password
c. if the password is OK, close the form on which the password is entered
and open the next form
d. if the password is incorrect, display a message, and allow the user to
try again

I created a form called PASSWORD. I also have a table (LetMeIN) with one
record containing a record number (key field) and the valid password in a
field called “Passwordâ€
The reason the password is held in a table is that I can change it
periodically.

The PASSWORD form contains a text box (unbound) and a command button as
follows:

• Text box “Enter Password†– user enters password here
• Command Button – user clicks to proceed

I am somewhat of a novice when it comes to Access Visual Basic.
However, by “borrowing†code, my attempt at achieving my objective is shown
below.

The code only works if I change
If Me.ThePassword.Value = DLookup("Password", "tblLetMeIn) Then

To
If Me.ThePassword.Value = “123456†Then
(where 123456 is the current password)

By the way, does anybody know where I could obtain a book that would help me
understand Access Basic, preferably a book containing examples!

Here is my code, for what it is worth (the code is linked to the OnClick
event on the command button.
I am using Access 2003.

___________________________________________________________________
Private Sub Get_Into_System_Click()

'Check to see if data is entered into the password box

If IsNull(Me.ThePassword) Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.ThePassword.SetFocus
Exit Sub
End If

'Check value of password in LetMeIN table to see if this
'matches value entered on PASSWORD form

If Me.ThePassword.Value = "THENVAA" Then

' If Me.ThePassword.Value = DLookup([CurrentPassword], "tblLetMeIn",
‘ "[RecNo] = 1") Then

'Close logon form and open Reminder CD screen

DoCmd.Close acForm, "PASSWORD", acSaveNo
DoCmd.OpenForm "Reminder re CD"

Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
'Me.ThePassword.SetFocus
End If
End Sub
 
J

Jack Leach

I generally test passwords by looking at InStr with vbBinaryCompare, and a
Len() comparison

If Instr(1, RealPassword, EnteredPassword, , vbBinaryCompare) = 1 _
AND _
(Len(RealPassword) = Len(EnteredPassword)) _
Then

'password is correct

Else

'password not correct

End If



Instr will test the existence of one string in another, case sensitive when
the vbBinaryCompare argument is set (don't believe the default to this...
doesn't work)

Len makes sure that the entered pass and real pass are the same number of
characters. So, if instr = 1 and Len = Len, the password has to be correct.



I did this before I learned of the StrComp function, which *should* do the
same thing easier, but my test in the immediate:

?strcomp("ASD", "asd", vbBinaryCompare)
-1
?cint(true)
-1

.... tells me otherwise. I don't know what MS's problem with binary compare
is, StrComp("ASD", "asd", vbBinaryCompare) is supposed to return False (0)
rather than true (-1), and there was a recent post where the user needed to
explicitly enter the vbBinaryCompare argument to the InStr function, even
though it's supposed to be the default. Perhaps Option Compare Database
overrides this... not sure.

Anyway, checking InStr and Len works.

hth

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



Rillo said:
My objective is to

a. Open a form on which the user is asked to enter a password
b. check the password
c. if the password is OK, close the form on which the password is entered
and open the next form
d. if the password is incorrect, display a message, and allow the user to
try again

I created a form called PASSWORD. I also have a table (LetMeIN) with one
record containing a record number (key field) and the valid password in a
field called “Passwordâ€
The reason the password is held in a table is that I can change it
periodically.

The PASSWORD form contains a text box (unbound) and a command button as
follows:

• Text box “Enter Password†– user enters password here
• Command Button – user clicks to proceed

I am somewhat of a novice when it comes to Access Visual Basic.
However, by “borrowing†code, my attempt at achieving my objective is shown
below.

The code only works if I change
If Me.ThePassword.Value = DLookup("Password", "tblLetMeIn) Then

To
If Me.ThePassword.Value = “123456†Then
(where 123456 is the current password)

By the way, does anybody know where I could obtain a book that would help me
understand Access Basic, preferably a book containing examples!

Here is my code, for what it is worth (the code is linked to the OnClick
event on the command button.
I am using Access 2003.

___________________________________________________________________
Private Sub Get_Into_System_Click()

'Check to see if data is entered into the password box

If IsNull(Me.ThePassword) Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.ThePassword.SetFocus
Exit Sub
End If

'Check value of password in LetMeIN table to see if this
'matches value entered on PASSWORD form

If Me.ThePassword.Value = "THENVAA" Then

' If Me.ThePassword.Value = DLookup([CurrentPassword], "tblLetMeIn",
‘ "[RecNo] = 1") Then

'Close logon form and open Reminder CD screen

DoCmd.Close acForm, "PASSWORD", acSaveNo
DoCmd.OpenForm "Reminder re CD"

Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
'Me.ThePassword.SetFocus
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top