Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem referencing public variable on report 2

Status
Not open for further replies.

ErnieMcCracken

Technical User
Oct 14, 2003
3
US
I have the following variable defined in a module:

Public MyVar as String

On my form, MyVar is set to either "dog" or "cat".

On the report header, I want the value of PublicVar to appear in a text box. So I set the control source of the text box to:

=[PublicVar]

right? --- wrong! This gives me #Error

I also tried:

= [Forms]![Form1]![MyVar]

Still....no dice. Anyone please help! This should not be hard, right?
 
You said your public variable was named MyVar, but you said you are trying to reference it through something called PublicVar. That would not work. You would need to reference it as MyVar.

Personally, I would reference the field on the form instead.

=[Forms]![Form1]![MyVar].text
 
Oops, I was changed the name in the example. That't not the problem. Assume it is MyVar or PublicVar or WhateverVar throughout.

 
The approach that hneal98 is one where you establish a control on the form called MyVar and populate the control with that value. It is a perfectly reasonable approach, but that's not a public variable.

The only way that I know of to get the value of public variable, and that is to write a public wrapper function to return the value, and set the ControlSource to the name of the function.
Code:
=GetMyVar()
and the function would be something like the following:
Code:
Public Function GetMyVar() as String
   GetMyVar = MyVar
End Function
where MyVar is the name of the public variable. For this truly to be a public var, then both the variable declaration, and the function should not be in a form, but in a code module.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
If the text that appears isn't going to change from record to record and doesn't need to shrink in size, it's easier to use a label rather than a textbox.

You can put something resembling the actual text to be displayed in the form design for the label's caption. Then in the Report_Open you can have a line like
Me.LabelName.Caption = MyVar

That way you avoid the overhead of defining and calling a function.
 
Thanks, Fellas! I think both ways would work, but I went with Cajun's method because in the real-world case, I have to concatenate another variable and some other odds & sods. Otherwise, I do think the label method would be the way to go. Thanks again to both of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top