Newbie
Newbie

Reputation: 55

Require Password to Open Access Form

I have a few forms in my Access database that only a few team members should be using I wrote the following to password-protect those forms, but if you enter the wrong password or hit cancel on the pop-up, it will open the form anyway.

How can I prevent Access from loading the form unless you enter the correct password?

Private Sub Form_Load()
    Dim strInput As String
    Dim strMsg As String

    Beep
    strMsg = "This function requires a password." & vbCrLf & vbLf & "Please see your manager for assistance."
    strInput = InputBox(Prompt:=strMsg, title:="Password:")
    If strInput = "administrator" Then 'password is correct
        DoCmd.OpenForm "AddNewRule"
    Else 'password is incorrect
        MsgBox "Incorrect Password!" & vbCrLf & vbLf & "Please see your manager for assistance", vbCritical, "Invalid Password"
        Exit Sub
    End If
End Sub

Upvotes: 1

Views: 1287

Answers (1)

Andre
Andre

Reputation: 27644

Instead of Load, use the Open event:

Private Sub Form_Open(Cancel As Integer)

There you can set Cancel = True, then the form won't be opened.

But note that if you open the form from somewhere else with DoCmd.OpenForm, it will raise an error that you need to trap or ignore.


Note that DoCmd.OpenForm "AddNewRule" in the success case is wrong, you are already opening the form.

Upvotes: 1

Related Questions