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
Carl Rapson - 02 May 2007 23:00 GMT
>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 [table] ([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
hmadyson - 03 May 2007 02:42 GMT
Here is some code that will do it for you.
Sub RunCode()
Dim qry As QueryDef
Dim strQuery As String
strQuery = strSQL = "INSERT INTO [table] ([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.
> 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