1

这是我第一次尝试创建登录表单。我已经阅读了一些关于它的论坛并自己尝试过。但是,我在尝试表单时遇到了错误。

“运行时错误‘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
4

1 回答 1

1

如果tblUser.LanID是文本数据类型,Me.txtLanID请用引号括起来以避免DLookup()表达式出错。(您不需要.Value那里,因为它是默认属性。)

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

如果这不是解释,请DLookup()在“立即”窗口中测试您的表达式(使用Ctrl+去那里g),看看它是否引发错误或返回什么值。

请注意,当您DLookup()在“立即”窗口中进行测试时,请使用 的当前值Me.txtLanID。例如,如果Me.txtLanID包含文本“ foo ”,则像这样测试它...

? DLookup("Password", "tblUser", "[LanID]='foo'")
于 2013-01-24T07:58:12.670 回答