Using global variable within query design criteria (1 Viewer)

Derek

Registered User.
Local time
Yesterday, 21:23
Joined
May 4, 2010
Messages
234
Hi Guys

I am using a global variable called 'GCampId' within code that holds 'Campaign ID' value. Now I want to use this value to filter my access query. I have created a query in design view and when I try to se the critaria of 'CampaignID' field to GCampID and run the query then it gives me an error message.

Can anyone pls help me in this?

Thanks
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:23
Joined
Sep 21, 2011
Messages
14,265
Why not just use the value in the design view?
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 14:23
Joined
Jan 20, 2009
Messages
12,852
One way is to create wrapper function in a Standard Module and use the function in the query criteria.

Another is to pass it as a parameter to the function using VBA.

However CampaignID doesn't sound a likely candidate for a Global variable to me.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:23
Joined
May 7, 2009
Messages
19,233
Yes a function like:

Global GCampID As Variant

Public Function getCampaignID()
getCampaignID= GCampID
End Function



On your query the criteria would be:

getCampaignID()
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 00:23
Joined
Oct 17, 2012
Messages
3,276
You can also reference the control of a form directly.

Another option is to use a TempVar, as, unlike global variables, TempVars can be accessed directly by queries.

However, I agree with Galaxiom: CampaignID doesn't seem like something you'd need saved in a global variable. What you're describing sounds more like something that should either just be passed as a named parameter or referenced directly via Forms!FormName!ControlName.
 

CedarTree

Registered User.
Local time
Today, 00:23
Joined
Mar 2, 2018
Messages
404
Feeding off this post, since my question is similar...

I have a criteria, called "team" and a get function fnTeam() = Team so I can reference in a saved query (where condition = fnTeam()). When Team = TeamA, works great or when Team = TeamB. But I want to also be able to choose all teams, so I set Team = "Like '*'" but in that case I get no returned records. If I created the query in code, I think it would work but trying to just use saved queries. Any suggestions? Thanks!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:23
Joined
May 7, 2009
Messages
19,233
You can modify your critetia. Supposed you want all team, set the global var to "All". Then on your criteria:

Iif(fnTeam()="All", [Team], fnTeam())
 

MarkK

bit cruncher
Local time
Yesterday, 21:23
Joined
Mar 17, 2004
Messages
8,181
I have created a query in design view and when I try to se[t] the crit[e]ria of 'CampaignID' field to GCampID and run the query then it gives me an error message.

Can anyone pls help me in this?
Suggest you show the error description, show the SQL, and show the code.
hth
Mark
 

Users who are viewing this thread

Top Bottom