Form Code to append results from query to table

  • Thread starter Thread starter Frank via AccessMonster.com
  • Start date Start date
F

Frank via AccessMonster.com

I have a button on a form that thru code will run a macro which will run a
query. How do I get the results from that query to append to a table, all
through vb code of my form?

Thanks,
Frank
 
Frank via AccessMonster.com said:
I have a button on a form that thru code will run a macro which will run a
query. How do I get the results from that query to append to a table, all
through vb code of my form?

Thanks,
Frank

Seems to me the easiest way would be for you to skip the macro and use the
query directly in an INSERT statement, something like:

strSQL = "INSERT INTO
([field1],[field2],...) SELECT
[field1],[field2],... FROM [query];"
CurrentDb.Execute strSQL, dbFailOnError

Alternately, you'd have to turn the query into an Append or MakeTable query
somehow.

Carl Rapson
 
Here is some code that will do it for you.

Sub RunCode()
Dim qry As QueryDef
Dim strQuery As String

strQuery = strSQL = "INSERT INTO
([field1],[field2],...) SELECT
" & _
"[field1],[field2],... FROM [query];"
Set qry = CurrentDb.CreateQueryDef("", strQuery)
qry.Execute

Set qry = Nothing
End Sub


just know that if you want to run multiple sql statements, the second time
you should just say qry.sql=strquery rather than creating new query objects.

Please let me know if I can provide more assistance.
 
Back
Top