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. 相反,问题在于代码使用了很多。

根据查询打开一个记录集,tableUser并从该记录集中获取您需要的值。然后从查询中打开第二个记录集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.lngEmpIDtableDepartment.Department如果它们尚未编入索引。

于 2013-03-07T04:03:01.603 回答