Can't reference a variable in a report

G

ggj13557

I am trying to reference a variable that was created within a form and use it
as the title of a report. I have tried to make the variable public but still
don't know enough to be able to make the report read the variable.

I have created a small database that provides an example of what I am trying
to do. In the example database you open form1, make your selection (either
color or number) and it opens report1 displaying only the information that
you requested. However, the submit button also contains the variable
"stRptTitle" which I want to appear as the Title for the report. This title
needs to change based on the information selected in form1. Here is the code
as it currently stands.

___________________________________________________
Dim stLinkCriteria As String
Dim stRptTitle As String

If Frame0.Value = 1 Then
stLinkCriteria = "(((Table1.Type)='color'))"
stRptTitle = "This Is The Color Report"
End If

If Frame0.Value = 2 Then
stLinkCriteria = "(((Table1.Type)='number'))"
stRptTitle = "This Is The Number Report"
End If

DoCmd.OpenReport "Report1", acViewPreview, , stLinkCriteria

DoCmd.Close acForm, "Form1"
____________________________________________________

Any help with this problem would be greatly appreciated.
 
B

Brendan Reynolds

It looks like the variable is declared inside the button's Click event
procedure? If so, that won't work - the variable would need to be moved to
the declarations section of the form (immediately after the Option Compare
Database and Option Explicit lines, and before any Sub or Function
declarations). You'd also need to leave the form open when opening the
report. You could then refer to the variable as ...

Forms("YourFormNameHere").stRptTitle

Alternatively, move the declaration of the variable out of the form
altogether, and declare it in a standard module (not a form, report, or
class module). This is known as a 'global' variable. Then you no longer need
to keep the form open, and you can refer to the variable simply by name.

Either way, do declare the variable as Public.
 
O

OfficeDev18 via AccessMonster.com

Just add a bit to your OpenReport statement, as follows:

DoCmd.OpenReport "Report1", acViewPreview, , stLinkCriteria,,stRptTitle

In the report's Open event, have the following code:

Me.ReportTitleVariableName = Me.OpenArgs
 

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