469,626 Members | 1,529 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Global Variables in Access Query

MSeda
159 Expert 100+
I would like to use a Global variable as a criteria in an access query.
in my first attempt to do so I wrote a Function that looks like this

Public Function GloVar(ByVal VarName) As String

GloVar = VarName

End Function

It Didn't Work.
It evaluates to the text of the variable name not the value of the variable. Additionaly access forces the VarName to be in quotes. If I Type GloVar(JobNo) it automatically changes to GloVar("JobNo").

I read a post from about a year ago that suggested using an If statement for each variable. i.e.

If VarName = "JobNo" Then
GloVar = JobNo

I (as the original poster) was hoping for a more universal function that would retrieve the value of whatever variable was entered without have to introduce an if statement for every variable.

Any help will be appreciated.

Megan
Nov 3 '06 #1
19 17401
NeoPa
32,202 Expert Mod 16PB
Am I right in thinking that you want a function (GloVar) to return the value (contents) of a variable, whose name (in the source code) matches the string passed in VarName?
Essentially this is impossible as the compiler is not running when the code is, so there is no access to the source code.
Strangely, VBA DOES provide a way (Eval() function) whereby you can call a function 'indirectly' in this way, but not to access a variable.

Interesting question.
Nov 3 '06 #2
MSeda
159 Expert 100+
You understand pretty well what I want to do.

Specifically what I want to accomplish is this.

When a job is created via the "Create New Job Form" the global variable JobNo is assigned the Job Number. which is passed to subsequent forms i.e. "Create a purchase order".

I would like to be able to use the variable as criteria in a query. I am attempting to eleminate some of the bulky queries and code in my database and this seems like the simplest solution since the global variable can maintain its value while forms open and close during the job creation process and without the need for flags at the table level.

I am currently using a function that looks like this

Public Function GloVar(ByVal VarName) As String


Select Case VarName

Case "CurEmp"
GloVar = CurEmp

Case "CurEquip"
GloVar = CurEquip

Etc... for each variable

End Select
End Function

This method requires each variable be entered as a case into the function. I was hoping for something a little more universal that could be used with any variable.

I appreciate the input.

Megan
Nov 3 '06 #3
NeoPa
32,202 Expert Mod 16PB
You can do what you're doing already, or alternatively, do a function for each variable.
As I explained in the previous post though, what you'd like to do is not supported in VBA (for the reasons I explained in said post it wouldn't make sense).
Nov 3 '06 #4
PEB
1,418 Expert 1GB
In fact Eval uses the visible objects from the modules in VBA...

And the global variables are visible only in modules but nowhere else..

Thill the functions are visible everywhere..

It's good solution your one using the select case method to show your global variables...

:)
Nov 4 '06 #5
MSeda
159 Expert 100+
Thanks for the Input. I guess I'll stick with the case select function.
Nov 6 '06 #6
NeoPa
32,202 Expert Mod 16PB
This function might prove helpful, although :
1. It works by ordinal (number of position in list) rather than name.
2. I haven't tested that it works independantly of my other routines (thug it passed a manual scan ;) ).

Expand|Select|Wrap|Line Numbers
  1. 'RptParms sets and returns a set of parameters required by a report.
  2. Public Function RptParms(intSetGet As Integer, _
  3.                          ParamArray avarParams() As Variant) As Variant
  4.     Static avarParms() As Variant
  5.     Dim intIdx As Integer
  6.  
  7.     RptParms = 0
  8.     If intSetGet = 0 Then
  9.         intSetGet = UBound(avarParams) + 1 - LBound(avarParams)
  10.         If intSetGet < 1 Then
  11.             ReDim avarParms(1 To 1)
  12.             avarParms(1) = "Error"
  13.             Exit Function
  14.         End If
  15.         ReDim avarParms(1 To intSetGet)
  16.         For intIdx = 1 To intSetGet
  17.             avarParms(intIdx) = avarParams(intIdx - 1)
  18.         Next intIdx
  19.     Else
  20.         'If outside bounds then it drops through and is set to "Error"
  21.         On Error Resume Next
  22.         If avarParms(intSetGet) = "Error" Then
  23.             RptParms = "Error"                  'On Error
  24.         Else
  25.             RptParms = avarParms(intSetGet)
  26.         End If
  27.     End If
  28. End Function
