2

DLookupテーブル内のフィールドを検索するために使用しています。正しく動作しますが、遅いです。スピードアップするためにできることはありますか?

これが私の既存のコードです:

Private Sub cmdLogin_Click()

strUserLevel = ""

If IsNull(Me.cmbUserName) Or Me.cmbUserName = "" Then
    MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
    Me.cmbUserName.SetFocus
    Exit Sub
End If

If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
        MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
        Me.txtPassword.SetFocus
    Exit Sub
End If

'strUserName = cmbUserName.Value

If Me.txtPassword.Value = DLookup("Password", "tableUser", "[lngEmpID]=" & Me.cmbUserName.Value) Then
    lngMyEmpID = Me.cmbUserName.Value
    strUserLevel = DLookup("Department", "tableUser", "[lngEmpID]=" & Me.cmbUserName.Value)
    strUserName = DLookup("User_Name", "tableUser", "[lngEmpID]=" & Me.cmbUserName.Value)
    boolInventoryMDL = DLookup("Inventory", "tableDepartment", "[Department]=""" & strUserLevel & """")
    boolDispositionMDL = DLookup("Disposition", "tableDepartment", "[Department]=""" & strUserLevel & """")
    boolReviewCloseMDL = DLookup("Review", "tableDepartment", "[Department]=""" & strUserLevel & """")
    boolAdministratorMDL = DLookup("Administrator", "tableDepartment", "[Department]=""" & strUserLevel & """")
    boolUserListMDL = DLookup("UserList", "tableDepartment", "[Department]=""" & strUserLevel & """")
    boolUserLevelMDL = DLookup("UserLevel", "tableDepartment", "[Department]=""" & strUserLevel & """")

    If strUserLevel = "Superuser" Then
        MsgBox "Welcome back Superuser! You can access all the modules here..", vbOKOnly, "Caution"
    Else
        MsgBox "Welcome! Login Success!", vbOKOnly, "Login Page"
    End If
    DoCmd.Close acForm, "frmLogin", acSaveNo
    DoCmd.OpenForm "frmModule"

Else
    MsgBox "Password Invalid.  Please Try Again", vbOKOnly, "Invalid Entry!"
    Me.txtPassword.Value = ""
    Me.txtPassword.SetFocus
End If

サブ終了

4

1 に答える 1

2

問題はの固有の遅さによるものではないと思いますDLookup。むしろ問題は、コードがそれらの多くを使用していることです。

のクエリに基づいて1つのレコードセットを開きtableUser、そのレコードセットから必要な値を取得します。次に、のクエリから2番目のレコードセットを開き、tableDepartment残りの値を取得します。

Dim db As DAO.database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strSelect As String

strSelect = "SELECT u.Password, u.Department, u.User_Name" & vbCrLf & _
    "FROM tableUser AS u WHERE u.lngEmpID = [which_EmpId];"
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strSelect)
qdf.Parameters("which_EmpId") = Me.cmbUserName
Set rs = qdf.OpenRecordset(dbOpenSnapshot)
If Not rs.EOF Then
    If rs![Password] = Me.txtPassword Then
        strUserLevel = rs!Department
        strUserName = rs!User_Name
        rs.Close

        ' open another recordset from a query of tableDepartment
        ' to retrieve your bool????? values

    End If
End If

その省略されたサンプルではQueryDef、​​パラメーター化されたSELECTクエリに一時的なものを使用しました。ただし、そのSQLを名前付きクエリ(おそらくqryFetchUserData )として保存することをお勧めします。次に、実行時に、毎回クエリを再作成する代わりに、保存したクエリを開くだけで済みます。

Set qdf = db.QueryDefs("qryFetchUserData")

最適なパフォーマンスを得るには、インデックスを追加する必要があります。まだインデックスが作成されていない場合は、インデックスを追加する必要がtableUser.lngEmpIDありtableDepartment.Departmentます。

于 2013-03-07T04:03:01.603 に答える