Passing a variable to a query

jmt90404

Registered User.
Local time
Today, 01:48
Joined
Mar 24, 2018
Messages
29
I’m trying to pass a variable in VBA to a query in order to create a temporary table. How do I reference the variable in the query?
 
You can't use a variable in a query.
However you can use a function whose value is based on the variable to filter your query
 
How would I do that? I don’t actually want to filter the results but actually use the variable in an append query. This is all still pretty new to me.
 
Say you have a public variable called strSelection, then create a function in a standard module as follows:

Code:
Function GetSelectiona()
   GetSelection=strSelection
End Function

Then, in your query, use GetSelection() in the criteria row for the field to be filtered
OR use GetSelection() as a field value according to what you need
 
Last edited:
Alternately you could just convert the query to sql and reference the variable that way or even use qry def to write the query if you needed to have the actual query for some reason.

Sent from my SM-G950U using Tapatalk
 
Good point.
Tempvars can indeed be referenced in queries
I never use them and tend to forget that.
 

Users who are viewing this thread

Back
Top Bottom