Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2003
    Posts
    81

    Unanswered: Display global variable value on forms

    How do I best display the current value of a global variable on a form?

    I intuitively set the control's Control Source = MyPublicVariable but that does not work. Instead I end up creating a function to call the variable's value and then set the control's control source = MyPublicFunction or I run private sub routines to populate unbound controls with the values.

    The value of the public variable is in memory, can't I just show it on the form without using a function? I have forms with many such controls and the forms are loading slow due to many roundtrips looking for variable values.

    I have gotten around this problem before by loading the values of public variables on a form at startup, keep it open while the application is running and calling the values from that form but it's a clumpsy method when the variable's value change often.

    Thanks,
    Bjorn

  2. #2
    Join Date
    Jun 2004
    Location
    Terrapin Nation
    Posts
    205
    on the forms open event:

    Me.YourControlName.Value = YourGlobalVAriable

  3. #3
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    I don't think you can do that for local variables either. It is not about variable being global or local. You should either set control value in code or set control source to a function to show any variable in your code.
    Last edited by ghozy; 09-21-04 at 14:59. Reason: Typo
    ghozy.

  4. #4
    Join Date
    Sep 2004
    Posts
    161
    You must update your control.
    If the name of your global variable is toto, and if you create a control named toto in a form, is not the same thing.
    the name of your control is form.toto and not toto

  5. #5
    Join Date
    Jan 2003
    Posts
    81
    My hope was that there is a way to set the control source = VariableValue but it seems I have to continue to use code or a function.

    Thanks
    Bjorn
    Last edited by Bjorn; 09-21-04 at 15:48.

  6. #6
    Join Date
    Jun 2004
    Location
    Terrapin Nation
    Posts
    205
    You should be able to do that if your Globals are stored in class objects.

  7. #7
    Join Date
    Jan 2003
    Posts
    81
    It does work when the variable is in the class object. Thanks!

  8. #8
    Join Date
    Sep 2004
    Posts
    161
    can you explain ?

  9. #9
    Join Date
    Jun 2004
    Location
    Terrapin Nation
    Posts
    205
    "can you explain ?"










    I will take a shot. Functions in modules are different in that they do not represent and instance of a value. Instead they are means to manipulate passed in values.

    However you create a class instance with the new keyword. therefore you are putting that reference in memory where it can be accesses globaly.

    someone correct me if i am wrong, i am pretty much talking out my arse.

  10. #10
    Join Date
    Jan 2003
    Posts
    81
    Explanation:

    I declare my public variable in the code of the form (not a module) as: Public MyClassVariable As Integer. Put this string right on top of the form's code page under Option Explicit.

    Then I set this variable by some event on the form (like Form_Open) as MyClassVariable = MyModuleVariable

    Then in the control on the SAME form I bind the control's control cource =[MyClassVariable]. I could also use unbound controls and pass public variables in code as Me.control.value = MyModuleVariable but I would not see the function of the unbound control by just looking at it on the form's design view.

    Either way with these two methods I don't have to use functions to get the public variable to show in the form.

    My original problem was how to bind the control's control cource =[MyModuleVariable] directly without a function call but that is not possible.

    Thanks for your help guys!

    Bjorn

  11. #11
    Join Date
    Jan 2003
    Posts
    81
    by the way, the form declared variable is NOT available to other forms.
    Bjorn

  12. #12
    Join Date
    Sep 2004
    Posts
    161
    Thanks guys

Posting Permissions

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