Skip to content
Advertisement

SQL statement not appending to temporary table

To export displayed user data from a form to excel I create a temporary table and export that table. Normally this works however, in the case where I need to Query two different tables and export them together it will output nothing, only the table’s structure nothing else.

I have tried manually querying the tables in addition to taking straight from the forms record source, manually doing the queries in the code, and using the standard output.

The code for all other forms is listed like this

The code with two parts looks like this:

It should export both the queries to the temporary table however it only outputs the table structure.

Edit: As requested the original record source for the Form output

Edit 2: The queries that are called by the code

qryOUTAGE_NEW_WOs_PA:

qryOUTAGE_NEW_WOs_PB:

Advertisement

Answer

Essentially, you are attempting to run an action query (make-table) based on parameterized recordset select query. However, when running VBA SQL queries with DoCmd.RunSQL, your parameters are never evaluated. As a result, your WHERE clause:

receives a NULL parameter value and does not evaluate to TRUE and hence no records are returned.

There are several solutions you can implement to evaluate parameters in a parameterized query.

  • Access’ DoCmd.OpenQuery

    Save your make-table query as its own stored query and call the action. Note: there is no need to close action queries:

    This GUI run method will locate the control [Forms]![frmOutputPickering]![ListSelectedSystem] across open forms and bind its value accordingly. If form is not opened, query returns no records.

  • Access’ QueryDefs

    Save your make-table query at its own stored query and in code, initialize a querydef and evaluate the parameter literally.

  • Excel’s CopyFromRecordset

    Even better, bypass any need of temp tables and use querydefs and recordsets to output data to open workbook using an Excel application object. Below shows how to export one parameterized query.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement