473,672 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preventing Global variables from being reset

60 New Member
Hi All,

I currently need to store 3 variables to be used as global variables throughout my application. I need to record the username, their employee group and the task they are doing. The username, for example, is entered into a field on a table to indicate who last worked on that particular record. I have created the code below and it seems to work fine except when the application occasionally errors out at which point the global variables lose their values. Is there a way to prevent the global variables from being reset until the database is closed? I've researched online but haven't found what I'm looking for yet. Also, just curious, but in a multi-user environment these global variables must obviously be local to a particular machine so that if two people log in at the same time they both have their own name stored in their own global variable. Is this true? If so, are these stored within the memory of each local machine?

The code below is in my Globals module
Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Compare Database
  3.  
  4. Global g_username As String
  5. Global g_group As String
  6. Global g_task As String
  7. Public Function Init_Globals()
  8. g_username = ""
  9. g_group = ""
  10. g_task = ""
  11. End Function
  12. Public Function get_global(gbl_parm)
  13. Select Case gbl_parm
  14.         Case "g_group"
  15.              get_global = g_group
  16.         Case "g_username"
  17.              get_global = g_username
  18.         Case "g_task"
  19.              get_global = g_task
  20. End Select
  21. End Function
  22.  
  23.  
On my Login Form I ask the user to pick their name, employee group and task they will be performing from a set of 3 combo boxes. I record these values with the click event of a button on the form as follows :

Expand|Select|Wrap|Line Numbers
  1.  
  2. g_username = Me.USERNAME
  3. g_group = Me.Group
  4. g_task = Me.Task_Combo_Box
  5.  
  6.  
Jan 20 '09 #1
14 19529
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi ramprat. Global variable values are inherently volatile if faced with untrapped code errors, as in these circumstances the VBA code is reset and their values are lost. Of course, all VBA code should have appropriate error handlers to prevent such interruptions.

Further, globals are not shared between instances of Access running on different PCs - they are purely local to the current instance as they are set and read within the active PC's memory.

Simplest thing to do is to store the shared values you need to refer to in an Access table instead. Local values such as username could remain global, but if you are writing some values to a reference table it may be as simple to write them all.

-Stewart
Jan 20 '09 #2
DonRayner
489 Recognized Expert Contributor
I could be wrong on this, but I believe that as soon as access encounters an errors without any form of error handling all the global variables get reset. If you want to avoid this issue you are going to have to implement error handling routines in all of your subs.
Jan 20 '09 #3
DonRayner
489 Recognized Expert Contributor
Oops too slow again. I realy have to work on my typing speed. ;)
Jan 20 '09 #4
ramprat
60 New Member
Thanks Stewart.

I thought of that but thought that it might affect performance by always writing to a table. Also, I've already got more tables than I'd like in my database so I was hoping to avoid adding another if possible. Also, being a multi-user environment will that table not get overwritten each time someone logs in? If not, I suppose I could create a table called temp_globals and populate it with the values at login and then I could implement code something to the effect of (I'm not sure if I have the syntax right but this is the general idea every time I need to reference one of the global variables) It's a crude form of error handling in the vein of what Don suggested. Would this work or am I out in left field here?

Thanks
Expand|Select|Wrap|Line Numbers
  1.  
  2. if isnull.get_global("g_username") = false then
  3.  
  4.      Me.USERNAME = get_global("g_username")
  5. else
  6.      Me.USERNAME = [temp_globals]![username]
  7.  
  8. end if
  9.  
  10.  
Jan 20 '09 #5
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi ramprat. I don't recognise the syntax you are using - the isnull function does not use dot notation for instance - so I'm not sure whether what you have written will work or not. I'd suspect not, of you are really trying to use IsNull that way!

You would need to set and clear values from your temporary table using a combination of VBA and SQL approaches. Not difficult, but as I don't know your requirements the way you do I can't advise you how to achieve what you intend here. Suffice to say that you can insert and modify rows for each logged in user as they open and close the database by using appropriate event handlers to do so.

Performance is very, very unlikely to be an issue here - I would not worry about that at all.

-Stewart
Jan 20 '09 #6
ramprat
60 New Member
I guess it would be better to use

if get_global("g_u sername") = "" then

etc


I was hoping to create a table with only one record in it. It would be the record for the current user. I imagine this would then be visible to all users and more importantly would get overwritten each time someone logs in unless instead of a table I wrote the current username to a text file on the local C drive and then used this value whenever the global variable got reset.
Jan 20 '09 #7
Megalog
378 Recognized Expert Contributor
If you're running this as a networked application, with each user having their own front end file, why not just use a local table to store the user globals? Then when the application starts, you delete all records in the table, and then populate the single new record with data retrieved from the login form you use. No need to mess with a text file (that they can delete).
Then, you can either redirect your global procedure to call on the table data, or, do a null check on the global and if it passes as true, refresh the global with the stored table data.
Jan 20 '09 #8
ramprat
60 New Member
Unfortunately it is a single database that everyone accesses at the same time.
Jan 20 '09 #9
Megalog
378 Recognized Expert Contributor
You need to consider splitting your database into Front End/Back End files, and distributing the front end to your users. If you're concerned at all with performance, this would be the first thing I'd do. Also, you're asking for corruption by having multiple users logging into a single standalone database.
Jan 20 '09 #10

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

Similar topics

8
2455
by: Mike Turco | last post by:
This is a strange one. I have an app that needs to know who is using the program but there's no need for security. When the program opens a form comes up with a listbox. The user double-clicks his name, the data is stuffed into a variable that is dimmed in a module, and the user is "logged in". From time to time, though, Access forgets the value! There is no other place in the database where the variable is written. This is happening...
17
5617
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
13
3070
by: Sunil | last post by:
Hi all, I want to know how good or bad it is using global variables i.e advantages and disadvantages of using global variables. Sunil.
8
1573
by: Giorgio | last post by:
Hi all, i have a problem with a website where i use asp 3 and a global.asa in the root. In global.asa i use some aaplication variables which contains strings, such as: Application("str1")="text". Sometimes (even if i dont change the code) application variables becomes null To solve the problem i delete from server my global.asa and upload new one (with same code inside) Provider says their servers are all right
8
1392
by: Phoebe. | last post by:
Hi, Good Day! I need to create a variable that it's value can be carry across within a form. I've created some above the "web form designer generated code" but it seems not working. It reset back to nothing when i click on a button.
9
8646
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
23
2248
by: David Colliver | last post by:
Hi, using c#, 1.1 I know that we are not supposed to use global variables etc. in c# I am having a problem, but not sure how to resolve. I did have another post here, but may have over confused things, so I will start afresh. An example of what I want to do...
112
5425
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
20
12739
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
8486
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
8931
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
7446
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
6238
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...
0
5705
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();...
0
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
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
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
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.