VBA to Save Query Results to Temp Table

ChuckRobert

Board Regular
Joined
Feb 26, 2009
Messages
64
I am trying to save the results of a query called qry_RosterBase to a temporary table called tbl_TempForRoster. The query formats data from the Personnel Table for the Roster report. I am hoping that the numerous queries required to generate the Roster report will run faster by referencing tbl_TempForRoster when it opens, instead of qry_RosterBase. The code below is activated with a button click. If I comment out the final line “DoCmd.DeleteObjectacTable, strTable”, I can see that the code does make the temp table, and copy all data from the query to the temp table, but only after clicking the button a second time. If I do not comment out the last line, the temp table is never built. I also tried to get just some of the query by changing “Select * to “Select qry_RosterBase.Name, qry_RosterBase.Assignment….. In this case it would copy it over on the second click if only one field is entered in the select statement, but not with more than one. Suggestions?
Code:
[COLOR=black][FONT=Times New Roman]Dim strSQL As String<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]Dim strTable As String<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]strTable = "tbl_TempForRoster"<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]'Delete the table if it exists<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]DoCmd.DeleteObject acTable, strTable<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]strSQL = "Select * INTO " & strTable & "FROM qry_RosterBase "<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]CurrentDb.Execute strSQL<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]'Do required actions…, then delete temp table<o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]<o:p> </o:p>[/FONT][/COLOR][COLOR=black][FONT=Times New Roman]DoCmd.DeleteObject acTable, strTable[/FONT][/COLOR]
<o:p></o:p>
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Instead of deleting the object and recreating it, why not delete and append the data? Something like...
Code:
 Dim strSQL_Delete As String
 Dim strSQL_Append As String
 Dim strTable As String
 strTable = "tbl_TempForRoster"
 strSQL_Delete = "Delete * FROM " & strTable
 strSQL_Append = "Insert * INTO " & strTable & "FROM qry_RosterBase "
 DoCmd.SetWarnings False
 'Delete the data and refresh it from the query
 CurrentDB.Execute strSQL_Delete
 CurrentDb.Execute strSQL_Append
 DoCmd.SetWarnings True

There are several advantages to this approach, including that you can create indexes on fields that will be searched or filtered, for better performance.
And there is no need to delete the table in order to refresh the data.

Denis
 
Upvote 0
Denis,<o:p></o:p>
Thanks for your feedback! I tried to implement this code, but for some reason it will not process the CurrentDb.Execute strSQL_Append command. Everything to that point works. I am certain the query name is correct, so I do not know why it stops there. <o:p></o:p>
I’m wondering if the database may be corrupt. This query is the foundation for building the Roster report, and the Roster report is taking longer than it should to build, and it does not print. On print, an error message displays "Cannot open any more tables", which I find odd as it is based on only 1 table, but the report does include many queries/sub-reports, DLookups, and about 12 crosstab queries are used to display required data in the report. <o:p></o:p>
Suggestions?
 
Upvote 0
You may need a different syntax for the Append statement. I got lazy. Try ...
INSERT INTO tbl_tmpForRoster ( comma delimited list of fields in here )
SELECT * FROM qry_RosterBase

Denis
 
Upvote 0
Denis,<o:p></o:p>
Thanks again for your guidance on this! <o:p></o:p>
Just to follow-up for those who are working a similar project... The base query [qry_RosterBase] pre-formats as much data as possible to support the Personnel Roster. Numerous underlying queries referenced the base query to generate sub-reports for each possible section and shift. The Personnel Roster contains many embedded sub-reports and was taking a long time to load. <o:p></o:p>
However, when this code transfers the minimum required fields from the base query to a temp table, and I re-directed all sub-queries to that, processing time dropped from 40-55seconds to about 6 seconds. I can live with that. <o:p></o:p>
Here is the code that transfers data from the base query to the temp table…
<o:p> </o:p>
Private Sub TransferQueryToTable_Click()<o:p></o:p>
Dim strSQL_Delete As String<o:p></o:p>
Dim strSQL_Insert As String<o:p></o:p>
Dim strTable As String<o:p></o:p>
strTable = "tbl_TempForRoster"<o:p></o:p>
strSQL_Delete = ""<o:p></o:p>
strSQL_Insert = ""<o:p></o:p>
strSQL_Delete = "Delete * FROM " & strTable<o:p></o:p>
strSQL_Insert = " INSERT INTO tbl_TempForRoster "_<o:p></o:p>
& "SELECT CName ," _<o:p></o:p>
& "RDOs ," _<o:p></o:p>
& "Rank ," _<o:p></o:p>
& "Rank_Order ," _<o:p></o:p>
& "Shift ," _<o:p></o:p>
& "Assignment ," _<o:p></o:p>
& "WpnsQual FROM [qry_RosterBase];"<o:p></o:p>
CurrentDb.Execute strSQL_Delete<o:p></o:p>
CurrentDb.Execute strSQL_Insert<o:p></o:p>
'MsgBox "Query data loaded to temp table."<o:p></o:p>

End Sub<o:p></o:p>
<o:p> </o:p>
Thanks again, Denis!<o:p></o:p>
<o:p></o:p>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,218
Messages
6,123,676
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top