Back for more huh?
Well there's probably a bunch of different ways to do this, but here's just one.
It's a little clumsy, but it works.
1. On the form that asks for the Password make the following changes on the Form's Properties list:
Control Box - No
Close Button - No
2. On the password form add a command button called cmdCancel and set the caption to be Cancel.
Put this code in the click event of the command button:
Private Sub cmdCancel_Click()
MyPassword = "NotThisOne"
DoCmd.Close acForm, "frmPassword"
End Sub
Substitute your actual name of this form.
3. Compile the code and save/close the form.
4. Open the protected form and enter this code into the Open event:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Error_Handler
Dim Hold As Variant
Dim tmpKey As Long
Dim I As Integer
Dim rs As DAO.Recordset
Dim db As DAO.Database
' Prompt the user for the Password.
DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
Hold = MyPassword
' User pressed the Cancel button on frmPassword
If MyPassword = "NotThisOne" Then
Cancel = True
Exit Sub
End If
' Open the table that contains the password.
Set db = CurrentDb
Set rs = db.OpenRecordset("tblPassword", dbOpenTable)
rs.Index = "PrimaryKey"
rs.Seek "=", Me.Name
If rs.NoMatch Then
MsgBox "Sorry cannot find password information. Try Again"
Cancel = -1
Else
' Test to see if the key generated matches the key in
' the table; if there is not a match, stop the form
' from opening.
If Not (rs![KeyCode] = KeyCode(CStr(Hold))) Then
MsgBox "Sorry you entered the wrong password." & _
"Try again.", vbOKOnly, "Incorrect Password"
Cancel = -1
End If
End If
ExitPoint:
rs.Close
db.Close
Exit Sub
Error_Handler:
MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number
Resume ExitPoint
End Sub
5. Compile the code, save/close the form and then try it out.
--
Jeff Conrad
Access Junkie
Bend, Oregon
The password works great until you hit the x to close the password form.
What happens is the form I wanted to password protect opens anyway. I tried
selecting NO for the close button but this doesn't work either. If someone
either doesn't know the password or forgot it, i need to have the ability to
close the form period...Any ideas
You're welcome, glad to help.
--
Jeff Conrad
Access Junkie
Bend, Oregon
That was it! Thanks Jeff
Did you manually type in the password you wanted right into the table
by
chance?
If so that's not how to enter the password.
You have to go to the immediate window (CTRL-G) and then type this in
and
press enter:
?KeyCode("yourpasswordhere")
Replace 'yourpasswordhere' with your actual password.
You will be given a number in the Immediate Window.
THAT number needs to go into the table (and it could be negative if I
remember correctly).
Remember when you type in your password on the form it will be case
sensitive I believe.
--
Jeff Conrad
Access Junkie
Bend, Oregon
I want to password protect a form. (Acc.2000) I copied MS Knowledge
Base
Artical- 209871, "How to create a password protected form or report"
I
went
through the instructions step by step, word by word, letter by
letter.
Anyway what happens is when I enter the password, the msgbox pops
up
stating "I have entered the wrong password", even though I have the
correct
password listed in the password table. Does anyone have any
experience
with
this and any solutions...Thanks..Randy