473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can I use a 'global variables' table to hold persistent values?

49
This relates to Microsoft Access 2003, file format Access 2000.

I am trying to create VBA code that stores values in a table called 1_GlobalVariables, then more code--tied to a different form--that retrieves these values for further actions. The specific goal at the moment is a command button next to a combobox that will take the user to a detailed 'add new' form and then simply pop the new ID into the combobox when the new record is saved. This sort of thing was requested in one of my other projects here (it was thought unreasonable to have to manually select the new record after just having created it, which is a fair point, as it should be the default) and I would expect to use it all the time.

Here's my first effort as it stands:

Expand|Select|Wrap|Line Numbers
  1.     ' First, note where the new name should be added.
  2.  
  3.     Dim stSQL As String
  4.     Dim stGlobalValue As String
  5.     Dim stGlobalType As String
  6.  
  7.     DoCmd.SetWarnings False
  8.  
  9.     stGlobalValue = "Forms!frm3ViewEditAddProperties"
  10.     stGlobalType = "LocationForm"
  11.  
  12.     stSQL = "UPDATE 1_GlobalVariables "
  13.     stSQL = stSQL & "SET GlobalValue = '" & stGlobalValue & "' "
  14.     stSQL = stSQL & "WHERE GlobalType = '" & stGlobalType & "';"
  15.  
  16.     DoCmd.RunSQL stSQL
  17.  
  18.     stGlobalValue = "cboMgtIndex"
  19.     stGlobalType = "LocationControl"
  20.  
  21.     stSQL = "UPDATE 1_GlobalVariables "
  22.     stSQL = stSQL & "SET GlobalValue = '" & stGlobalValue & "' "
  23.     stSQL = stSQL & "WHERE GlobalType = '" & stGlobalType & "';"
  24.  
  25.     DoCmd.RunSQL stSQL
  26.  
  27.     DoCmd.SetWarnings True
That part works great. My two parameters are placed in the table exactly where I want them, ready to bring in the new value from the second form whenever the user has finished with it, requery the combobox and set the focus there. However, I cannot work out how to use VBA to look up the values I've stored in this global table. I would have guessed that would be simple, but after repeated attempts to research it, I'm still stuck.

If I can get this to work, I'll expand it to include the most recently viewed record for a form, the form last in use, possibly tied to the user, etc, with an eye to facilitating the data entry and management.

Sorry to have to post this as a new question, as I'm sure it's been addressed before, but somehow I'm having trouble finding anything I can use; links to relevant threads would be absolutely brilliant to get me started. I thought I'd seen an approach like this described somewhere, but can't track it down again. And any results for web searches like 'microsoft access global variables' are way over my head at this point. But since information is so hard to find, I wonder if I am attempting a method that doesn't work well with Access.

Am I going about this the right way, and if so, what next?
Feb 25 '10 #1

✓ answered by TheSmileyCoder

Having read your post more careful, I have another suggestion.

First in a Module not bound to a form or report add a public variable for example:
Public strStorehouse as String

Then in your commandbutton do:
Expand|Select|Wrap|Line Numbers
  1. 'Make sure to empty variable
  2. strStoreHouse=""
  3. DoCmd.OpenForm "frm_Test", acNormal, , , , acDialog
  4. me.cmb_Test.requery
  5. me.cmb_Test=strStoreHouse
Then set some code in the OnClose event on your form, for instance
Expand|Select|Wrap|Line Numbers
  1. strStoreHouse=me.ID
Opening the form as acDialog means your code will be paused until the form is closed again.

7 4133
TheSmileyCoder
2,322 Expert Mod 2GB
Im not sure your doing it the best way, but I can tell you how to retrieve data from a table.
Use the Dlookup("myField","myTable","myCrit")

In your example it would probably look like:

Expand|Select|Wrap|Line Numbers
  1. Dlookup("GlobalValue","1_GlobalVariables","GlobalType = '" & stGlobalType & "'")
Remember the criteria expresion should NOT be prefixed by WHERE.
Feb 25 '10 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Having read your post more careful, I have another suggestion.

First in a Module not bound to a form or report add a public variable for example:
Public strStorehouse as String

Then in your commandbutton do:
Expand|Select|Wrap|Line Numbers
  1. 'Make sure to empty variable
  2. strStoreHouse=""
  3. DoCmd.OpenForm "frm_Test", acNormal, , , , acDialog
  4. me.cmb_Test.requery
  5. me.cmb_Test=strStoreHouse
Then set some code in the OnClose event on your form, for instance
Expand|Select|Wrap|Line Numbers
  1. strStoreHouse=me.ID
Opening the form as acDialog means your code will be paused until the form is closed again.
Feb 25 '10 #3
JeremyI
49
Thanks, Smiley--I'll give it a try and let you know.
Feb 26 '10 #4
JeremyI
49
Very well explained, Smiley--I've had a chance to try it out this morning, and it works perfectly. The dialog mode is exactly what I was looking for so the user can't wander off while adding the new entry, and it's just what people are used to seeing. I'll adapt it for my own tables and sprinkle it in everywhere.

I've never worked with unbound modules before, but I have seen them in others' code and not been sure when they were needed. Do I understand correctly that the value of these public variables only lasts until the session ends? If so, I'll see about a table for including anything that might be needed when the application is next used. I feel stupid for not realising that DLookup can be used in VBA.

One more thing, just to make sure I understand the logic: why does the requery come before setting the combobox value?

Thanks again!
Mar 1 '10 #5
missinglinq
3,532 Expert 2GB
That's correct, once the database is closed the global variable value is lost. You also need to be aware that if your app bombs out on an unhandled error the value also will be lost, which is why many developers shun them.

Welcome to Bytes!

Linq ;0)>
Mar 1 '10 #6
TheSmileyCoder
2,322 Expert Mod 2GB
I was working under the assumption that you needed to add a new entry into the table being displayed in the combobox. A combobox is queried when a form is opened, and not requery'ed unless you ask it to.

Since you want the combobox to allready have selected the item you added in your dialog form, you must requery it first (because it doesnt contain the last entry you just made). Once required your telling it to select the new value.
Mar 1 '10 #7
JeremyI
49
Yes, new values are being added to the table by the second form. I thought the need for the requery was probably what you've described, but wanted to make sure.

Appreciate the notes from both of you, Smiley and missinglinq--I'm getting a clearer picture all the time. :-)
Mar 1 '10 #8

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

Similar topics

13
by: Larry L | last post by:
I have a Module that declares several arrays as public, some string, some integers. I then have 2 forms. Form A is the main form, that loads on start-up, and has a command button to open Form B. On...
6
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...
4
by: DerekM | last post by:
I have a database project that I created with several forms and reports. I would like to be able to declare a single title and be able to change the title on all the forms and reports by changing...
17
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...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
3
by: Brent Ritchie | last post by:
Hello all, I just got my first job in the IT field as a junior programmer. Right now I am working on my first assignment. It's not the best assignment in the world, but it's something I have...
9
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
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...
0
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...

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.