我只是浪费了7个多小时试图解决我在尝试制作登录表时遇到的这个问题,
我有tblUsers:
UserID | UserName | Password | UserType
1 | Admin | password | admin
2 | Mark | password2 | BSS
我写了这段代码:
Option Compare Database
Option Explicit
Dim bCanSafleyClose As Boolean
Private Sub btnLogin_Click()
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("SELECT * From tblUsers WHERE UserName = '" & txtUserName & "' AND Password = '" & txtPassword & "' AND UserType ='" & cmbxUserType & "';")
If rst.RecordCount = 1 Then
bCanSafleyClose = True
DoCmd.Close
If cmbxUserType = "Admin" Then ==> This failed I don't know why
DoCmd.OpenForm "AdminMain"
End If
If cmbxUserType = "NSS" Then
DoCmd.OpenForm "ReportsMain"
End If
Else
MsgBox "Error!"
bCanSafleyClose = False
End If
End Sub
Private Sub Form_Load()
bCanSafleyClose = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Cancel = Not bCanSafleyClose
End Sub
但那是行不通的!
我希望有人可以找出这个问题并帮我解决它:) 谢谢..
答案 0 :(得分:0)
未测试:
Private Sub btnLogin_Click()
Const USR_TYPE_ADMIN As String = "Admin"
Const USR_TYPE_NSS As String = "NSS"
Dim rst As Recordset, ut As String
Set rst = CurrentDb.OpenRecordset("SELECT UserType From tblUsers WHERE UserName = '" & _
txtUserName & "' AND Password = '" & txtPassword & "'")
If Not rst.EOF Then
bCanSafleyClose = True
ut = rst.Fields("UserType").Value
If ut = USR_TYPE_ADMIN Then
DoCmd.Close '<<added
DoCmd.OpenForm "AdminMain"
ElseIf ut = USR_TYPE_NSS Then
DoCmd.Close '<<added
DoCmd.OpenForm "ReportsMain"
Else
'anything else?
End If
Else
MsgBox "Error!"
bCanSafleyClose = False
End If
End Sub
答案 1 :(得分:0)
解决方案:
Option Compare Database
Option Explicit
Private Sub btnLogin_Click()
Dim UserLevel As Integer
If IsNull(Me.txtUserName) Then
MsgBox "Please Enter username", vbInformation, "LoginID required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please Enter Password", vbInformation, "Password required!"
Me.txtPassword.SetFocus
Else
'process the job
If (IsNull(DLookup("[UserName]", "tblUsers", "[UserName] ='" & Me.txtUserName.Value & "' And Password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Incorrect User or Password"
Else
UserLevel = DLookup("SecuirtyLevel", "tblUsers", "[UserName] ='" & Me.txtUserName.Value & "'")
DoCmd.Close
If UserLevel = 1 Then
'MsgBox "LoginID and Password is correct"
DoCmd.OpenForm "AdminMain"
ElseIf UserLevel = 2 Then
DoCmd.OpenForm "ReportsMain"
ElseIf UserLevel = 3 Then
DoCmd.OpenForm "ReportsMain"
Else
DoCmd.Close
End If
End If
End If
End Sub
谢谢Andre和Tim :)