Nov 6 '06 #7
MMcCarthy
14,534 Expert Mod 8TB
I would like to use a Global variable as a criteria in an access query.
in my first attempt to do so I wrote a Function that looks like this

Public Function GloVar(ByVal VarName) As String

GloVar = VarName

End Function

It Didn't Work.
It evaluates to the text of the variable name not the value of the variable. Additionaly access forces the VarName to be in quotes. If I Type GloVar(JobNo) it automatically changes to GloVar("JobNo").

I read a post from about a year ago that suggested using an If statement for each variable. i.e.

If VarName = "JobNo" Then
GloVar = JobNo

I (as the original poster) was hoping for a more universal function that would retrieve the value of whatever variable was entered without have to introduce an if statement for every variable.

Any help will be appreciated.

Megan
Assuming varName is declared as a string

Global VarName As String

You are returning a string using the function you described
Instead declare VarName as a Variant and return a Variant from the function. This will allow for different datatypes.

Global VarName As Variant

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function GloVar(ByVal VarName) As Variant
  3.  
  4. GloVar = VarName
  5.  
  6. End Function
  7.  
  8.  
Nov 7 '06 #8
MSeda
159 Expert 100+
Unfortunately changing to a GloVar to a variant still returns the name of the variable, or whatever text is entered as varname. I'm using the Case Select function for now. But I definately would prefer the simpler solution if you think of anything.
Thanks
Nov 8 '06 #9
MMcCarthy
14,534 Expert Mod 8TB
Unfortunately changing to a GloVar to a variant still returns the name of the variable, or whatever text is entered as varname. I'm using the Case Select function for now. But I definately would prefer the simpler solution if you think of anything.
Thanks
Can you post the query statement in which you are trying to use this function.
Nov 9 '06 #10
MSeda
159 Expert 100+
This query subtracts used inventory from instock inventory:
UPDATE [Inventory Stock Table] SET [Inventory Stock Table].[Inv Part Quan In Stock] = GloVar("StkQn")-GloVar("QnUse")
WHERE ((([Inventory Stock Table].[Inv Stock #])=Glovar("StkID")));

this query creates an Inventory Transmital for a job that was just created:
INSERT INTO [Purchase Order Table] ( [Job#], [PO/IT #], Vendor, [PO Acct], [PO Bus Loc], [PO Parts Ordered by] )
SELECT [Job List Table].[Job #], "IT " & [Job #] AS PIT, "INVENTORY" AS Vendor, 11070 AS Acct, [Job List Table].[Job Business Location], [Job List Table].[Job Initiated By]
FROM [Job List Table]
WHERE ((([Job List Table].[Job #])=GloVar("JobNo")));

here are two examples. I use it mostly in action queries that require I close a form to release the table lock before I can perform a query.
Nov 9 '06 #11
NeoPa
32,202 Expert Mod 16PB
MSeda,

Have you looked at the function I posted earlier?
It doesn't use a variable name, as such, but enables you to set a number of Variants in code then refer to them later by their ordinal (or number in the list they occur).
This might do what you want, but perhaps not in quite as swish a way as desired.
Nov 9 '06 #12
MMcCarthy
14,534 Expert Mod 8TB
I tested this and it worked for me...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function GloVar(ByRef varName As String) As Variant
  3.  
  4.   GloVar = varName
  5.  
  6. End Function
  7.  
  8.  
Nov 10 '06 #13
NeoPa
32,202 Expert Mod 16PB
Does this not return the string that is the name of the variable rather than the contents of the variable?
Nov 10 '06 #14
MMcCarthy
14,534 Expert Mod 8TB
Does this not return the string that is the name of the variable rather than the contents of the variable?
No

The ByRef seems to make it return the value.

Mary
Nov 10 '06 #15
MSeda
159 Expert 100+
I appriciate all of the suggestions. I looked at Neopa's use of ordinals but unfortunately it is a bit over my head programming-wise so I'm still using the case select function since it's on a level I can understand.

As far as the other suggestions they aren't working for me, maybe I'm missing something.

I tried the ByRef that mmcarthy posted but I get the #Name? error.
and as previously posted similar code using ByVal returns a string

The way I test to see if access recognizes the variables is I have a form with text boxes with the control source =GloVar("JobNo") or whatever the variable name is. (I put the variable name in quotes because access automatically brackets it if you don't quote it) I also have combo and text boxes that allow me to set the variables to appropriate values so I can test queries and stuff with out having to use the front end forms to create a new job in entirity.

I saw that Mary also said the ByRef worked for her too.
Right now I have just cut and pasted the code into a module and saved it, that usually works.Is there something I might not be doing or doing differently that is causing this not to work for me?
Nov 10 '06 #16
MSeda
159 Expert 100+
Sorry I made a mistake in my previous post. The byRef function also returns a string, I mistyped the name of the function in my test form.
Nov 10 '06 #17
NeoPa
32,202 Expert Mod 16PB
The function can just be copied into a module in your project.
The way it is used is to set up the variables in the first call where the first parameter = 0 and the others are those that you need to reference later.
Expand|Select|Wrap|Line Numbers
  1. Call RptParms(0, "A", Date(), 36)
If later, in a query, form or report, yo want the date saved you get it by calling
Expand|Select|Wrap|Line Numbers
  1. x=RptParms(2)
The string and number values would be returned with parameters of 1 and 3 respectively.

Does that make it usable?
Nov 10 '06 #18
MMcCarthy
14,534 Expert Mod 8TB
By putting the variabe name in quotes you are passing a string value instead of the variable name. You can pass the variable name as a string deftype but if you put it in quotes the VBA will not recognise it as anything other than a string value. The only way you can use this in a query, if you cannot pass without the quotes, is by creating the query in VBA.

As these are action queries you can do this using runsql in VBA code as follows:

DoCmd.runSQL "UPDATE [Inventory Stock Table] SET " & _
"[Inventory Stock Table].[Inv Part Quan In Stock] = " & _
GloVar(StkQn)-GloVar(QnUse) & _
" WHERE ((([Inventory Stock Table].[Inv Stock #])=" & _
Glovar(StkID) & "));"


I appriciate all of the suggestions. I looked at Neopa's use of ordinals but unfortunately it is a bit over my head programming-wise so I'm still using the case select function since it's on a level I can understand.

As far as the other suggestions they aren't working for me, maybe I'm missing something.

I tried the ByRef that mmcarthy posted but I get the #Name? error.
and as previously posted similar code using ByVal returns a string

The way I test to see if access recognizes the variables is I have a form with text boxes with the control source =GloVar("JobNo") or whatever the variable name is. (I put the variable name in quotes because access automatically brackets it if you don't quote it) I also have combo and text boxes that allow me to set the variables to appropriate values so I can test queries and stuff with out having to use the front end forms to create a new job in entirity.

I saw that Mary also said the ByRef worked for her too.
Right now I have just cut and pasted the code into a module and saved it, that usually works.Is there something I might not be doing or doing differently that is causing this not to work for me?
Nov 10 '06 #19
PEB
1,418 Expert 1GB
Wow it seems genious
ByRef, it can do a great work!

I tested this and it worked for me...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function GloVar(ByRef varName As String) As Variant
  3.  
  4.   GloVar = varName
  5.  
  6. End Function
  7.  
  8.  
Nov 18 '06 #20

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Dante | last post: by
6 posts views Thread by Salvani Langosta | last post: by
41 posts views Thread by Miguel Dias Moura | last post: by
10 posts views Thread by Charles O'Flynn | last post: by
8 posts views Thread by rottmanj | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.