VBA的RecordSet功能需要太多的时间用总记录更新记录

问题描述 投票:0回答:1

我有一个桌子和一个查询。两者具有相同的数据字段,但表的列名等于查询的行名称。我使用下面的代码成功查询的行数据更新表,但它需要太多的时间来更新,因为表中的50多列名为每个员工 -

Set rst1 = CurrentDb.OpenRecordset("SELECT * FROM tblPayRollDataTEMP")
Set rst2 = CurrentDb.OpenRecordset("SELECT * FROM qryEmpVerifySalary ")

Do Until rst1.EOF
    rst2.MoveFirst
    Do Until rst2.EOF                  
        For l = 0 To rst1.Fields.count - 1
            If rst1!EmpID = rst2!EmpID And rst1.Fields(l).Name = rst2!Head And rst1!PayBillID = TempVars!BillID Then
                With rst1
                    rst1.Edit
                    rst1.Fields(l).Value = rst2!Amount
                    rst1!totDeductions = DSum("Amount", "qryEmpVerifySalary", "[PayHeadType] = 'Deductions' AND [EmpID] = " & rst2!EmpID & "") + DLookup("NPS", "qryEmpPayEarning", "[EmpID] = " & rst2!EmpID & "")
                    rst1!totRecoveries = DSum("Amount", "qryEmpVerifySalary", "[PayHeadType] = 'Recoveries' AND [EmpID] = " & rst2!EmpID & "")
                    rst1!NetPayable = rst1!totEarnings - (Nz(rst1!totDeductions, 0) + Nz(rst1!totRecoveries, 0))
                    rst1.Update
                End With

            End If

        Next
        rst2.MoveNext
    Loop
    rst1.MoveNext
Loop

Set rst1 = Nothing
Set rst2 = Nothing

如何提高代码的性能?

access-vba
1个回答
1
投票

您应该使用查询来更新你的记录。这将是最快的解决方案。通常一个将匹配EmpID和拖放字段到更新查询或使用表达式。如果你有组之前或其他复杂的东西更querys(2或3)拆呢。它不事关你,因为到最后你只需要执行一个更新查询。

为了您的代码,你可以更换domainaggregate功能。 DLookup()DSum(),等等......这些都是最差的表现。简单的SELECT语句的运行速度比DLookup()方式更快。这里有一些替代品:

Function DCount(Expression As String, Domain As String, Optional Criteria) As Variant

  Dim strSQL As String

  strSQL = "SELECT COUNT(" & Expression & ") FROM " & Domain

  'Other Replacements:
  'DLookup: strSQL = "SELECT " & Expression & " FROM " & Domain
  'DMax: strSQL = "SELECT MAX(" & Expression & ") FROM " & Domain
  'DMin: strSQL = "SELECT SUM(" & Expression & ") FROM " & Domain
  'DFirst: strSQL = "SELECT FIRST(" & Expression & ") FROM " & Domain
  'DLast: strSQL = "SELECT LAST(" & Expression & ") FROM " & Domain
  'DSum: strSQL = "SELECT SUM(" & Expression & ") FROM " & Domain
  'DAvg: strSQL = "SELECT AVG(" & Expression & ") FROM " & Domain

  If Not IsMissing(Criteria) Then strSQL = strSQL & " WHERE " & Criteria
  DCount = DBEngine(0)(0).OpenRecordset(strSQL, dbOpenForwardOnly)(0)

End Function
© www.soinside.com 2019 - 2024. All rights reserved.