I have a database which is available for everyone to view (but not modify). I have one form to enable data add/edit which I want to password protect rather than setting up user accounts and access rights.
So only those authorised to edit/add can get to that form.
I have a main menu with a command button to open it - is the button protected or the form?
If it was me doing this id open a seperate form for your password on the click of your button on the main form, when the user inputs the correct password then you can then proceed to your protected form, but thats just me
I'd do both
If someone can view the database then they can open the form, so you probably need to put some code into the forms "on open" event and chuck 'em out if not authenticated. I often also set whether the usergroup is allowd to add,edit, change or delete in the "on open" event. The alternative is to set the workgrop permisions so that users cannot read the design of the forms
As a design stylistic I'd disable the button unless they can use the button (and therefore view the form).
I have just checked it on my computer , it is working fine, the important part of the code to prevent the form from staying open is the DoCmd.Close check that is correct.
if you want to have the password on a command button on another form then the code would be like this
Private Sub cmdopenform_Click()
Dim x As String
x = "password"
Dim y As String
y = InputBox("Enter Password for form")
If x <> y Then
MsgBox "wrong Password"
Exit Sub ' wrong password take no further action exit
Else 'Password is correct continue to open the form
Dim stDocName As String
stDocName = "YourFormName"
Don't forget that a user can also load the form directly (from the database dialog), without going through your switchboard or menu.
So I'd also put some validation in the forms "open" event, it can refer to the password control on, or variable in the calling form, and if not found cancel the form load.
private sub form_open(cancel as integer)
if forms!<parentform>!<controlsource> <> vbtrue then
this assumes that
<controlsource> has been set if the password is OK
so if the parent form was called mainmenu the controlsource was IsValid then
Private Sub Form_Open(Cancel As Integer)
on error goto cancelload
if forms!mainfrorm!isvalid <> vbtrue then
on error resume next
cancelload: 'the main reason for geting here is if an error has occured
'likely candidates are the calling form isn't loaded
Remember to make your database as MDE file and keep the MDB file as a master backup in case you need modify it. That way no one could mess around with your form design. Since most access beginer know how go to Design the Code of the form then they will get the password easily.