Ms access 有没有比DLookup更快的替代方案?

Ms access 有没有比DLookup更快的替代方案?,ms-access,vba,ms-access-2007,Ms Access,Vba,Ms Access 2007,我正在使用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.SetF

我正在使用
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

End Sub

我不认为问题是由于
DLookup
固有的缓慢造成的。相反,问题是代码使用了这么多

根据查询
tableUser
打开一个记录集,并从该记录集中获取所需的值。然后从查询
tableDepartment
中打开第二个记录集,并获取剩余值

Dim db作为DAO.database
将qdf设置为DAO.QueryDef
Dim rs作为DAO.Recordset
Dim strSelect As字符串
strSelect=“选择u.密码、u.部门、u.用户名”&vbCrLf&_
“以u的形式从tableUser开始,其中u.lngEmpID=[which_EmpId];”
Set db=CurrentDb
设置qdf=db.CreateQueryDef(vbNullString,strSelect)
qdf.Parameters(“which_EmpId”)=Me.cmbUserName
Set rs=qdf.OpenRecordset(dbOpenSnapshot)
如果不是卢比,那么
如果是的话![Password]=Me.txtPassword然后
strUserLevel=rs!部门
strUserName=rs!用户名
rs.Close
'从tableDepartment的查询中打开另一个记录集
“为了找回你的胸部?”?????价值观
如果结束
如果结束
在这个简短的示例中,我使用了一个临时的
QueryDef
作为参数化的
SELECT
查询。不过,最好将该SQL保存为命名查询,可能是qryFetchUserData。然后在运行时,不必每次都重新创建查询,只需打开保存的查询即可

Set qdf=db.querydfs(“qryFetchUserData”)
为了获得最佳性能,您应该在
tableUser.lngEmpID
tableDepartment.Department
上添加索引(如果它们尚未编制索引)