My database consists of multiple forms. I am wondering if it's possible to password protect one of the forms in my database (and not the whole database). How do I do this?
The best way would be to implement security with Access. Please keep in mind that users, if they know how, can access information directly off the data tables.
Access security will allow you to control access at the form and table level.
Okay, back to the form -- there are a couple of tricks, and they are just tricks, not real security...
The sequence of events for a from are...
Open -> Load -> Resize -> Activate -> Current
Data is not loaded until the current event occurs.
Consequently, at the form level, you can use the On Load event to close the form.
if strPass <> HoweverYouGetYourPassword then
DoCmd.Close acForm, YourFromName
end if
Perhaps a neater solution is to open a password form. After the password validation has occurred, then hide the password form - make it invisible, then in the load of the form in question, for the On Load event, check to make sure the password form is still open. If not close the form. (But remember to make the password form visible and close it upon closing the main form in question....
After loading main form from password form, for the On Load event...
if Not IsLoaded("YourPasswordForm" then
DoCmd.Close acForm, YourFromName
end if
What is wrong with the form control...
- Data can be viewed at the table level
- The form could be opened in edit mode, and code disabled
If it is really important, consider implementing security. (BUT make sure you have backups)
In the on open event of the form use some VB code like this:
Private Sub Form_Open(Cancel As Integer)
Pword = InputBox("Enter Password"
If Pword <> "YourPassword" Then
MsgBox "Password Incorrect"
DoCmd.CancelEvent
End If
End Sub
Luceze, I copied and pasted your code into my form (but changed the password), but when I open the form I get "Variable not defined" and "Pword = InputBox..." is highlighted. What did I do wrong?
Private Sub Form_Open(Cancel As Integer)
dim Pword as string
Pword = InputBox("Enter Password"
If Pword <> "YourPassword" Then
MsgBox "Password Incorrect"
DoCmd.CancelEvent
End If
End Sub
If Pword <> "YourPassword" Then
MsgBox "Password Incorrect"
DoCmd.CancelEvent
End If
End Sub
Okay, but you may still be missing a step. The way the current code reads is that the user has to type "YourPassword" to open the form. This is called "hard coding".
Richard, I changed "YourPassword" to the password of my choice. Isn't this sufficient? (The people I am protecting this form from are NOT computer literate and won't think to open the form in design view or even look at the table.) I am relatively new to Access and don't understand complicated coding or other complicated procedures, so when I can just copy and paste something into my form, I always go for that option! (I didn't quite understand your notes about "password forms," etc.) The only problem I have with Eric's code is I'd rather not have the password showing as it is typed, but have *'s appear instead. Is there any way to accomplish this?
The way to have your user's password masked as **** is to create another form with an unbound textbox. Name the text box "txtPword" In the textbox's input mask property (on the Data tab)type "password". Then put a button on the form. In the on click event put this code:
if me.txtPword="YourPassword" then
docmd.openform "YourFormName"
docmd.close acform,"YourPasswordFormName"
else
msgbox "Incorrect Password",vbOkOnly,"Password Error"
docmd.close acform, "YourPasswordFormName"
end if
Now format the form to look how you want it to.
If you are using a switchboard have the switchboard open this form instead of the form you are trying to protect.
As Richard said above this is a makeshift way to implement security as users with any knowledge of Access will be able to open the form regardless.
As you delve further along the VBA coding world...
You have done a great job by just getting the code entered!
Eric has provided something a little more sophisticated per my line of thinking, "Neater solution...", by using a form instead of an input box.
If you wish to hardcode the password, that is fine.
You can make it a little harder by storing the password in a table, or on another form that is already opened as a hidden field (such as the switchboard).
Regardless, it seems that you now have at least a partial resolution.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.