Results 1 to 3 of 3
  1. #1
    graffiti is offline Newest Newbie ever
    Windows XP Access 2003
    Join Date
    Nov 2011
    Worthing, south coast of the UK

    Newbie. Global variables. This is my understanding, am I correct?

    Hi all.

    For my own reasons I want a variable (holding a persons name) to be available in all forms. So I need to declare a global variable. Fine. I have read in these forums and on other websites but only get an overview, so some queries remain.

    In my project I create a new module. Naming the module shouldn't matter other than it makes sense. I assume there is no list of pre allocated titles as there are with keywords? Is there a default 'Global' module I should create and use?

    My module looks like this at the moment, full of scraps...

    module name is GlobalStuff.

    Option Compare Database
    'Declare the variables themselves
    Public gblCurrentUser As String
    Public gblUserID As String
    Dim strUser As String
    Option Explicit
    'This was copied over from a web page example. Junk at the moment.
    Private Sub Form_Open(Cancel As Integer)
    ' doing something here
    End Sub
    'This is for the db to get this info at the beginning. It will be (isn't yet) called from the form_open event of the first form.
    Public Sub subGetWindowsUsername()
    strUser = Environ("USERNAME")
    End Sub
    This page has this code...

    The following is the code we use to initialize our VBA global variables:
    Option Compare Database
    'Access global variables definition
    Global GBL_Username As String
    Global GBL_Previous_Tab As String
    Option Explicit
    Questions are... what does the 'global' word do? When I F1 over it I get help refering to "Public Statement". Is it, in this module, better than using Dim?

    I have seen remarks saying Global variables are potential problems, but not telling why they are. What should I look out for?

    I know this stuff is buried in the books but I am digging out the stuff I need to know as I go. Probably the worst way to learn.

    That's enough. Happy Christmas to you all and thanks in advance for any light you can shed on my understanding.


  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    8300' in the Colorado Rocky Mountains
    A variable declared Public in a Standard Module is what I use for "Global" variables and will work. I know that "Global" is a reserved word but cannot find any reference to using it to Dim variables.

  3. #3
    Rainlover's Avatar
    Rainlover is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2009
    Queensland Australia
    Global Variables are only a problem to those who code incorrectly.

    In other words there is nothing wrong with Global Variables if they are used correctly.

    Not sure if this answers your question. Post agin if you need to.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Understanding DATE fomat
    By djclntn in forum Database Design
    Replies: 17
    Last Post: 11-24-2011, 09:50 PM
  2. Setting global variables
    By Remster in forum Programming
    Replies: 1
    Last Post: 08-24-2011, 08:47 AM
  3. Replies: 9
    Last Post: 06-26-2011, 09:14 PM
  4. Understanding afterupdate
    By MAM8433 in forum Access
    Replies: 8
    Last Post: 05-23-2011, 02:05 PM
  5. Understanding SQL Querys
    By jacobbiljo in forum Access
    Replies: 8
    Last Post: 11-17-2009, 05:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Other Forums: Microsoft Office Forums