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
19 17993 NeoPa 32,579
Recognized Expert Moderator MVP
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.
MSeda 159
Recognized Expert New Member
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
NeoPa 32,579
Recognized Expert Moderator MVP
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).
PEB 1,418
Recognized Expert Top Contributor
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...
:)
MSeda 159
Recognized Expert New Member
Thanks for the Input. I guess I'll stick with the case select function.
NeoPa 32,579
Recognized Expert Moderator MVP
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 ;) ). - 'RptParms sets and returns a set of parameters required by a report.
-
Public Function RptParms(intSetGet As Integer, _
-
ParamArray avarParams() As Variant) As Variant
-
Static avarParms() As Variant
-
Dim intIdx As Integer
-
-
RptParms = 0
-
If intSetGet = 0 Then
-
intSetGet = UBound(avarParams) + 1 - LBound(avarParams)
-
If intSetGet < 1 Then
-
ReDim avarParms(1 To 1)
-
avarParms(1) = "Error"
-
Exit Function
-
End If
-
ReDim avarParms(1 To intSetGet)
-
For intIdx = 1 To intSetGet
-
avarParms(intIdx) = avarParams(intIdx - 1)
-
Next intIdx
-
Else
-
'If outside bounds then it drops through and is set to "Error"
-
On Error Resume Next
-
If avarParms(intSetGet) = "Error" Then
-
RptParms = "Error" 'On Error
-
Else
-
RptParms = avarParms(intSetGet)
-
End If
-
End If
-
End Function
MMcCarthy 14,534
Recognized Expert Moderator MVP
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 -
-
Public Function GloVar(ByVal VarName) As Variant
-
-
GloVar = VarName
-
-
End Function
-
-
MSeda 159
Recognized Expert New Member
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
MMcCarthy 14,534
Recognized Expert Moderator MVP
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 ?>
|
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...
|
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
|
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')
|
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
| |
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...
|
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
|
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...
|
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...
|
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
|
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...
| |
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,...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |