我创建了一个登录表单,其中包含用户类型(管理员,用户)的组合框和密码的文本框。表格的代码如下。
Private Sub txtPassword_AfterUpdate()
If IsNull(Me.cboUser) Then
MsgBox "You need to select a user!", vbCritical
Me.cboUser.SetFocus
Else
If Me.txtPassword = Me.cboUser.Column(2) Then
If Me.cboUser.Column(3) = True Then
MsgBox "Password does not match, please re-enter!", vboOkOnly
Me.txtPassword = Null
Me.txtPassword.SetFocus
End If
DoCmd.OpenForm "FE1"
Me.Visible = False
Else
MsgBox "Password does not match, please re-enter!", vboOkOnly
Me.txtPassword = Null
Me.txtPassword.SetFocus
End If
End If
End Sub
Private Sub cboUser_AfterUpdate()
Forms!frmLogin!cboUser.Column (2)
End Sub
如果登录是作为用户,当他们到达FE1表单时,我希望他们只是能够阅读表单,而不是进行任何更改。我一直试图使用的代码如下:
Private Sub Form_Open()
If Forms!frmLogin!cboUser.Column(2) = 2 Then
Me.AllowEdits = False
Me.AllowAdditions = False
Me.AllowDeletes = False
Else
Me.AllowEdits = True
Me.AllowAdditions = True
Me.AllowDeletes = True
End If
End Sub
但我一直收到错误:
表达式On Open,您输入了事件属性设置 产生了以下错误:程序声明没有 匹配具有相同名称的事件或程序的描述。
*表达式可能不会产生宏的名称,用户定义函数的名称或[事件过程]。 *评估函数,事件或宏时可能存在错误。
我可能已经看了太久了,但是我无法弄清楚我哪里出错了??
答案 0 :(得分:1)
您的Form_Open
程序签名错误,错过了Cancel
参数。
必须是:
Private Sub Form_Open(Cancel As Integer)
不要手动编写事件程序,让Access创建它们。
修改强>
我建议你完全删除Form_Open
子。然后让Access从属性表创建它。
您可以使用如下变量来简化代码:
Private Sub Form_Open(Cancel As Integer)
Dim AllowWriting As Boolean
AllowWriting = Not (Forms!frmLogin!cboUser.Column(2) = 2)
Me.AllowEdits = AllowWriting
Me.AllowAdditions = AllowWriting
Me.AllowDeletes = AllowWriting
End Sub
使用RecordsetType Property: 或更短
Private Sub Form_Open(Cancel As Integer)
If Forms!frmLogin!cboUser.Column(2) = 2 Then
Me.RecordsetType = 2 ' Snapshot = read-only
Else
Me.RecordsetType = 0 ' Dynaset = read-write
End If
End Sub