Microsoft Access 2003 VBA - 登录表单

时间:2013-01-24 07:33:37

标签: ms-access access-vba ms-access-2003

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

“运行时错误'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 个答案:

答案 0 :(得分: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'")