Results 1 to 7 of 7
  1. #1
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111

    Passing global variables question

    Routine in my form:



    Code:
    Private Sub cmdSomeClick()
    
        Dim theform As String
        
        theform = Me.Name  'grabs the name of the active form 
        
        SomeModule.SomeRoutine (theform)
    
    End Sub

    Code in SomeModule

    Code:
    Sub SomeRoutine (theform)
    
        Forms(theform).label.Caption = "A name" 'just an example
    
    End Sub
    I have code like above throughout my database. The top two lines in the cmdSomeClick() routine never changes but the third one's module name or routine name may change but the variable "theform" will always be referenced as Me.Name. I know I can declare theform variable globally by putting it before the routine or functions such as

    Code:
    Public theform As String

    Is there a way I can do something similar with the line?:

    Code:
    theform = Me.Name
    so that in my code when I call the routine I only need to enter the third line and avoid repeating the top two lines everythime I want to call this routine or function from whatever form I am using? i.e. Do something like below:

    New Routine in my form:

    Code:
    Private Sub cmdSomeClick()
      
        SomeModule.SomeRoutine (theform)
    
    End Sub
    New Code in SomeModule

    Code:
    Public theform As String
    Public theform = Me.Name
    
    Sub SomeRoutine (theform)
    
        Forms(theform).label.Caption = "A name" 'just an example
    
    End Sub
    Access 2007

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    AFAIK, Access VBA does not allow variables to be set in headers, only within procedures. Constants can be set in header.

    A variable declared in a general module header can be referenced and set by any procedure in any module, including form and report modules.

    A variable declared in a form or report module makes that variable available to any procedure in that module.

    Set the variable in some form event that runs when the form opens (like Open or Load) then just reference the variable.

    However, be aware that interruption of code execution will clear the variable. This is why I prefer to set a textbox on form with the value then refer to the textbox. Often makes debugging easier.

    An alternative may be TempVars. I've never used them but I didn't know about them when I first wrote code. TempVars do not lose value if code interrupts.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    You dont pass global variables....they are global. Accessable anywhere , anytime.

    If you define theform as global, then re-define it elsewhere, you destroy the globalness.
    cmdSomeClick()
    Dim theform As String


    Hence you should label the scope of the variables w the first character to keep track,
    public gvForm 'global
    private mvForm 'modular
    dim vForm 'local

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You probably know this, but for clarity, you don't/can't "call" a procedure prefixing it with the module name. Your example should be
    Code:
    Private Sub cmdSomeClick()
      
        ' this sub is in a standard module
        SomeRoutine (theform)
    
    End Sub

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Ssanfu, that is usually the case. Would not use the form reference to call a procedure in the same module. Would not use general module name in calling a procedure in general module.

    However, a procedure behind a form can be called by another module by prefixing with the form reference.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    Quote Originally Posted by ssanfu View Post
    You probably know this, but for clarity, you don't/can't "call" a procedure prefixing it with the module name. Your example should be
    Code:
    Private Sub cmdSomeClick()
      
        ' this sub is in a standard module
        SomeRoutine (theform)
    
    End Sub
    My code does prefix the procedure with the module name. I have several modules, each named by the general functions performed by the modules (e.g make controls visible might have a module called ModVisibleCtrls). This way I know which module contains the code I am calling? Why would that be the wrong approach?

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I think if procedures are declared as Public, VBA will not allow duplicate names across modules. This is why the module name is not required as prefix but apparently doesn't hurt as I just tested. And it does have the advantage of intellisense popup tips.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Cannot create Global Variables
    By Paul H in forum Programming
    Replies: 3
    Last Post: 05-20-2014, 11:27 AM
  2. Global Variables?
    By futurezach in forum Reports
    Replies: 4
    Last Post: 06-20-2013, 03:45 PM
  3. Replies: 2
    Last Post: 03-29-2013, 12:40 PM
  4. Replies: 2
    Last Post: 12-23-2011, 08:22 AM
  5. Setting global variables
    By Remster in forum Programming
    Replies: 1
    Last Post: 08-24-2011, 08:47 AM

Tags for this Thread

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