Using VBA to change the criteria in a query?

N

nodoby

How do I use Access VB to change the criteria in an existing query?
I am querying a customer table to display only records for a specific
CustomerCode. In the criteria area I would normally type in the CustomerCode
number and run the query, or "Like [Enter Customer Code]" would ask me when
the query is run. Now I want to run the query from Access VB which is not a
problem, but I want the code to insert the criteria and then run the query.

Any suggestions.
 
J

John Vinson

How do I use Access VB to change the criteria in an existing query?
I am querying a customer table to display only records for a specific
CustomerCode. In the criteria area I would normally type in the CustomerCode
number and run the query, or "Like [Enter Customer Code]" would ask me when
the query is run. Now I want to run the query from Access VB which is not a
problem, but I want the code to insert the criteria and then run the query.

Any suggestions.

Use the Querydef's Execute method:

Dim db As DAO.Database
Dim qd As DAO.Querydef
Set db = CurrentDb
Set qd = db.QueryDefs("qryCustomerCode")

<do something to get a search criterion into a variable
lngCustomerCode>
qd.Parameters(0) = lngCustomerCode
<(1), (2), (3) for additional parameters>
qd.Execute dbFailOnError

John W. Vinson[MVP]
 
N

nodoby

Hmmm!
A little over my head, but I'll have to read up on QueryDefs and their
parameters.

It looks like you can create queries and edit them from code. This going to
be some trial and error.

Thanks for your help, this gives me the opportunity to learn something.


John Vinson said:
How do I use Access VB to change the criteria in an existing query?
I am querying a customer table to display only records for a specific
CustomerCode. In the criteria area I would normally type in the
CustomerCode
number and run the query, or "Like [Enter Customer Code]" would ask me
when
the query is run. Now I want to run the query from Access VB which is not
a
problem, but I want the code to insert the criteria and then run the
query.

Any suggestions.

Use the Querydef's Execute method:

Dim db As DAO.Database
Dim qd As DAO.Querydef
Set db = CurrentDb
Set qd = db.QueryDefs("qryCustomerCode")

<do something to get a search criterion into a variable
lngCustomerCode>
qd.Parameters(0) = lngCustomerCode
<(1), (2), (3) for additional parameters>
qd.Execute dbFailOnError

John W. Vinson[MVP]
 
N

nodoby

Thanks
I got it to work!
I used db.QueryDefs(0).SQL = strSQL

This is very powerful!

Thank-you again.

nodoby said:
Hmmm!
A little over my head, but I'll have to read up on QueryDefs and their
parameters.

It looks like you can create queries and edit them from code. This going
to be some trial and error.

Thanks for your help, this gives me the opportunity to learn something.


John Vinson said:
How do I use Access VB to change the criteria in an existing query?
I am querying a customer table to display only records for a specific
CustomerCode. In the criteria area I would normally type in the
CustomerCode
number and run the query, or "Like [Enter Customer Code]" would ask me
when
the query is run. Now I want to run the query from Access VB which is not
a
problem, but I want the code to insert the criteria and then run the
query.

Any suggestions.

Use the Querydef's Execute method:

Dim db As DAO.Database
Dim qd As DAO.Querydef
Set db = CurrentDb
Set qd = db.QueryDefs("qryCustomerCode")

<do something to get a search criterion into a variable
lngCustomerCode>
qd.Parameters(0) = lngCustomerCode
<(1), (2), (3) for additional parameters>
qd.Execute dbFailOnError

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top