Do not use on any
shared computer
May 17, 2008 10:52am pdt
10.04.2007 at 07:11AM PDT, ID: 22872079
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Global variables in Access VBA - How?
Tags: access, global, vba, variable
*************
* Background *
*************

I need to store and update data in a load of global variables:
- When I load up forms I pre-populate the fields with this data.
- When controls are changed the global fields I update the globals

I have sucessfully defined my variables in a module like this:
    ' Setup globals
    Global gblUser As Integer
    Global gblSampler As Integer
    Global gblProject As Integer
    Global gblExportPath As String

*************
* The problem *
*************

When I hit an error all the globals are lost (reset to default non-initiated values - "" or null or something).

********
* Ideas *
********

- Everytime I call a global I check it's not blank - if it is I reset globals to defaults.
- Save the values of globals in a db table.
- Open a form and set controls in this form to the global values. Grab / update the data straight from here.

***************
* My question!!! *
***************

What is the best way to store "globals" in Access?

Please feel free to add your own ideas - preferably really good ones :oD
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: Angelp1ay
Solution Provided By: jimhorn
Participating Experts: 2
Solution Grade: A
Views: 300
Translate:
Loading Advertisement...
10.04.2007 at 07:13AM PDT, ID: 20014253

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2007 at 07:13AM PDT, ID: 20014258

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2007 at 07:23AM PDT, ID: 20014331

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2007 at 07:28AM PDT, ID: 20014378

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2007 at 07:44AM PDT, ID: 20014543

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2007 at 07:47AM PDT, ID: 20014569

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2007 at 08:16AM PDT, ID: 20014864

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2007 at 08:37AM PDT, ID: 20015063

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2007 at 09:06AM PDT, ID: 20015286

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.05.2007 at 03:12AM PDT, ID: 20020618

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
10.04.2007 at 07:13AM PDT, ID: 20014253

Rank: Genius

>When I hit an error all the globals are lost
Likely, when you have to break execution of code, all global variables are wiped out.

In my apps I have a table VAR_VARIABLES, with an ID and VALUE column, and use it to store variable-like values that I wish to be retained across user sessions.
 
10.04.2007 at 07:13AM PDT, ID: 20014258

Rank: Genius

<When I hit an error all the globals are lost>

how do you handle the error?

do you have error handling routine?

the values of the global variables should still be intact. (AFAIK)
Assisted Solution
 
10.04.2007 at 07:23AM PDT, ID: 20014331
<< In my apps I have a table VAR_VARIABLES, with an ID and VALUE column, and use it to store variable-like values that I wish to be retained across user sessions. >>

I will have multiple users logging in so if I did use the db table method it would have the advantage that I could maintain globals per user if I wished. I'm a little worried about the extra load  caused by continually updating a globals table, and also (although less so) about unnecessarily increasing db size.


<< how do you handle the error? >>
Very badly - at the moment I just let it die!

I'd prefer not to rely on having error code everywhere that catches my globals. Obviously I'll try my best to stop all errors but if I can maintain my globals even when I fail to trap an error that would be better.
 
10.04.2007 at 07:28AM PDT, ID: 20014378

Rank: Genius

>I'm a little worried about the extra load  caused by continually updating a globals table
For the most part I load the ones I care about on startup, and will get / set variables whenever I need to do any form-to-form gymnastics and don't wish to deal with form references.

>I will have multiple users logging in
I also have a VARU_VARIABLES_USER table set up to handle user-specific variables, but I have yet to impliment this.
 
10.04.2007 at 07:44AM PDT, ID: 20014543
So for the db stored globals I would have something like this:

Form:
    Public Sub Form_Load()
        Me.cbo_project.Value = GetGbl('project')
    End Sub

    Public Sub cbo_project_AfterUpdate()
        SetGbl('project', Me.cbo_project.Value)
    End Sub

Global Module:
    Public Function GetGbl(type AS String)
        GetGblProject = CurrentProject.Connection.Execute("SELECT globals.val FROM globals WHERE globals.type = '" & type & "';").Collect(0)
    End Function

    Public Sub SetGbl(type AS String, value As String)
        CurrentProject.Connection.Execute("UPDATE globals SET globals.val = '" & value & "' WHERE globals.type = '" & type & "';")
    End Sub

Looking pretty neat. Could be extended easily enough to have a value per user.

I have two questions!!!!
1) Will there be nasty load on setting the global value - I will be setting globals VERY frequently
2) Does Form_Load fire after an error when the form is recovered? I want fields to be restored after any error.
 
10.04.2007 at 07:47AM PDT, ID: 20014569

Rank: Genius

1) - I have never noticed a significant delay.

2) - No.  One way you can get around this is to throw this line in the top of every sub/function that uses a global

If gsSomeValue Is Nothing Then Call GetAllGlobals
Accepted Solution
 
10.04.2007 at 08:16AM PDT, ID: 20014864
<< 2) - No.  One way you can get around this is to throw this line in the top of every sub/function that uses a global

If gsSomeValue Is Nothing Then Call GetAllGlobals >>

Perhaps something like this:

    Public Sub Form_Activate()
        If Nz(Me.cbo_project.Value,"") = "" Then Me.cbo_project.Value = GetGbl('project')
        If Nz(Me.cbo_user.Value,"") = "" Then Me.cbo_user.Value = GetGbl('user')
    End Sub

Infact, taking this a little further, say I tag up all the controls pre-populated from globals with "gbl". I could then have something like this:

    Public Sub Form_Activate()
        Dim ctl AS Control
        For Each ctl In Me.Controls
            If ctl.Tag = "gbl" Then
                If Nz(ctl.Value,"") = "" Then ctl.Value = GetGbl(special_trim(ctl.Name))
            End If
        Next ctl
    End Sub

    Public Function special_trim(control_name AS String)
        special_trim = Some code to snip the control name down to give a global "type"
    End Function

Got a bit carried away there!!! Enough of that.

****************
Is the db method better than using traditional VBA global variables or using a hidden form populated with the global variables? Are there any advantages / disadvantages for each? Are there any other cunning ideas for globals?
 
10.04.2007 at 08:37AM PDT, ID: 20015063

Rank: Genius

>Is the db method better than using traditional VBA global variables or using a hidden form populated with the global variables?
I highly recommend against the hidden form method, as that requires that that form be open in order to grab the variable.
DB vs. VBA global variables, no real difference, whichever floats your boat will be okay.
Assisted Solution
 
10.04.2007 at 09:06AM PDT, ID: 20015286
Well I'm certainly leaning towards the DB table method at the moment. I think the tag method in the activate trigger above is a good way to be checking that fields are not empty post errors.

The problem with the db method is that it maintains the value between instances of access. Values used in a previous session are not necessarily of use in the following session, but they also won't be "" / null and trip the previous check.

I suppose I could date stamp any updates to the globals and only keep them for a period of time - not great... what length of time is enough, but not too much.

Is there a trigger fired each time Access starts? I could record the most recent Access instance's start time and only recognise globals set since then!!!
 
10.05.2007 at 03:12AM PDT, ID: 20020618
Ok, I'm going to just run something in the startup form. If the user bypasses it then they'll have to deal with the first set of globals being old!

Cheers for the help guys, points on the way :o)
 
 
20080206-EE-VQP-25 / EE_QW_2_20070628