Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set SQL of Query with VB?

Status
Not open for further replies.

Bullsandbears123

Technical User
Feb 12, 2003
291
US
How can I set the SQL for a Query using VB.

I have tried
qryQueryname.recordsource=SQLstring

qryQueryname being the query's name and
SQLString being the SQL statement I want to set it to.

but nothing works.

Thanks
 
If I understand you right SQLString and qryQueryname are both the same thing.
What you want is

SQLString = "SELECT * FROM tblYourTable;"

Me.RecordSource = SQLString

RecordSource is a Form Property

 
Let me clarify.
I have a query I would like to open and run.
The query would be independent of the form.
The form is used just to set the parameters.
For instantance I have to define 2 values in the query. Theses values come from a cbo box list, and these values are dependent on a another cbobox choice.
The query needs these values to run correctly.
So I am trying to set the SQL built using these cbobox values and then trying to set the SQL for the Original query. After the query is set to the SQL I need to run a report from the changed Query. All dependent on the cbo box values.
THanks
 
Dim mysql As QueryDef
Dim db As Database

Set db = CurrentDb()
Set mysql = db.QueryDefs("your query name")
With mysql
.SQL = "Your query def"
End With
Set mysql = Nothing


Randall Vollen
National City Bank Corp.
 
I tried your(hwkranger) code and I recieved a compile error: User-defined type not defined. When setting the
dim mysql as queryDef
dim db as database

Do I need to add a component to use the code. It looks like "querydef" is some sort of function do I need a function added. I am running the code within Access.
THANKS!

 
IT WORKS!
I added the DAO 3.6 component an all is well.

Thanks it really helped a Lot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top