2013-01-24 98 views
1

這是我第一次嘗試創建登錄表單。我已經閱讀了幾個關於它的論壇,並親自嘗試過。不過,我在嘗試表單時遇到了錯誤。Microsoft Access 2003 VBA - 登錄表格

「運行時錯誤'2001':您取消了以前的操作。」

這是我的代碼!突出顯示的錯誤是DLOOKUP語句。當我移動光標到LanID,這似乎是0。(我想這有什麼關係呢?)

Option Compare Database 
Option Explicit 

Private intLoginAttempts As Integer 

Private Sub cmdLogin_Click() 
'Check mandatory fields 
If IsNull(Me.txtLanID) Or Me.txtLanID = "" Then 
    MsgBox "Lan ID is required.", vbOKOnly, "Required Data" 
    Me.txtLanID.SetFocus 
    Exit Sub 
End If 

If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then 
    MsgBox "Password is required.", vbOKOnly, "Required Data" 
    Me.txtPassword.SetFocus 
    Exit Sub 
End If 

'Compare input password and database password 
If Me.txtLanID <> "" Then 
    If Me.txtPassword = DLookup("Password", "tblUser", "[LanID]=" & Me.txtLanID) Then 
     LanID = Me.txtLanID 

     'Close Login Page 
     DoCmd.Close acForm, "frmUserLogin", acSaveNo 

     'Check whether user is an admin 
     If Me.txtAdmin = DLookup("Administrator", "tblUser", "[LanID]=" & Me.txtLanID) Then 
     If Me.txtAdmin = -1 Then 
      DoCmd.OpenForm "frmMenu" 
      Else 
       DoCmd.OpenForm "frmBack" 
     End If 
     End If 

    Else 
     MsgBox "Invalid Password. Try again.", vbOKOnly, "Invalid Entry!" 
     Me.txtPassword.SetFocus 
    End If 
End If 

'If user enters incorrect password 3 times 
intLoginAttempts = intLoginAttempts + 1 
If intLoginAttempts = 3 Then 
    MsgBox "You do not have access to the database. Please contact system administrator.", vbCritical, "Restricted Access!" 
    Application.Quit 
End If 
End Sub 

回答

1

如果tblUser.LanID是文本數據類型,用引號括起來Me.txtLanID以避免錯誤的DLookup()表達式。 (你不需要.Value那裏,因爲它是默認屬性。)

DLookup("Password", "tblUser", "[LanID]='" & Me.txtLanID & "'") 

如果這還說明,在立即窗口中測試DLookup()表達(與去那裏按Ctrl )來查看它是否拋出錯誤或它返回的值。

請注意,在立即窗口中測試DLookup()時,請使用當前值Me.txtLanID。例如,如果Me.txtLanID包含文本「」,測試它像這樣...

? DLookup("Password", "tblUser", "[LanID]='foo'") 
+0

哦...它說變量沒有在這方面尚未創建... – GuessWho

+0

聽起來像是你試圖用立即窗口中的Me.txtLanID。對不起,我應該預料到那個錯誤。查看更新的答案。 – HansUp

+0

哦!謝謝你的耐心!我試過了,密碼出現在DLookup聲明之後。 – GuessWho