Printing Global Variable

G

Guest

I have a report program that computes and stores values in global variables.
Is there a way to print these global variables to the report form together
with the records that the report program generated from the query.

The records generated by the query printed on the report without any
problems. It is the computed global variables I can't seem to print.

Thanks,
Fred
 
G

Graham Mandeno

Hi Fred

Global variables are available only from VBA code, and so can be referenced
only from there.

Two ways spring to mind:

1. Write some code behind the report (say in the Format event for the report
section) to put the global values into unbound textboxes:
Me.txtVar1 = GlobalVariable1
Me.txtVar2 = GlobalVariable2
... etc

2. Write a function to return a named global variable:

Public Function GlobalValue( strVarName as String ) as variant
Select Case strVarName
Case "GlobalVariable1"
GlobalValue = GlobalVariable1
Case "GlobalVariable2"
GlobalValue = GlobalVariable2
... etc
Case Else
GlobalValue = "Unknown variable name"
End Select
End Function

You can then use the function in the ControlSource of your textbox (or
even in a query, as required):

=GlobalValue("GlobalVariable1")
 
G

Guest

Graham,

When I tried your first example I get an error:

Run-time error '-2147352567(80020009)':
You can't assign a value to this object.

Just to give you a background of the difficulties that I am encountering; I
have a form(frmcalling) that initiates printing a report form(rptform). The
"frmcalling" assigns a value to GlobalVariable1. In the "On Open" event of
"rptform" I assign the GlobalVariable1 to a text box on the "rptform":

Me.txtBox = GlobalVariable1

It is at this point where I get the error message above. Is there something
that I am doing wrong?

FYI: When I debug/trace the "rptForm" the GlobalVariable1 seem to have the
right value. It is after execution of "Me.txtBox = GlobalVariable1" that
MS Access "complains". This seems to be a straight forward syntax.


Thanks,
Fred




Graham Mandeno said:
Hi Fred

Global variables are available only from VBA code, and so can be referenced
only from there.

Two ways spring to mind:

1. Write some code behind the report (say in the Format event for the report
section) to put the global values into unbound textboxes:
Me.txtVar1 = GlobalVariable1
Me.txtVar2 = GlobalVariable2
... etc

2. Write a function to return a named global variable:

Public Function GlobalValue( strVarName as String ) as variant
Select Case strVarName
Case "GlobalVariable1"
GlobalValue = GlobalVariable1
Case "GlobalVariable2"
GlobalValue = GlobalVariable2
... etc
Case Else
GlobalValue = "Unknown variable name"
End Select
End Function

You can then use the function in the ControlSource of your textbox (or
even in a query, as required):

=GlobalValue("GlobalVariable1")

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Fred said:
I have a report program that computes and stores values in global
variables.
Is there a way to print these global variables to the report form together
with the records that the report program generated from the query.

The records generated by the query printed on the report without any
problems. It is the computed global variables I can't seem to print.

Thanks,
Fred
 
G

Guest

Graham,

Ignore my initial response. Your first example works. I put the routine on
the "Format" event. Apparently, the global variable assignment does not work
in the "On Open" event of the form.

Thanks a million for your help....

Thanks,
Fred

Graham Mandeno said:
Hi Fred

Global variables are available only from VBA code, and so can be referenced
only from there.

Two ways spring to mind:

1. Write some code behind the report (say in the Format event for the report
section) to put the global values into unbound textboxes:
Me.txtVar1 = GlobalVariable1
Me.txtVar2 = GlobalVariable2
... etc

2. Write a function to return a named global variable:

Public Function GlobalValue( strVarName as String ) as variant
Select Case strVarName
Case "GlobalVariable1"
GlobalValue = GlobalVariable1
Case "GlobalVariable2"
GlobalValue = GlobalVariable2
... etc
Case Else
GlobalValue = "Unknown variable name"
End Select
End Function

You can then use the function in the ControlSource of your textbox (or
even in a query, as required):

=GlobalValue("GlobalVariable1")

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Fred said:
I have a report program that computes and stores values in global
variables.
Is there a way to print these global variables to the report form together
with the records that the report program generated from the query.

The records generated by the query printed on the report without any
problems. It is the computed global variables I can't seem to print.

Thanks,
Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top