ms访问查询来创建一个新的登录id,每次我都希望通过用户详细信息表单添加一个新用户



我有一个Login表,其中包含用户信息,如登录id、用户名、密码和访问级别。因此,我创建了一个名为userdetailsform的表单,可以在其中向数据库中添加新用户。

每次打开用户详细信息表单时,都会通过运行查询生成一个新的id。

这是我在查询字段中键入的内容,以生成一个新的id,

new_login_ID: "LI" & Right([Login_ID],2)+1

问题是,在我的登录表单中,我尝试使用以下代码登录:

Private Sub LoginBtn_Click()
'Check to see if data is entered into the UserName combo box
Dim lngMyEmpID As Long
If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Sub
End If
lngMyEmpID = Me.cboEmployee.Value
'Check to see if data is entered into the password box
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'Check value of password in tblAdmins to see if this matches value chosen in combo box
If Me.txtPassword.Value <> DLookup("Password", "tbl_login", "[Login_ID]=" & lngMyEmpID) Then
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
Me.txtPassword = Null
intLogonAttempts = intLogonAttempts + 1
'If User Enters incorrect password 3 times database will shutdown
If intLogonAttempts >= 3 Then
MsgBox "You do not have access to this database. Please contact your system administrator.", vbCritical, "Restricted Access!"
Application.Quit
End If
Else
Me.txtPassword = Null
'Open correct form
Dim strAccessLevel As String
strAccessLevel = DLookup("[Access]", "tbl_login", "[Login_ID]=" & lngMyEmpID)
If strAccessLevel = "Admin" Then
MsgBox "Welcome " & DLookup("Username", "tbl_login", "Login_ID=" & lngMyEmpID)
DoCmd.Close
DoCmd.OpenForm "frm_Admin"
ElseIf strAccessLevel = "Manager" Then
'MsgBox "Welcome " & DLookup("Username", "tbl_login", "Login_ID")
MsgBox "Welcome " & DLookup("Username", "tbl_login", "Login_ID=" & lngMyEmpID)
DoCmd.Close
DoCmd.OpenForm "frm_main_menu"
End If
End If

End Sub

我得到一个运行时错误,说"不匹配",当我调试时,它指向我的登录代码的这一行——lngMyEmpID = Me.cboEmployee.Value

我不知道该怎么解决,有人能解释一下我哪里出了问题,以及我该怎么解决这个问题吗。。提前感谢:)

您在注释中写的这一行应该更正为以下内容(因为lngMyEmpID现在是一个字符串):

If Me.txtPassword.Value <> DLookup("Password", "tbl_login", "[Login_ID]='" & lngMyEmpID & "'")

以这种方式使用DLookup和ID的每一行也是如此。

最新更新