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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Display Global Variable On Report?

Status
Not open for further replies.

DBLoser

MIS
Apr 7, 2004
92
US
I have a global variable defined on a form and would like to have a text box on my report = the global variable. I tried inserting the text box and setting it equal to the variable but Access converts this to a field with [] around it and does not work. How can I call reference the variable?
 
Try ="variable" in the control source of the text box.
 
You can't access a global variable as a ControlSource in a report (or in a form, for that matter) because global variables are part of Visual Basic and not really part of Access.

However, what I've done in the past is to create a public function that returns the value of the global variable. For example, if you have a module named basGlobalVariables with the following code:
Code:
Option Compare Database
Option Explicit

Global CurrUser As String

Public Function GetCurrUser() As String
    GetCurrUser = CurrUser
End Function
you can then put =GetCurrUser() as the ControlSource of a control on the report.


[shadeshappy] Cruising the Information Superhighway
[sub] (your mileage may vary)[/sub]
 
I have a global variable defined on a form"
- NOTE - a variable defined with the public keyword at the top of a forms/reports module IS NOT a public variable. You will need to put it into a standard module (in VBE - Insert | Module) to make it public (global).

A form public, is only available as long as the form is open. When it closes, the form public looses the value (out of scope).

wemeier's suggestion will work well with a public variable (declared in a standard module), but if you're going to try to retrieve a form public you'll need to ensure the form is open, then fetch it like a form property, for instance like this:

[tt]Public Function GetCurrUser() As String
GetCurrUser = forms("nameofyourform").CurrUser
End Function[/tt]

wemeier's or this function will need to be in a standard module.

Roy-Vidar
 
Good point, Roy-Vidar. When brucewbannan said "a global variable" I assumed it was in a module since global variables (defined using the GLOBAL keyword) can't be in a form.

[shadeshappy] Cruising the Information Superhighway
[sub] (your mileage may vary)[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top