Windows 7, 64bit
Office 2010 Professional 32bit version 14.0.6112.5000
I have a button that I click that runs a delete query, then an append query, and then opens a report based on some parameters and the data in the table that the query appends to.
When I first open the database, the whole procedure takes a few seconds, which is acceptable. If I close the report and click the button again, it still takes a few seconds. But if I sort by any field in the report before closing the report, the next time I click the button the append query takes over a minute run. The code for the button is:
Private Sub btnOpenrptLotData_Click()
DoCmd.OpenQuery "qryLotDataConglomerateDELETE"
DoCmd.OpenQuery "qryLotDataConglomerateAPPEND"
DoCmd.OpenReport "rptLotData", acViewReport, , "([qryLotDataConglomerateWithTotals].[FormattedDate] Between #" & VBA.Date & "# And #" & DateAdd("m", -1, VBA.Date) & "#) And ([FirstOfModel] Like ""*" & [VINCodeReportSortByUnitField] & """" & " Or [FirstOfModel] Like """ & [VINCodeReportSortByUnitField] & " *"")"
End Sub
But I don't think the problem resides there, because once the slow-down occurs, I can run the append query directly and it STILL takes over a minute to run. The SQL for the append query is:
INSERT INTO tblLotDataConglomerate ( RecordID, SeqNum, LotNum, VINCode, Model, FormattedDate, FormattedTime, UniqueID, SkippedVIN, RightVINCode7, FirstVINOfLot, LastVINOfLot, Quantity )
SELECT qryLotDataWithUID.RecordID, qryLotDataWithUID.SeqNum, qryLotDataWithUID.LotNum, qryLotDataWithUID.VINCode, qryLotDataWithUID.Model, qryLotDataWithUID.FormattedDate, qryLotDataWithUID.FormattedTime, qryLotDataWithUID.UniqueID, tblSkippedVINSheets.SkippedVIN, Right([VINCode],7) AS RightVINCode7, tblReceivedVINSheets.FirstVINOfLot, tblReceivedVINSheets.LastVINOfLot, tblReceivedVINSheets.Quantity
FROM (qryLotDataWithUID LEFT JOIN tblSkippedVINSheets ON qryLotDataWithUID.VINCode = tblSkippedVINSheets.SkippedVIN) LEFT JOIN tblReceivedVINSheets ON qryLotDataWithUID.UniqueID = tblReceivedVINSheets.UniqueLotID
ORDER BY qryLotDataWithUID.FormattedDate DESC , qryLotDataWithUID.FormattedTime DESC;
The query qryLotDataWithUID (which the append query is mostly based on) takes a little over a second to run both when the append query is running quickly and when it's running slowly.
Once I've Compact/Repair-ed, the problem goes away. Is there anything in the code that could be causing this problem, or does anyone have any ideas for changes or workarounds that might help fix it?