Password locking a Form

  • Thread starter Thread starter Greg Staley
  • Start date Start date
G

Greg Staley

I tried this, but any word I enter, or even enter by itself, gets past the
input box and opens the form. Anyone else have an idea as to what I can
do? I am just trying to keep the ID 10 Ts from accesing the form, nothin
serious.

Greg

A crude way of doing this would be as follows:

Private Sub Form_Load()
Dim sPassWord As String
Const sAccess = "Password"
sPassWord = InputBox("Please enter the password for
the form", "Login")

If sPassWord <> sAccess Then
DoCmd.Close acForm, "form Name", acSaveNo
End If
End Sub
 
Greg Staley said:
I tried this, but any word I enter, or even enter by itself, gets past the
input box and opens the form. Anyone else have an idea as to what I can
do? I am just trying to keep the ID 10 Ts from accesing the form, nothin
serious.

I'm not sure you can put a close form command in the same form's Load event.

Use the Open event of the form and if the password is wrong you can set Cancel = True
and the form will never finish opening. That way you don't need a command to close
it.
 
I assume you're getting to this "password protected form" from a button on
another form. If so, then have the button go to an intermediary form; let's
call it FrmPassword

On this form [FrmPassword], create an unbound text box called password, and
set it's input mask property to password, and it's default value to ""

Now, either on the onchange of the text box, or on a button on this password
form, use this code.

Function CheckPass()
On Error GoTo CheckPass_Err

With CodeContextObject
If (.Password = "lemon") Then
DoCmd.OpenForm "TheForm", acNormal, "", "", , acNormal
Forms!frmpassword.Visible = False
Exit Function
End If
Beep
MsgBox "Incorrect Password - Please Try Again", vbExclamation,
"Password Check"
DoCmd.OpenForm "frmPassword", acNormal, "", "", , acNormal
End With


CheckPass_Exit:
Exit Function

CheckPass_Err:
MsgBox Error$
Resume CheckPass_Exit

End Function

Good Luck
 
Back
Top