By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,633 Members | 1,930 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,633 IT Pros & Developers. It's quick & easy.

How to Use a Global Variable in a Report's Format Header Event

100+
P: 124
I have a form that contains parameters for a report. After the user clicks Preview I have code that set's the Where clause for the report's record source. Once the report opens I close the parameter form. However, the user enters values on the parameter form that I want to display on the report (StartDate and EndDate, specifically). But because I close the parameter form (otherwise it'd be in the way of the report) I lose those values and they don't render on the report. So I thought I could store them in a global variable that I can then call in the report's header format event. This is what I have:

Parameter Form:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Public gstrStartDate As String
  3. Public gstrEndDate As String
  4. Option Explicit
  5.  
  6. Private Sub cmdPreviewReport_Click()
  7.  
  8.         gstrStartDate = Me.txtStartDate
  9.         gstrEndDate = Me.txtEndDate
  10.  
  11. ...more code...
  12.  
  13. DoCmd.OpenReport "r_FamStmt", acViewPreview, WhereCondition:=strWhere
  14. DoCmd.Close acForm, "f_ParamFamStmt"
  15.  
  16. End Sub
  17.  
My report's format header event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub FamilyIdGroupHeader_Format(Cancel As Integer, FormatCount As Integer)
  2.  
  3. Me.txtReportingPeriod = "Reporting Period: " & gstrStartDate & " to " & gstrEndDate
  4.  
  5. End Sub
  6.  
The issue is that when I debug my code, it doesn't recognize the global variables gstrStartDate and gstrEndDate in my report's header format code above. Error is "Variable not defined". What I don't understand is that I did define it as global variable in my cmdPreviewReport sub at the top so why isn't it recognizing it?
May 4 '11 #1

✓ answered by dsatino

Yes, but you defined it within the forms module. Put the global variable declartion in your own module and you should be fine.

Share this Question
Share on Google+
1 Reply


dsatino
100+
P: 393
Yes, but you defined it within the forms module. Put the global variable declartion in your own module and you should be fine.
May 5 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.