how do i use variables in queries?

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Hi

If I have a value stored in a variable ("vTest" for example), how would I
use that in a query? For example if I wanted to do a simple Select * from
Mytable where Myfield = vTest

Thanks
 
You can't use VBA Variables in a Query since JET (the database engine)
doesn't know Varibles. However, you can write a simple VBA custom function
to get the value of the variable. This is known as a "wrapper" function.

The function should be something like:

Public Function fnGetMyVarValue() As Variant
fnGetMyVarValue = MyVar
End Function

JET recognise the wrapper function and will retrieve the value of the
(public) variable MyVar for processing.
 
Sorry to be a bit stupid here but....

how would i drop that value into the query from the wrapper?
 
Dave said:
Sorry to be a bit stupid here but....

how would i drop that value into the query from the wrapper?

In the same way you can use a built in function like Format() or Date() you can
use a custom function MyFunction() directly in the query.

Assuming that you tried to use the variable and it didn't work, just put the
function in the same place you tried the variable.
 

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

Back
Top