Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2010
    Posts
    5

    Unanswered: VBA Global Variables

    I have a main menu form with 5 buttons. Each buttons defines my Location variable differently. This location will be then be displayed in a text box on several subsequent forms.

    It works fine on one from, but not the other two. I put a MsgBox = Location in Form_Open to see what's going on. The variable is blank on the those two. It is not blank on the one that works. There is no obvious reasons for this. The general flow of the code of all the forms are the same. And no where do I reset the variable.

    What's odd is that I changed the control source of the form that worked to no source (it doesn't need one; it is just a menu). Then the variable would be null when opening the form. OK. So it would seem that maybe your forms need to have a control source to access a global variable? No, because on one form that doesn't work it has a table control source and I can't change that anyway.

    So, basically when does a Global variable lose it's value? Or a form can't see a global variable. All this code is in the same project.

    Any help would be appreciated. Please don't tell me to use OpenArgs. I haven't got a good explanation of that yet and I would like to figure this out first. It should not be this difficult.

  2. #2
    Join Date
    Aug 2010
    Posts
    5
    OK. So I changed Public Location to Private Location at the module level. The private method should make this variable available to everything in the same project with is all I need.

    This time I got more info. The form that was working and could retrieve the variable still worked. Form2 said "invalid null" value for MsgBox Location. And Form3 said "variable not defined" for Location.


    This is weird.

  3. #3
    Join Date
    Aug 2010
    Posts
    5
    OK. One more thing. I put it back to Public Location. Form2 still gives an invalid use of null for MsgBox Location placed in Form_Open. But now Form3 just returns a blank Msg Box for the same MsgBox Location placed in Form_Open)

    How can the exact same code placed at the beginning of Form_Open in two differents produce such differant results?

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    1. Check that Option Explicit is present at the beginning of each declaration section of each module.

    2. Recompile

    3. Instead of using a message box to check the status of the variable use:
    Code:
    Debug.Print MyVariable
    Stop
    and check the value of MyVariable in the immediate windows when the code stops executing.
    Have a nice day!

  5. #5
    Join Date
    Aug 2010
    Posts
    5
    Thanks. The variable is still null when I open the form and it shouldn't be. It's not empty on the one form that is working right. Unless something happens before form_load or form_open, that I don't know about?

    BTW. I created a new blank form and called the variable but it was still null. But when I set the form source to a table then it does work. WTF?.. NO Where does anything say anything about the form source affecting global variables. But then, it does not work on the other forms that do have a form source.

    Forget about. I used the OpenArgs method and it works now. I see why people perfer that way. But I only wanted pretty simple Global variable.

    And now I have like 50 dim statements throughout the project. I don't think I did that right.

    How come it won't let me set my variables in the declarations section. Is that where your suppose to do it? I get errors if I put a dim statement there. So I had to declare them in each procedure.

    That's fine with me if that's what everyone is talking about by writing more code. But if there is a more efficient way please let me know.

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Did you declare the variables using the syntax:
    Code:
    Public MyVariable As VariableType
    Also, don't you re-declare the variable in the declaration section of the class module of the form? See: Variable Scope in VBA for MS Access - Microsoft Access / VBA insights
    Have a nice day!

  7. #7
    Join Date
    Aug 2010
    Posts
    5
    All is fine now. Thanks.

    I renamed my location variable to Glocation. No problems as a Global variable now.

    The error message I was getting were confusing and it wouldn't go into debug more. And I still don't see why if Location is a reserved name that it would work on one form, but not any other. (and there are no duplicate declarations for location). That would make you think there is a problem on those forms.

  8. #8
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I haven't kept up with this or read it all, but are there any objects on your form called Location? Or a bound table with a column called location?
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •