R
rblivewire
I am trying to password protect my form and I used this code that I
found on a website:
Option Compare Database
Dim stLinkCriteria As String
Dim stDocName As String
Dim counter As Integer 'This variable is designed to count the number
of times the user
'attempts to enter the password. If it has reached a certain point,
they will be locked-out.
Private Sub Form_Load()
stDocName = "Tool Tracking List" 'The form you are protecting;
frmPassword will pop up
'when this form is opened.
counter = 0 'reset the counter
End Sub
Now, you need to set up the OK and Close buttons. The Close button is
pretty straight-forward, but the OK button has a bit of logic in it.
Coderivate Sub cbOK_Click()
On Error GoTo Err_cbContinue_Click
counter = counter + 1 'Add 1 each time they try to enter a password
If LCase(txtPassword.Value) = LCase("YourPassword") Then
'If the entered password is the same as your pre-set password,
then...
'NOTE: this could also be found in a table. That way, the password
would not be in code,
'which is often safer.
DoCmd.Close
ElseIf counter = 3 Then
'If they try 3 times with the wrong password, they are locked out
MsgBox "You have been locked out for failing to enter the
correct password.", _
vbCritical, "Incorrect Password"
'Close this form and the protected form
DoCmd.Close
DoCmd.Close stDocName, , , stLinkCriteria
Else
'On the 1st and 2nd wrong tries, you go here to give the user
another chance
MsgBox "The password you have entered is incorrect.",
vbExclamation, _
"Incorrect Password"
txtPassword.SetFocus
End If
Exit_cbContinue_Click:
Exit Sub
Err_cbContinue_Click:
MsgBox "Error #" & Err.Number & " - " & Err.Description,
vbCritical, "Error"
Resume Exit_cbCancel_Exit
End Sub
Private Sub cbCancel_Click()
On Error GoTo Err_cbCancel_Click
DoCmd.Close
Exit_cbCancel_Click:
Exit Sub
Err_cbCancel_Click:
MsgBox "Error #" & Err.Number & " - " & Err.Description,
vbCritical, "Error"
Resume Exit_cbCancel_Exit
End Sub
This all ensures that your form works correctly. Now you need to make
sure the form looks right. Open frmPassword in Detail View and open the
properties of txtPassword. Under the Data tab, set Input Mask =
"Password" (when you type, you just get *** instead of letters). Also
be sure that the On Click properties for cbOK and cbCancel are set =
[Event Procedure].
Open the properties for Form (click on the dark gray space to the
side--not Detail or Header). Under the Other tab, set Pop Up = Yes and
Modal = Yes. This makes the form open like a pop-up, as well as keeping
the focus until closed. Under the Format tab, set Boder Style = Dialog.
This makes the form's size unchangeable by the user.
Now, open the form you want to password protect. To ensure that the
password is opened before the rest of the form, go to the VB code and
add this code:
Coderivate Sub Form_Open(Cancel As Integer)
Dim stLinkCriteria As String
DoCmd.OpenForm "frmPassword", , , stLinkCriteria
'If you have other commands in this sub, they would go here (after
opening the password)
End Sub
When I run this code, The box pops up fine, but if you press any button
on the form (OK or Cancel) it gives me the message "The expression On
Load you entered as the event property setting produced the following
error: Duplicate option statement. I cannot get this to work. I have
a feeling it is something in this portion:
Private Sub Form_Load()
stDocName = "Tool Tracking List" 'The form you are protecting;
frmPassword will pop up
'when this form is opened.
counter = 0 'reset the counter
End Sub
but I am not sure. Any suggestions with this code???
found on a website:
Option Compare Database
Dim stLinkCriteria As String
Dim stDocName As String
Dim counter As Integer 'This variable is designed to count the number
of times the user
'attempts to enter the password. If it has reached a certain point,
they will be locked-out.
Private Sub Form_Load()
stDocName = "Tool Tracking List" 'The form you are protecting;
frmPassword will pop up
'when this form is opened.
counter = 0 'reset the counter
End Sub
Now, you need to set up the OK and Close buttons. The Close button is
pretty straight-forward, but the OK button has a bit of logic in it.
Coderivate Sub cbOK_Click()
On Error GoTo Err_cbContinue_Click
counter = counter + 1 'Add 1 each time they try to enter a password
If LCase(txtPassword.Value) = LCase("YourPassword") Then
'If the entered password is the same as your pre-set password,
then...
'NOTE: this could also be found in a table. That way, the password
would not be in code,
'which is often safer.
DoCmd.Close
ElseIf counter = 3 Then
'If they try 3 times with the wrong password, they are locked out
MsgBox "You have been locked out for failing to enter the
correct password.", _
vbCritical, "Incorrect Password"
'Close this form and the protected form
DoCmd.Close
DoCmd.Close stDocName, , , stLinkCriteria
Else
'On the 1st and 2nd wrong tries, you go here to give the user
another chance
MsgBox "The password you have entered is incorrect.",
vbExclamation, _
"Incorrect Password"
txtPassword.SetFocus
End If
Exit_cbContinue_Click:
Exit Sub
Err_cbContinue_Click:
MsgBox "Error #" & Err.Number & " - " & Err.Description,
vbCritical, "Error"
Resume Exit_cbCancel_Exit
End Sub
Private Sub cbCancel_Click()
On Error GoTo Err_cbCancel_Click
DoCmd.Close
Exit_cbCancel_Click:
Exit Sub
Err_cbCancel_Click:
MsgBox "Error #" & Err.Number & " - " & Err.Description,
vbCritical, "Error"
Resume Exit_cbCancel_Exit
End Sub
This all ensures that your form works correctly. Now you need to make
sure the form looks right. Open frmPassword in Detail View and open the
properties of txtPassword. Under the Data tab, set Input Mask =
"Password" (when you type, you just get *** instead of letters). Also
be sure that the On Click properties for cbOK and cbCancel are set =
[Event Procedure].
Open the properties for Form (click on the dark gray space to the
side--not Detail or Header). Under the Other tab, set Pop Up = Yes and
Modal = Yes. This makes the form open like a pop-up, as well as keeping
the focus until closed. Under the Format tab, set Boder Style = Dialog.
This makes the form's size unchangeable by the user.
Now, open the form you want to password protect. To ensure that the
password is opened before the rest of the form, go to the VB code and
add this code:
Coderivate Sub Form_Open(Cancel As Integer)
Dim stLinkCriteria As String
DoCmd.OpenForm "frmPassword", , , stLinkCriteria
'If you have other commands in this sub, they would go here (after
opening the password)
End Sub
When I run this code, The box pops up fine, but if you press any button
on the form (OK or Cancel) it gives me the message "The expression On
Load you entered as the event property setting produced the following
error: Duplicate option statement. I cannot get this to work. I have
a feeling it is something in this portion:
Private Sub Form_Load()
stDocName = "Tool Tracking List" 'The form you are protecting;
frmPassword will pop up
'when this form is opened.
counter = 0 'reset the counter
End Sub
but I am not sure. Any suggestions with this code???