none
must you dim a Public Variable in the General section of a Form Module? Want Variable to be read / write everywhere in the database project Access 2016 win 10 64-bit RRS feed

  • Question

  • Why I am asking this.

    the reference doc's in MSDN seem to be saying NOW I can make a Public variable in the TOP of ANY module, a FORM MODULE or a Class Module or ANY "Standard Module"

    WHY am I asking?  the variable seems to NOT have a scope of the complete database that I need.

    IF the "PUBLIC variable"   MUST STILL be declared in a "Form Module" that would explain the problems I am having getting string values to and from that Public variable.

    It seems in the past you had to "Dim" a "Public" variable in the module of some form.

    bye the way, in one standard module, that only has several Public Sub procedures. Based on what Sub is called, determines what string is loaded into the Public variable, then in a form's module the Public variable is used in making a bigger string.

    I have not found yet a way to tell if a variable is available to only the module it is created in, or every in the current database.

    I am aware of variables good only for procedure in which they are defined, or the limits IF you use Option Private in the module where the variable is declared.

    None of these seem to apply to my issue.  So I am not sure if I am creating the variable wrong or error is in assigning or retrieving a string to the Public variable.

    any info will help.  looking at git hub about "Public" variable was not clear to me about this issue.

    thanks,

    Mark J


    Mark J

    Tuesday, December 4, 2018 2:38 AM

Answers

  • You can certainly declare a public variable in a standard module, and it will then be available in every VBA module in the database, including form modules, report modules, and other class modules.  In order for it to be visible outside of a specific procedure, it must be defined at the module level, outside of any procedure.  In order for it to be visible outside of the module in which it is declared, it must be defined as Public.  So a global variable might be declared like this:

        '---- start of module ----
        Option Compare Database
        Option Explicit

        Public gstrMyGlobalVariable As String

        ' … remainder of module …

    Did you read this discussion of variable scope and visibility:

        https://docs.microsoft.com/en-us/office/vba/language/concepts/getting-started/understanding-scope-and-visibility

    I can't tell what you've tried or where you've gone wrong, but you should be aware that you can "mask" a global variable within a given module by declaring a variable of the same name inside that module.  Similarly, you mask a global or module-level variable inside a procedure by declaring a variable of the same name within that procedure.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by PuzzledByWord Wednesday, December 5, 2018 2:01 AM
    Tuesday, December 4, 2018 4:41 AM
  • You have a good answer from Dirk.

    In the case of a form, and declaring a variable as public? Well, the variable not really global since the variable ONLY exists WHEN the form is open.

    However, if the form was assumed to be open, then declaring the form level (or a class module) variable as public has “some” use, since then if the form is open (or an instance of the class module is active), then other code and programs could in fact reference the public variable with:

    Msgbox  forms!MyFormB!ThePublicVariable.

    Or

    Msgbox  MyClassObjectInstance.ThePublicVarible

    So it not really “much” use to declare a variable in a form or class module as public, but if you need to “use” or “reference” that variable that you declared in a class or a form module from “other” code then declaring the variable as public will allow this.

    So you can thus reference that public variable (form/class) from any other code bit – but you have to prefix the variable with the class or form “instance”.

    At the end of the day, this means that if you looking for what is considered real public variable that any and all code can use at ANY time? Then  you need to declare that public variable in a standard code module. Once you do this, then any code, form etc. can freely use that global variable.

    However, as I pointed out, declaring a variable in a class or form module does allow other code bits to use and consume that variable (but only if you prefix the variable with the given form or instance of the class).

    So there is still a legitimate “use” case for declaring variables as public in a form or class module, but they not be a “global” variable in the sense that the variable will “always” exist and can always be used (such variables only exist when an instance of the form or class exists).

    Regards,

    Albert D. Kallal (Access MVP, 2003-2017)

    Edmonton, Alberta Canada

    • Marked as answer by PuzzledByWord Sunday, July 28, 2019 6:17 PM
    Tuesday, December 4, 2018 6:52 AM

All replies

  • You can certainly declare a public variable in a standard module, and it will then be available in every VBA module in the database, including form modules, report modules, and other class modules.  In order for it to be visible outside of a specific procedure, it must be defined at the module level, outside of any procedure.  In order for it to be visible outside of the module in which it is declared, it must be defined as Public.  So a global variable might be declared like this:

        '---- start of module ----
        Option Compare Database
        Option Explicit

        Public gstrMyGlobalVariable As String

        ' … remainder of module …

    Did you read this discussion of variable scope and visibility:

        https://docs.microsoft.com/en-us/office/vba/language/concepts/getting-started/understanding-scope-and-visibility

    I can't tell what you've tried or where you've gone wrong, but you should be aware that you can "mask" a global variable within a given module by declaring a variable of the same name inside that module.  Similarly, you mask a global or module-level variable inside a procedure by declaring a variable of the same name within that procedure.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by PuzzledByWord Wednesday, December 5, 2018 2:01 AM
    Tuesday, December 4, 2018 4:41 AM
  • You have a good answer from Dirk.

    In the case of a form, and declaring a variable as public? Well, the variable not really global since the variable ONLY exists WHEN the form is open.

    However, if the form was assumed to be open, then declaring the form level (or a class module) variable as public has “some” use, since then if the form is open (or an instance of the class module is active), then other code and programs could in fact reference the public variable with:

    Msgbox  forms!MyFormB!ThePublicVariable.

    Or

    Msgbox  MyClassObjectInstance.ThePublicVarible

    So it not really “much” use to declare a variable in a form or class module as public, but if you need to “use” or “reference” that variable that you declared in a class or a form module from “other” code then declaring the variable as public will allow this.

    So you can thus reference that public variable (form/class) from any other code bit – but you have to prefix the variable with the class or form “instance”.

    At the end of the day, this means that if you looking for what is considered real public variable that any and all code can use at ANY time? Then  you need to declare that public variable in a standard code module. Once you do this, then any code, form etc. can freely use that global variable.

    However, as I pointed out, declaring a variable in a class or form module does allow other code bits to use and consume that variable (but only if you prefix the variable with the given form or instance of the class).

    So there is still a legitimate “use” case for declaring variables as public in a form or class module, but they not be a “global” variable in the sense that the variable will “always” exist and can always be used (such variables only exist when an instance of the form or class exists).

    Regards,

    Albert D. Kallal (Access MVP, 2003-2017)

    Edmonton, Alberta Canada

    • Marked as answer by PuzzledByWord Sunday, July 28, 2019 6:17 PM
    Tuesday, December 4, 2018 6:52 AM