I think I am good. I have further reduced my "set Col/Del" calls from
1440+ times (Over 10 Minutes) - 12 times per customer
to
120 times (1-2 Minutes) - 1 time per customer
to
1 time (1 Second) - 1 time per run.
I think I am good now. Just got to be sure your code is always efficient as possible. I have noticed over the last few months that using "Set x = y.openrecordset()" does take some time away from the program.
I'm calling it a day. Tomorrow I will get the report checked out for validity.
Thanks for the suggestions.
-----------------
UPDATE: for those who want to see the new code to compare it and learn
Code:
' NOTE FROM: Developer
' Code has been reworked 3 times to bring down the run time
' from 10+ minutes, to 2 minutes, to now 1 second).
' Logic shouldnt be hard to follow
Dim db As DAO.Database Dim tbl As DAO.Recordset, Cus As DAO.Recordset, Col As DAO.Recordset, Del As DAO.Recordset
Set db = CurrentDb()
db.Execute ("DELETE * from BuyVsPay") ' clear the table. We will rebuild it each time
Set tbl = db.OpenRecordset("SELECT * FROM BuyVsPay")
Set cu = db.OpenRecordset("SELECT [Customer Name],[Customer Number] FROM Customer order by [customer name] asc")
Dim custLookup As String ' holds the customer name so we can search for it. Might need to change to use ID in future
Dim monthNum As Integer ' Counter used to pull in the months in order.
Dim x As Integer ' Counter | How many total reocrds?
Dim cansearch As Boolean ' Used to turn off are SQL lookup.
If Not cu.EOF Then ' MASTER CONDITIONAL -
cu.MoveFirst
cansearch = True ' Turn the switch on just this once
' MASTER LOOP:
Do While Not cu.EOF
If IsNull(cu("Customer Name")) Then ' This incase the blank record shows up again in the future
custLookup = "XX"
Else
custLookup = cu("Customer Name")
custLookup = Trim(custLookup) ' Sometimes they will out spaces at the end of the names. -.-
End If
If cansearch Then ' only search the first time. This allows the report to be made in just 1 second rather than minutes.
Set Col = db.OpenRecordset( _
"SELECT [Custom],[Month], [sumofcollection in usd],[sumofcollection in rmb] FROM MonthTotalsCollection " & _
"ORDER BY [Custom],[Month] ASC") ' select total collection records
Set Del = db.OpenRecordset( _
"SELECT [Customer],[Month],[SumOfinclude VA Extended Value],[sumofBilled USD] FROM MonthTotalsDelivery " & _
"ORDER BY [Customer],[Month] ASC") ' select total delivery records
cansearch = False ' switch it off for the rest of run-time
If Not Col.EOF Then Col.MoveFirst
If Not Del.EOF Then Del.MoveFirst
End If
For monthNum = 1 To 12 ' Get 12 months of data, whether they exist or not.
tbl.AddNew ' create the record in the report
If monthNum < 10 Then ' this will add a zero onto month in index if < 10
tbl("index") = custLookup & CStr(Year(Date)) & "0" & CStr(monthNum) 'JohnDoe201508
Else
tbl("index") = custLookup & CStr(Year(Date)) & CStr(monthNum) 'JohnDoe201512
End If
tbl("Customer") = custLookup ' Give it the customer
tbl("CustID") = cu("Customer Number") ' ID for future reference.
tbl("theMonth") = monthNum ' give it the month.
' Main logic for collection data
If Col.EOF Then ' This must come before the others incase we have reached the EOF for this query
tbl("USD_Collect") = 0
tbl("RMB_Collect") = 0
Else ' in this we check is month matches, or if customer matchs.
If Not Col("Month") = monthNum Or Not Col("Custom") = custLookup Or Col.EOF Then
tbl("USD_Collect") = 0
tbl("RMB_Collect") = 0
Else
tbl("USD_Collect") = Nz(Col("SumOfCollection in USD"), 0)
tbl("RMB_Collect") = Nz(Col("SumOfCollection in RMB"), 0)
If monthNum < 13 And Not Col.EOF Then Col.MoveNext
End If
End If
' main logic for delivery data.
If Del.EOF Then
tbl("USD_Bill") = 0
tbl("RMB_Bill") = 0
Else
If Not Del("Month") = monthNum Or Not Del("Customer") = custLookup Or Del.EOF Then
tbl("USD_Bill") = 0
tbl("RMB_Bill") = 0
Else
tbl("USD_Bill") = Nz(Del("SumOfBilled USD"), 0)
tbl("RMB_Bill") = Nz(Del("SumOfinclude VA Extended Value"), 0)
If monthNum < 13 And Not Del.EOF Then Del.MoveNext
End If
End If
'''''''''''''''''''''''''''''''''''
tbl.Update ' SAVE THE NEW RECORD WE JUST CREATED
' THAT'S IT. MOVE TO NEXT MONTH! :)
' monthNum = monthNum + 1 ' go to the next month
x = x + 1
Next monthNum ' month <> 12
cu.MoveNext ' move onto the next customer
Loop ' cu EOF
End If ' cu EOF