How to run query by using string variable?

  • Thread starter Thread starter hkgary33 via AccessMonster.com
  • Start date Start date
H

hkgary33 via AccessMonster.com

Dear all,
In one of my form’s button click event procedure, I’ve constructed a string
for query, for example, the string variable stores: “SELECT * FROM tblInfo
WHERE ….ORDER BY… ”
Now I would like to run this query in this click event, so what commands can
I use to feed in this query string and then run the query?
Thanks a lot!!!

Gary
 
Gary,

DoCmd.RunSQL YourSQLStringHere

where 'YourSQLStringHere' is the string variable you mentioned.

Hope this helps,

Sam
 
Since you are constructing a Select qeury, the RunSQL or Execute methods
will not work. The OpenQuery method will not work with an SQL string. It has
to have the name of a store query, so, here is how you can do that:

Dim qdfs As QueryDefs
Dim qdf As QueryDef
Dim dbf As Database
Dim strSQL as String

Set dbf = Currentdb
Set qdfs = dbf.QueryDefs

strSQL = “SELECT * FROM tblInfo WHERE ….ORDER BY… â€

Set qdf = dbf.CreateQueryDef("_Temp", strSQL)

DoCmd.OpenQuery qdf.name

qdfs.Delete qdf.Name
 
Back
Top