refering to variable in a form.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • burger54
    New Member
    • Jan 2008
    • 12

    refering to variable in a form.

    I have a form that the user enters a financial quarter and year to produce a report. the report is based on a query that uses the quarter and year. seems simple enough. The financial quarter and year are drop down boxes. When the run query button is pushed the form concantinates the strings QueryStartDate and QueryEndDate. How do I refer to these variables in my query expression?

    Public Sub Preview_Report_ Click()
    On Error GoTo Err_Preview_Rep ort_Click

    Dim stDocName As String
    Dim QueryEndDate As String
    Dim QueryStartDate As String

    If Me.Quarter = 1 Then
    QueryStartDate = "01/01/" & Me.Year
    QueryEndDate = "04/01/" & Me.Year
    ElseIf Me.Quarter = 2 Then
    QueryStartDate = "04/01/" & Me.Year
    QueryEndDate = "07/01/" & Me.Year
    ElseIf Me.Quarter = 3 Then
    QueryStartDate = "07/01/" & Me.Year
    QueryEndDate = "09/01/" & Me.Year
    ElseIf Me.Quarter = 4 Then
    QueryStartDate = "09/01/" & Me.Year
    QueryEndDate = "12/31/" & Me.Year
    End If
    stDocName = "QuarterlyTestR eport"
    DoCmd.OpenRepor t stDocName, acPreview

    Thanks in advance
    PS if there is a simpler way to do this I would like to know.
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    You don't reference them in your query expression. You set them as criteria in the OpenReport function.
    [code=vb]
    Public Sub Preview_Report_ Click()
    On Error GoTo Err_Preview_Rep ort_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim QueryEndDate As String
    Dim QueryStartDate As String

    If Me.Quarter = 1 Then
    QueryStartDate = "01/01/" & Me.Year
    QueryEndDate = "04/01/" & Me.Year
    ElseIf Me.Quarter = 2 Then
    QueryStartDate = "04/01/" & Me.Year
    QueryEndDate = "07/01/" & Me.Year
    ElseIf Me.Quarter = 3 Then
    QueryStartDate = "07/01/" & Me.Year
    QueryEndDate = "09/01/" & Me.Year
    ElseIf Me.Quarter = 4 Then
    QueryStartDate = "09/01/" & Me.Year
    QueryEndDate = "12/31/" & Me.Year
    End If
    stDocName = "QuarterlyTestR eport"
    stLinkCriteria = "[DateField] BETWEEN #" & QueryStartDate & "# AND #" & QueryEndDate & "#"

    DoCmd.OpenRepor t stDocName, acPreview, , stLinkCriteria
    [/CODE]

    Comment

    • burger54
      New Member
      • Jan 2008
      • 12

      #3
      Thanks that worked perfectly

      Comment

      Working...