2017-04-09 36 views
-1

我做了兩個MainWindow表格(一個用於管理員和一個用戶)的MS Access數據庫應用程序。在這個數據庫中我創建了一個表,tbl_Users添加用戶登錄密碼爲管理員和用戶。在這張表中,我有一列可以確定用戶是管理員還是普通用戶。如何在VBA中創建登錄代碼?

如果我們有一個管理員和tbl_users(具有特定用戶登陸和每個密碼),當我們打開登錄管理員管理員的形式和類型用戶登陸的用戶密碼用戶MainWindow將會打開並且不會引發消息:「不正確的LoginId或密碼」。問題是什麼?

Option Compare Database 

Private Sub Command1_Click() 
Dim userlevel As Integer 

If IsNull(Me.txtLoginID) Then 
    MsgBox "please insert ID", vbInformation, "LoginID required" 
    Me.txtLoginID.SetFocus 
ElseIf IsNull(Me.txtPassword) Then 
    MsgBox "Please inter Password", vbInformation, "Password required" 
    Me.txtPassword.SetFocus 
Else 
    'process the job 
    If (IsNull(DLookup("UserLogin", "tbl_Users", "UserLogin ='" & Me.txtLoginID.Value & "'"))) Or _ 
    (IsNull(DLookup("Password", "tbl_Users", "Password ='" & Me.txtPassword.Value & "'"))) Then 
    MsgBox "Incorrect LoginId or Password" 
Else 
    userlevel = DLookup("UserSecurity", "tbl_users", "userLogin = '" & Me.txtLoginID.Value & "'") 
    DoCmd.Close 
    If userlevel = 1 Then 
     'msgbox "LoginID and Password correct" 
     DoCmd.OpenForm "MainForm_final_admin" 
    Else 
     DoCmd.OpenForm "MainForm_final_user" 
    End If 
    End If 
End If 

End Sub 

回答

3

您只需要一個電話:

Private Sub Command1_Click() 

    Dim userlevel As Variant 

    If IsNull(Me.txtLoginID) Then 
     MsgBox "please insert ID", vbInformation, "LoginID required" 
     Me.txtLoginID.SetFocus 
    ElseIf IsNull(Me.txtPassword) Then 
     MsgBox "Please inter Password", vbInformation, "Password required" 
     Me.txtPassword.SetFocus 
    Else 
     'process the job 
     userlevel = DLookup("UserSecurity", "tbl_Users", "UserLogin = '" & Me.txtLoginID.Value & "' And [Password] = '" & Me.txtPassword.Value & "'") 

     Select Case Nz(userlevel, -1) 
      Case -1 
       MsgBox "Incorrect LoginId or Password" 
       DoCmd.Close 
      Case 1 
       DoCmd.OpenForm "MainForm_final_admin" 
      Case Else 
       DoCmd.OpenForm "MainForm_final_user" 
     End Select 

    End If 

End Sub 
+0

佔地方編譯器錯誤的 「的情況是空」:期望值=或<>或>或<=或= < – user8344677

+0

我改「案例是空的「到」案例是=空「,但我還有第一個問題。 – user8344677

+0

你說得對,你不能以這種方式檢查_Null_。使用Nz將Null轉換爲一些不存在的用戶級別,如-1,請參閱編輯答案。 – Gustav