Passing a variable to a query (1 Viewer)

jmt90404

Registered User.
Local time
Today, 16:02
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?
 

isladogs

MVP / VIP
Local time
Today, 20:02
Joined
Jan 14, 2017
Messages
18,186
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
 

jmt90404

Registered User.
Local time
Today, 16:02
Joined
Mar 24, 2018
Messages
29
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.
 

isladogs

MVP / VIP
Local time
Today, 20:02
Joined
Jan 14, 2017
Messages
18,186
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:

1268

Registered User.
Local time
Today, 15:02
Joined
Oct 11, 2012
Messages
44
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
 

isladogs

MVP / VIP
Local time
Today, 20:02
Joined
Jan 14, 2017
Messages
18,186
Good point.
Tempvars can indeed be referenced in queries
I never use them and tend to forget that.
 

Users who are viewing this thread

Top Bottom