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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bullfrog83
    New Member
    • Apr 2010
    • 124

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

    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:
    Code:
    Option Compare Database
    Public gstrStartDate As String
    Public gstrEndDate As String
    Option Explicit
    
    Private Sub cmdPreviewReport_Click()
    
            gstrStartDate = Me.txtStartDate
            gstrEndDate = Me.txtEndDate
    
    ...more code...
    
    DoCmd.OpenReport "r_FamStmt", acViewPreview, WhereCondition:=strWhere
    DoCmd.Close acForm, "f_ParamFamStmt"
    
    End Sub
    My report's format header event:
    Code:
    Private Sub FamilyIdGroupHeader_Format(Cancel As Integer, FormatCount As Integer)
    
    Me.txtReportingPeriod = "Reporting Period: " & gstrStartDate & " to " & gstrEndDate
    
    End Sub
    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 cmdPreviewRepor t sub at the top so why isn't it recognizing it?
  • dsatino
    Contributor
    • May 2010
    • 393

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

    Comment

    Working...