MS Access 表单上的密码保护提交按钮仍会导致按钮提交
我正在尝试用密码保护我在 Microsoft Access 2003 中构建的表单上的提交按钮。单击该按钮时,会将新记录添加到我的数据库中。密码保护的想法是,当用户单击按钮时,会出现要求输入密码的提示。他们可以输入密码并单击“确定”以继续验证密码是否已正确输入,也可以单击“取消”并关闭提示窗口(之后他们将收到确认警报)。如果密码与硬编码的内容匹配,则会添加记录。如果密码不匹配,将显示错误消息。
这应该很容易。但是,无论密码输入错误、未输入密码或用户退出密码窗口,该记录都将始终添加到数据库中。下面的代码我做错了什么?
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", vbInformation, "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
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", vbInformation, "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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了实现您想要的行为,您必须设置表单以防止添加新记录。然后,询问用户密码,重新设置表单以启用添加新记录,然后移至新记录。
如果属性表不是
显示后,在“视图”菜单上单击
显示表单的属性
属性表。
在“表单”属性表中,单击
数据选项卡,然后设置
AllowAdditions
属性设置为 No。向
形式。设置命令
按钮的
OnClick
属性设置为 [Event步骤],然后单击构建
OnClick 右侧的按钮
属性框。输入以下内容
Form_Customers
中的语句模块:
http://support.microsoft.com/kb/208586
我不建议检查在用户输入数据后获取密码。当您填写表格,最后却发现您无权保存您的工作时,您会感到沮丧。
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 [EventProcedure], and then click the Build
button to the right of the OnClick
property box. Type the following
statement in the
Form_Customers
module:
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.
在表单上挂钩
BeforeInsert
事件,并在其中添加密码检查。如果他们没有提供正确的密码,您可以设置Cancel=True
,这将导致放弃添加记录。例子:
Hook the
BeforeInsert
event on your form, and add the password check there. You can setCancel=True
if they don't provide the correct password, and that will cause the addition of the record to be abandoned.Example:
尽量避免使用绑定表单,这将使您通过一些额外的工作更好地控制您的数据,最好的思考方式是从相反的两端处理您的数据。
绑定表单主要是告诉数据库阻止您不希望进行的更新,而未绑定表单更多的是在您绝对满意之前不进行任何更改。
这只是个人喜好,但我认为不受约束是值得花时间和精力的。
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.