How do I require a password to open a form in Access?

G

Guest

I am trying to set a password requirement on a button that opens another
form. I have seen it before through the use of code or a macro I just do not
remember how to set either one of them up. The database does not need to be
secured it is for students to sign in for a test. I just do not want them to
be able to open the other forms and edit instructor or grade data.
 
R

Rick Brandt

I am trying to set a password requirement on a button that opens another
form. I have seen it before through the use of code or a macro I just do
not remember how to set either one of them up. The database does not
need to be secured it is for students to sign in for a test. I just do
not want them to be able to open the other forms and edit instructor or
grade data.

At its most basic...

If InputBox("Enter Password") = "YourPassword" Then
DoCmd.OpenForm "FormName"
Else
MsgBox "Wrong Password"
End If

Problems are that the password is not masked as you type and unless you
distribute an MDE anyone can examine the code to see what the password is.
The above also relies on the form only being opened from the above code.
Somewhat better is to put the code in the Open event of the form...

If InputBox("Enter Password") <> "YourPassword" Then
MsgBox "Wrong Password"
Cancel = True
End If

That allows you to create the code only once and it won't matter what
mechanism is used to open the form.

If you created your own form for entering the password and opened it in
acDialog mode then you could use a password input mask so that only ****
would be displayed as the password was typed. In the [OK] button code of
that form just hide it rather than close it. The Open event of your
protected form would then look like...

DoCmd.OpenForm "PasswordEntry",,,,acDialog
If Forms!PasswordEntry!Password <> "YourPassword" Then
MsgBox "Wrong Password"
Cancel = True
End If
DoCmd.Close acForm, "PasswordEntry"

(guessing on the number of commas here in first line)
 

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