Open a form with password

jedder18

Just Livin the Dream!
Local time
Today, 11:07
Joined
Mar 28, 2012
Messages
135
Access 2007 db, Win7
Have a button setup to open a form that only 1 person can have access to.
Would like to generate a password prompt.
Would like it to be a simple on button click, password prompt shows up, user enters and form opens.
Would like to do this without 85 lines of code :rolleyes:
I'll set the password as "Legal"
Any help? Hoping it can be a simple IIF statement onclick???

TIA...

Jen
 
I think the solution offered by pr2-eugin is the way to go but a more simplistic solution might be to use the code below in the Click event of the button:
Code:
  If InputBox("Password Required") = "Legal" Then
    DoCmd.OpenForm "[B][COLOR=red]YourFormName[/COLOR][/B]"
  Else
    MsgBox "Password is NOT correct."
  End If
 
I got it figured out with a combo of many things :)
Thanks for all the help....
 
my final code that worked for anyone else searching...


Private Sub Command673_Click()
On Error GoTo Err_Command673_Click
Retry:
Dim strInput As String
strInput = InputBox("Password", "Enter Password")
If strInput = "legal" Then
DoCmd.OpenForm "Legal subform", acNormal
Else
If MsgBox("Incorrect password. Retry?", vbQuestion + vbYesNo, "Password Error") = vbYes Then
GoTo Retry
End If
End If
Exit_Command673_Click:
Exit Sub
Err_Command673_Click:
MsgBox Err.Description
Resume Exit_Command673_Click
End Sub
 
Another way would be to use the Cancel argument of the Form_Open event to do the work for you. The first has a hard-coded password.

Code:
Private Sub Form_Open(Cancel As Integer)
Dim lngTries As Long, strPassword As String, strPrompt As String
On Error Resume Next

lngTries = 3
strPrompt = "Enter Password"

Do
    Cancel = True
    strPassword = LCase(InputBox(strPrompt, "Protected Form", ""))
    Cancel = (strPassword <> "legal")
    lngTries = lngTries - 1
    If (Cancel = False) Or (strPassword = "") Or (lngTries <= 0) Then Exit Do
    strPrompt = "Incorrect password. " & lngTries & " tries remaining."
Loop

End Sub

The second, if you didn't want to hard code the password, uses the tag property of the form instead.

Code:
Private Sub Form_Open(Cancel As Integer)
Dim lngTries As Long, strPassword As String, strPrompt As String
On Error Resume Next

lngTries = 3
strPrompt = "Enter Password"

Do
    Cancel = True
    strPassword = LCase(InputBox(strPrompt, "Protected Form", ""))
    Cancel = (strPassword <> LCase(Me.Tag))
    lngTries = lngTries - 1
    If (Cancel = False) Or (strPassword = "") Or (lngTries <= 0) Then Exit Do
    strPrompt = "Incorrect password. " & lngTries & " tries remaining."
Loop

End Sub
 

Users who are viewing this thread

Back
Top Bottom