Search code examples
vbams-accesscontrols

Use Access macro to display variable in a label


Writing a macro for an Access form.

Since I know of no way to "trace" the value of a local variable, I want to put a label or textbox on the form that shows the current value of the variable. I tried to use SetProperty:

SetProperty
Control Name: lblMyVariable
Property: Caption
Value: MyVarible

You won't be surprised to learn that what's displayed in the label is the word "MyVariable", not the value thereof.

I also tried =[MyVariable], but that doesn't work either.


Solution

  • Unfortantly, you can't bind a control to a variable.

    However, what you can do is in that form do this:

    Public Function MyCompanyName as string
    
        MyCompanyName = strCompanyName   ' the varible here is strCompanyName
    
    End function.
    

    Now, you can place in the text box, or even label this:

    =MyCompanyName()
    

    So, you can place expressions as a text box or label source, but not actual variables. By wrapping the variable inside of a public function, then you can use that public function name as the source. Of course you have to create a function for each variable you wish to display. And of course scope (global or local to the form) will also be a factor.

    If the variable is to be global in scope, then the function has to be in a standard code module outside of the form. But for variables in the forms code, then a local public function will also work.