Run Select query from STRING (1 Viewer)

memaxt

Registered User.
Local time
Today, 08:33
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?
 

memaxt

Registered User.
Local time
Today, 08:33
Joined
Mar 12, 2013
Messages
62
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?
 

plog

Banishment Pending
Local time
Today, 10:33
Joined
May 11, 2011
Messages
11,638
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?
 

memaxt

Registered User.
Local time
Today, 08:33
Joined
Mar 12, 2013
Messages
62
Ahh my apologies,

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

memaxt

Registered User.
Local time
Today, 08:33
Joined
Mar 12, 2013
Messages
62
Thank you,

the part where it says:

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

I changed it to

qdfTemp = me.txtstring

but to no joy
 

plog

Banishment Pending
Local time
Today, 10:33
Joined
May 11, 2011
Messages
11,638
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.
 

Grumm

Registered User.
Local time
Today, 17:33
Joined
Oct 9, 2015
Messages
395
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

Top Bottom