Using an external variable as a field value

Posted on 2013-01-07
Last Modified: 2013-01-07
I wish to update the values in a column in a field in a table using an update query with the value of a variable called MonthVar which was declared in a module called modFunctions. How would I refer to this variable?
Question by:geeta_m9
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
LVL 75

Accepted Solution

DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 300 total points
ID: 38752766
If you are using Access 2010 (?), I would suggest using TempVars() ... very cool.

Short tutorial by Access MVP Juan Soto

LVL 75
ID: 38752768
Unlike Global variables, you can refer to the TempVar() collection from a Query - like you would a Function call to retrieve your global.


Author Comment

ID: 38752769
Yes, I am using Access 2010.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 38752838
I did what you suggested. So in the Query Design would the variable look like like this:

LVL 75
ID: 38752840
Then TempVars() is the slickest way to go.

Basically ... set your tempvar item (where ever)

TempVars.Add "SomeVarName", <value>

In your query - create an expression (or criteria) that retrieves the TempVar var using

LVL 75
ID: 38752859
Two examples:

FROM Table1
WHERE (((Table1.FIELD3)=TempVars("YourVarName")));

SELECT Table1.FIELD1, Table1.FIELD2, TempVars("YourVarName") AS GetMyVar
FROM Table1;

Remember ... somewhere you have to initialize the TempVar item ... maybe in the On Load event of a Form, etc.

LVL 75
ID: 38752863
Another super cool thing about TempVars.  IF ... an error occurs and a reset occurs - this does NOT hose the TempVar collection - just like if they were in a table.

LVL 29

Assisted Solution

IrogSinta earned 100 total points
ID: 38752882
If the variable, MonthVar, was declared at the top of the module (which would make its scope global), then you could create a function to that can be used to retrieve this value like so:
Function GetMonthVar() As Integer 'Or String or whatever data type...
   GetMonthVar = MonthVar
End Function

Open in new window

Your query then can refer to this value by using

I agree though with MX, if you are using 2007 or newer, TempVars is the way to go.  You would assign your value in your code like so:
TempVars("MonthVar") = 5

Open in new window

Then you would use it in a query this way:
LVL 29

Expert Comment

ID: 38752889
I guess I should have refreshed this page first.  :-)

The other cool thing about TempVars is that you don't have to declare it in a module for it to be available globally.


Author Comment

ID: 38752941
One more thing, how do I initialize my TempVar item?
LVL 75
ID: 38752959

Private Sub Form_Load()
    TempVars.Add ("YourVarName", <SomeValue>)
End Sub

Basically, wherever you would have initialized your Global variable ...


Author Comment

ID: 38753107

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question