Run Select query from STRING

memaxt

Registered User.
Local time
Today, 11:39
Joined
Mar 12, 2013
Messages
62
Hi there,

I would like to run a select query from a string. basically I want to store the SQL statements in a texbox and click a button to run the code.

Textbox contents =

SELECT CurrentVersion.AreaTeam, CurrentVersion.Version, CurrentVersion.Selected
FROM CurrentVersion
GROUP BY CurrentVersion.AreaTeam, CurrentVersion.Version, CurrentVersion.Selected
HAVING (((CurrentVersion.Selected)=True));


I want the button to execute the code above to create the select query.

I've tried:

Dim strSQL As String

StrSQL = me.txtString

docmd.RunSQL StrSQL


but I get the following error: A RunSQL action requires an argument consiting of an SQL Statement.

is this possible to do for select queries?
 
Hi Plog,

Many thanks for you reply,

I do indeed mean run, is it possible to run the query from statement I have stored in my textbox?
 
I didn't ask if you meant run, I asked what do you meant by 'run'. What do you hope will occur on your computer screen with that SQL?
 
Ahh my apologies,

I would like it to execute the select statement stored in the textbox field and display the query results.
 
Thank you,

the part where it says:

qdfTemp = .CreateQueryDef("", _
"SELECT * FROM Employees")

I changed it to

qdfTemp = me.txtstring

but to no joy
 
Find out what me.txtstring actually contains. Then paste that into an actual query and see if it runs. Sounds like you have bad SQL in that variable.
 
I would have used recordsets since I didn't know that other methode...
Most of the time i just make an sSQL string with the entire correct query
Then use
Code:
Set rs_Records = CurrentDb.OpenRecordset(sSQL)
It may be a wrong practice but it works for me.
 

Users who are viewing this thread

Back
Top Bottom