By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,146 Members | 1,705 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

How to program command button in Access Macro to require a password

P: 20
I created a simple macro to open a form with several command buttons on the form. On one of these buttons I want to require a password for access. I am not a programmer. What do I need to do to make this happen?

Thanks for your help
Oct 21 '10 #1

✓ answered by Rixxe

You really should google in the first instance, as many of the answers you want are avaliable.

None the less:

Open the switchboard form in design view and select the command button.
Select the On Click event and choose Event Procedure from the drop down list. Click on the ellipses (...) to enter the code window.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOpenEmpForm_Click()
  2.  
  3. 'Attached to On Click event of cmdOpenEmpForm
  4.  
  5.     Dim strPasswd
  6.  
  7.     strPasswd = InputBox("Enter Password", "Restricted Form")
  8.  
  9.     'Check to see if there is any entry made to input box, or if
  10.     'cancel button is pressed. If no entry made then exit sub.
  11.  
  12.     If strPasswd = "" Or strPasswd = Empty Then
  13.         MsgBox "No Input Provided", vbInformation, "Required Data"
  14.         Exit Sub
  15.     End If
  16.  
  17.     'If correct password is entered open Employees form
  18.     'If incorrect password entered give message and exit sub
  19.  
  20.     If strPasswd = "Graham" Then
  21.         DoCmd.OpenForm "frmEmp", acNormal
  22.     Else
  23.         MsgBox "Sorry, you do not have access to this form", _
  24.                vbOKOnly, "Important Information"
  25.         Exit Sub
  26.     End If
  27. End Sub
  28.  
You will notice the line If strPasswd = "Graham" Then . This contains the required password that will be required when attempting to open the form.

This can be changed to any password that you wish.


(Note: This information is originally from, and can be found at: http://www.databasedev.co.uk/button_security.html)

Share this Question
Share on Google+
3 Replies


P: 7
You really should google in the first instance, as many of the answers you want are avaliable.

None the less:

Open the switchboard form in design view and select the command button.
Select the On Click event and choose Event Procedure from the drop down list. Click on the ellipses (...) to enter the code window.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOpenEmpForm_Click()
  2.  
  3. 'Attached to On Click event of cmdOpenEmpForm
  4.  
  5.     Dim strPasswd
  6.  
  7.     strPasswd = InputBox("Enter Password", "Restricted Form")
  8.  
  9.     'Check to see if there is any entry made to input box, or if
  10.     'cancel button is pressed. If no entry made then exit sub.
  11.  
  12.     If strPasswd = "" Or strPasswd = Empty Then
  13.         MsgBox "No Input Provided", vbInformation, "Required Data"
  14.         Exit Sub
  15.     End If
  16.  
  17.     'If correct password is entered open Employees form
  18.     'If incorrect password entered give message and exit sub
  19.  
  20.     If strPasswd = "Graham" Then
  21.         DoCmd.OpenForm "frmEmp", acNormal
  22.     Else
  23.         MsgBox "Sorry, you do not have access to this form", _
  24.                vbOKOnly, "Important Information"
  25.         Exit Sub
  26.     End If
  27. End Sub
  28.  
You will notice the line If strPasswd = "Graham" Then . This contains the required password that will be required when attempting to open the form.

This can be changed to any password that you wish.


(Note: This information is originally from, and can be found at: http://www.databasedev.co.uk/button_security.html)
Oct 21 '10 #2

P: 20
Thanks very much. Worked great.
Oct 21 '10 #3

P: 7
You're welcome.
Oct 22 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.