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.txtLanIDDLookup().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 に答える