Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

P/w protecting a form

Status
Not open for further replies.

Minuet

Technical User
Dec 18, 2002
48
CA
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?
 
Minuet

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.

Dim strPass as String

strPass = InputBox("Enter access code: ", "Validation", "****")

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(&quot;YourPasswordForm&quot;) 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)

Richard
 
In the on open event of the form use some VB code like this:

Private Sub Form_Open(Cancel As Integer)
Pword = InputBox(&quot;Enter Password&quot;)
If Pword <> &quot;YourPassword&quot; Then
MsgBox &quot;Password Incorrect&quot;
DoCmd.CancelEvent
End If
End Sub

HTH,
Eric
 
Luceze, I copied and pasted your code into my form (but changed the password), but when I open the form I get &quot;Variable not defined&quot; and &quot;Pword = InputBox...&quot; is highlighted. What did I do wrong?
 
Sorry. Try changing the code to this:

Private Sub Form_Open(Cancel As Integer)
dim Pword as string
Pword = InputBox(&quot;Enter Password&quot;)
If Pword <> &quot;YourPassword&quot; Then
MsgBox &quot;Password Incorrect&quot;
DoCmd.CancelEvent
End If
End Sub
 
Private Sub Form_Open(Cancel As Integer)

[blue]Dim Pword as String[/blue]

Pword = InputBox(&quot;Enter Password&quot;)

If Pword <> &quot;YourPassword&quot; Then
MsgBox &quot;Password Incorrect&quot;
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 &quot;YourPassword&quot; to open the form. This is called &quot;hard coding&quot;.

Richard
 
Eric, thanks! Your code now works!

Richard, I changed &quot;YourPassword&quot; 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 &quot;password forms,&quot; 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 &quot;txtPword&quot; In the textbox's input mask property (on the Data tab)type &quot;password&quot;. Then put a button on the form. In the on click event put this code:


if me.txtPword=&quot;YourPassword&quot; then
docmd.openform &quot;YourFormName&quot;
docmd.close acform,&quot;YourPasswordFormName&quot;
else
msgbox &quot;Incorrect Password&quot;,vbOkOnly,&quot;Password Error&quot;
docmd.close acform, &quot;YourPasswordFormName&quot;
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.

HTH,
Eric
 
Minuet

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, &quot;Neater solution...&quot;, 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.

Good lcuk on the rest.
Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top