Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

Using an external variable as a field value

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?
0
geeta_m9
Asked:
geeta_m9
  • 6
  • 4
  • 2
2 Solutions
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
If you are using Access 2010 (?), I would suggest using TempVars() ... very cool.

Short tutorial by Access MVP Juan Soto
http://blogs.office.com/b/microsoft-access/archive/2010/09/27/power-tip-maximize-the-user-of-tempvars-in-access-2007-and-2010.aspx

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Unlike Global variables, you can refer to the TempVar() collection from a Query - like you would a Function call to retrieve your global.

mx
0
 
geeta_m9Author Commented:
Yes, I am using Access 2010.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

[TempVars]![MonthVar]
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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

TempVars(SomeVarName)
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Two examples:

SELECT Table1.FIELD1, Table1.FIELD2
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.

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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.

mx
0
 
IrogSintaCommented:
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
=GetMonthVar()


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:
=TempVars!MonthVar
0
 
IrogSintaCommented:
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.

Ron
0
 
geeta_m9Author Commented:
One more thing, how do I initialize my TempVar item?
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Example:

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

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

mx
0
 
geeta_m9Author Commented:
Thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now