I am struggling with a performance issue in my vba code.
I have read a lot in the forums and it seems that I should be using stored queries but have no idea how to perform this function without looping through records.
Table Bank Deposits T contains a record for each subscriber for each month
MonthlyFee, InstallFee, & LockboxFee are the current values for that subscriber table Subscriber T
Table Subscriber History Temp T only contains records for months in which there has been a change to one of these values
Need to update the values in Bank Deposits T if there is an entry for that month in Subscriber History Temp T
My code works but the section in red is very slow
Currently around 1400 records to loop through
CODE:
Public Sub depositsupdate()
Dim db As Database
Dim ssr As Recordset, ssw As Recordset, qd As Recordset
Dim strSQL As String
Dim strStartDate As String
Set db = CurrentDb()
Set ssw = db.OpenRecordset("Bank Deposits T", DB_OPEN_TABLE) ssw.MoveFirst
Do Until ssw.EOF
strSQL = "SELECT Max([Subscriber History Temp T].[MonthStart]) AS StartingMonth FROM (SELECT TOP 1 [Subscriber History T].MonthStart FROM [Subscriber History T] ORDER BY [Subscriber History T].MonthStart) AS Period, [Subscriber History Temp T] WHERE [Subscriber History Temp T].MonthStart <= #" & ssw!paydate & "# ;"
Set qd = db.OpenRecordset(strSQL)' Finds [Monthstart] as [StartingMonth] in [Bank Deposits Temp T] with [MonthStart] same date as current record in [Subscriber Statements T].[paydate] or if does not exist finds most recent previous record
If IsNull(qd!StartingMonth) Then GoTo Nextssw
strSQL = SELECT [Subscriber History Temp T].SubID, [Subscriber History Temp T].MonthStart, [Subscriber History Temp T].MonthlyFee, [Subscriber History Temp T].InstallFee, [Subscriber History Temp T].LockboxFee From [Subscriber History Temp T] WHERE [Subscriber History Temp T].SubID = " & ssw!SubID & " AND [Subscriber History Temp T].MonthStart = #" & qd!StartingMonth & "#;"
Set ssr = db.OpenRecordset(strSQL) ' Finds record, if exits, in [Bank Deposits Temp T] with [SubID] matching current [Bank Deposits T] record and [Monthstart] matching [StartingMonth]
If ssr.EOF Then ' Skip if no matching History record
GoTo Nextssw
End If
If ssw!MonthlyFee + ssw!InstallFee + ssw!LockboxFee = ssr!MonthlyFee + ssr!InstallFee + ssr!LockboxFee Then ' Skip if no updates required
GoTo Nextssw
Else ' Update [Subscriber Statements T]
ssw.edit
ssw!MonthlyFee = ssr!MonthlyFee
ssw!InstallFee = ssr!InstallFee
ssw!LockboxFee = ssr!LockboxFee
ssw.Update
End If
Nextssw:
ssw.MoveNext
Loop
End Sub
Thanks
George
I have read a lot in the forums and it seems that I should be using stored queries but have no idea how to perform this function without looping through records.
Table Bank Deposits T contains a record for each subscriber for each month
MonthlyFee, InstallFee, & LockboxFee are the current values for that subscriber table Subscriber T
Table Subscriber History Temp T only contains records for months in which there has been a change to one of these values
Need to update the values in Bank Deposits T if there is an entry for that month in Subscriber History Temp T
My code works but the section in red is very slow
Currently around 1400 records to loop through
CODE:
Public Sub depositsupdate()
Dim db As Database
Dim ssr As Recordset, ssw As Recordset, qd As Recordset
Dim strSQL As String
Dim strStartDate As String
Set db = CurrentDb()
Set ssw = db.OpenRecordset("Bank Deposits T", DB_OPEN_TABLE) ssw.MoveFirst
Do Until ssw.EOF
strSQL = "SELECT Max([Subscriber History Temp T].[MonthStart]) AS StartingMonth FROM (SELECT TOP 1 [Subscriber History T].MonthStart FROM [Subscriber History T] ORDER BY [Subscriber History T].MonthStart) AS Period, [Subscriber History Temp T] WHERE [Subscriber History Temp T].MonthStart <= #" & ssw!paydate & "# ;"
Set qd = db.OpenRecordset(strSQL)' Finds [Monthstart] as [StartingMonth] in [Bank Deposits Temp T] with [MonthStart] same date as current record in [Subscriber Statements T].[paydate] or if does not exist finds most recent previous record
If IsNull(qd!StartingMonth) Then GoTo Nextssw
strSQL = SELECT [Subscriber History Temp T].SubID, [Subscriber History Temp T].MonthStart, [Subscriber History Temp T].MonthlyFee, [Subscriber History Temp T].InstallFee, [Subscriber History Temp T].LockboxFee From [Subscriber History Temp T] WHERE [Subscriber History Temp T].SubID = " & ssw!SubID & " AND [Subscriber History Temp T].MonthStart = #" & qd!StartingMonth & "#;"
Set ssr = db.OpenRecordset(strSQL) ' Finds record, if exits, in [Bank Deposits Temp T] with [SubID] matching current [Bank Deposits T] record and [Monthstart] matching [StartingMonth]
If ssr.EOF Then ' Skip if no matching History record
GoTo Nextssw
End If
If ssw!MonthlyFee + ssw!InstallFee + ssw!LockboxFee = ssr!MonthlyFee + ssr!InstallFee + ssr!LockboxFee Then ' Skip if no updates required
GoTo Nextssw
Else ' Update [Subscriber Statements T]
ssw.edit
ssw!MonthlyFee = ssr!MonthlyFee
ssw!InstallFee = ssr!InstallFee
ssw!LockboxFee = ssr!LockboxFee
ssw.Update
End If
Nextssw:
ssw.MoveNext
Loop
End Sub
Thanks
George
Last edited: