Password protecting forms

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.

Code:private 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:

Code:private 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???
 
K

Keith Wilby

I am trying to password protect my form and I used this code that I
found on a website:
but I am not sure. Any suggestions with this code???

I can't help you with your code but have you considered implementing the
built-in user-level security? There's a steep learning curve but if you
have a need to protect one form you can bet that, as you app grows, so
you'll have another form that you'll want to protect slightly differently.

Just my 2p worth.

Keith.
www.keithwilby.com
 
G

Guest

The error you are getting is not a problem in the code itself, but a problem
with the Option statments in the form module. It indicates there is more
than one of the same Option statement.
Look at the very top of the form module. You will see something like:
Option Compare Database
Option Base 0
Option Explicit
etc.

The Options tell the module how to handle certain conditions or situations.

Now, the real issue here is the value of this kind of security. It is
pretty much like leaving the key in the lock. It doesn't take much to get
around it. You would be better served to take the time to study Access
security and use that.


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.

Code:private 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:

Code:private 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???
 

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

Similar Threads


Top