Shawn V. Wilson
Shawn V. Wilson

Reputation: 1111

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.

Upvotes: 1

Views: 1100

Answers (2)

Andrej Čikoš
Andrej Čikoš

Reputation: 1

Here is an example that will use myVariable to track current record number on form, with navigtion buttons disabled and custom buttons on form are used for navigation. Blocks are built with macro builder for OnClick event.

First You have to declare variable with

SetLocalVar 
 Name: myVariable
 Expression: =[CurrentRecord]

then You can use the variable in next block, label - TestLabel is used on form

SetProperty
 Control Name: TestLabel
 Property: Caption
 Value: =[LocalVars]![myVariable]

Remember that You need to use exact syntax, with brackets and exclamation mark

=[LocalVars]![variableName]

Once the macro is finished, LocalVars are destroyed

Another variable type is temporary variables, defined with

SetTempVar
 Name: myVariable
 Expression: =[CurrentRecord]

then u can call it like this

SetProperty
 Control Name: TestLabel
 Property: Caption
 Value: =[TempVars]![myVariable]

TempVars are global variables and will stay active untill Database is closed, and there is limited number of active TempVars. If You need to release the variables :

RemoveTempVar
 Name: mYVariable

If you wonder why I do actually bother with this instead of just going into VBA scripts, well.. our company policy allows only "trusted publisher" scripts in our network. As I am not developer, just an enthusiast, that likes to dub in automation for Excel and lately Access, I am limited to use of Macro blocks for Access.

Upvotes: 0

Albert D. Kallal
Albert D. Kallal

Reputation: 49039

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.

Upvotes: 1

Related Questions