Password protecting submit button on MS Access form still results in button submission
I'm trying to password protect a submit button on a form I'm building in Microsoft Access 2003. The button, when clicked, will add a new record to my database. The idea of the password protection is that when the user clicks the button, a prompt will appear asking for a password. They can either enter the password and click OK to proceed with verifying it has been entered properly, or they can click Cancel and close the prompt window (after which they will receive an confirmation alert). If the password matches what is hardcoded, the record will be added. If the password is not a match, an error message will display.
This should be easy enough. However, the record will ALWAYS be added to the database, no matter if the password is entered incorrectly, no password is entered, or the user cancels out of the password window. What am I doing wrong with the below code?
Private Sub AddLeadServerButton_Click()
Dim strPasswd
strPasswd = InputBox("Enter Password", "Restricted Form")
'Check to see if there is any entry made to input box, or if
'cancel button is pressed. If no entry made then exit sub.
If strPasswd = "" Or strPasswd = Empty Then
MsgBox "No Input Provided", vbInform开发者_C百科ation, "Required Data"
Exit Sub
End If
'If correct password is entered open Employees form
'If incorrect password entered give message and exit sub
If strPasswd = "thepassword" Then
DoCmd.GoToRecord , , acNewRec
Me.Parent!NewInstallation.Form!InstallationLeadServerComboBox.Requery
Me.Parent!NewReport.Form!LeadServerFilterComboBox.Requery
Else
MsgBox "Sorry, you do not have access to this form", _
vbOKOnly, "Important Information"
Exit Sub
End If
End Sub
To accomplish the behavior you want, you will have to set the form to prevent new records from being added. Then, ask the user for the password, set the form back to enable adding new records, and move to the new record.
If the property sheet is not displayed, on the View menu, click Properties to display the form's property sheet.
In the Form property sheet, click the Data tab, and then set the
AllowAdditions
property to No.Add a command button to the form. Set the command button's
OnClick
property to [Event Procedure], and then click the Build button to the right of the OnClick property box. Type the following statement in theForm_Customers
module:Forms!Customers.AllowAdditions = True
http://support.microsoft.com/kb/208586
I don't recommend checking for the password after the user has already entered data. It gets frustrating when you fill out a form, only to find out at the end that you don't have rights to save your work.
Hook the BeforeInsert
event on your form, and add the password check there. You can set Cancel=True
if they don't provide the correct password, and that will cause the addition of the record to be abandoned.
Example:
Private Sub Form_BeforeInsert(Cancel As Integer)
If MsgBox("Insert new record here?", _
vbOKCancel) = vbCancel Then
Cancel = True
End If
End Sub
Try to avoid using bound forms where you can, this will give you greater control over your data with a little extra work, the best way to think of it is addressesing your data from opposite ends.
Bound forms are very much about telling the database to prevent updates that you don't wish to make whereas unbound forms are more about not making any changes until you are absolutely happy.
Just a personal preference but I think unbound are worth the time and effort.
精彩评论