473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global Variables in Access Query

MSeda
159 Recognized Expert New Member
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
19 17996
MSeda
159 Recognized Expert New Member
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,579 Recognized Expert Moderator MVP
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 Recognized Expert Moderator MVP
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,579 Recognized Expert Moderator MVP
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 Recognized Expert Moderator MVP
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 Recognized Expert New Member
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 Recognized Expert New Member
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,579 Recognized Expert Moderator MVP
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 Recognized Expert Moderator MVP
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 Recognized Expert Top Contributor
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

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

Similar topics

2
5733
by: Dante | last post by:
Hi. I am designing a page and want to use global variables, but I can't quite get them to work. For example I have this: <?php echo $title ?> ....text... <?php require_once ("colophon.php") ?> In the body of my page. In colophon.php I have the line <?php $title="Title" ?> I cannot access the variable though, even when I tried <?php echo GLOBAL ?>
2
9174
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document #1, very application-like, must include the JS file for this library of functions. One reason is that it must call a function to name a "callback" function, and within its own script block, must define the callback function. The callback handles...
6
8217
by: Salvani Langosta | last post by:
In an Access 97 database, I use serveral global variables that hold information about the database, for example: gstrFileServer - holds the server root where the database is stored gstrDataServer - holds the server root where a related data warehouse is stored The values of these variables are set when the database is opened. I want to retrieve one or more of these values in a query, but Access
15
2631
by: Jean | last post by:
Hello, I have the following query that I set up as a test, and it runs fine: SELECT STATUSHISTORIE.* FROM STATUSHISTORIE LEFT JOIN PROBLEM_DE ON STATUSHISTORIE.PROBLEM_ID = PROBLEM_DE.PROBLEMNR WHERE (((STATUSHISTORIE.STATUSDATUM)<#1/1/2005#) AND ((PROBLEM_DE.DATENBEREICH)='SPMO') AND (((Left((.),InStr(.,"-")-2)))='K29')
41
10682
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
10
2666
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a large number of global variables visible inside functions - it's not possible to pass them into the...
9
8665
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
1
29392
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
8
3825
by: rottmanj | last post by:
. In order to teach my self more. I have started to convert some of my cf scheduled tasks to perl applications. One area where things are kind of fuzzy is setting up global variables that can be called from any module with in an application. So far I have created a farily standard module that will act as my global config. This module will store variables that are populated from the database. So that I will not be forced to re-create/query...
20
12767
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The AutoExec macro calls a function "InitApplication" which, in turn, calls a function to set the value of a global string variable
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.