how do i use variables in queries?

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
 
V

Van T. Dinh

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.
 
D

Dave

Sorry to be a bit stupid here but....

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

Rick Brandt

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.
 
Top