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
哦...它說變量沒有在這方面尚未創建... – GuessWho
聽起來像是你試圖用立即窗口中的Me.txtLanID。對不起,我應該預料到那個錯誤。查看更新的答案。 – HansUp
哦!謝謝你的耐心!我試過了,密碼出現在DLookup聲明之後。 – GuessWho