Date syntax error in query expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OzNet
    New Member
    • Aug 2007
    • 31

    #1

    Date syntax error in query expression

    Can someone tell me what I am doing wrong here please?

    I have a form with two unbound text boxes formatted to short date and an OK button.

    My button code is as follows:

    Private Sub cmdOK_Click()

    Dim strFilter As String

    strFilter = "Between #" & Me.txtStartDate .Value & "# And #" & Me.txtEndDate.V alue & "#"

    'Open Report before applying the filter
    DoCmd.OpenRepor t "rptDisbursemen tSummaryReport" , acViewPreview

    With Reports![rptDisbursement SummaryReport]
    .Filter = strFilter
    .FilterOn = True
    End With
    End Sub

    I am getting the following error message:

    Syntax error (missing operator) in query expression '(Between #12/12/2007# And #11/12/2008#)'.

    Many thanks
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    The filtering of the report can be passed by using the WHERE parameter of the DoCmd.OpenRepor t like:
    Code:
    Private Sub cmdOK_Click()
    
    Dim strFilter As String
    
    strFilter = "Between #" & Me.txtStartDate.Value & "# And #" & Me.txtEndDate.Value & "#"
    
    'Open Report before applying the filter
    DoCmd.OpenReport "rptDisbursementSummaryReport", acViewPreview,,strFilter 
    
    End Sub
    Nic;o)

    Comment

    • OzNet
      New Member
      • Aug 2007
      • 31

      #3
      Thanks for the advice Nic;o)

      However, I am still getting the Syntax error.

      I suspect it is something to do with this but I am not sure what is wrong.

      strFilter = "Between #" & Me.txtStartDate .Value & "# And #" & Me.txtEndDate.V alue & "#"

      Thanks

      Comment

      • nico5038
        Recognized Expert Specialist
        • Nov 2006
        • 3080

        #4
        Dates are sometimes mis-interpreted due to the local settings, I always use a format statement:
        Code:
        "Between #" & Format(Me.txtStartDate,"mm-dd-yyyy") & "# And #" & Format(Me.txtEndDate,"mm-dd-yyyy") & "#"
        Oops, also specify which field to test so the statement should look like:

        "TableDateF ield between xxx and yyy"

        Just give it a try.

        Nic;o)

        Comment

        • OzNet
          New Member
          • Aug 2007
          • 31

          #5
          Thanks Nic'o

          I tried:
          strFilter = "DisbursDat e Between #" & Format(Me.txtSt artDate.Value, mm - dd - yyyy) & "# And #" & Format(Me.txtEn dDate.Value, mm - dd - yyyy) & "#" but I still got the error.

          I tried a different approach which is now working:
          the OK button code is:
          Private Sub cmdOK_Click()

          DoCmd.OpenRepor t "rptDisbursemen tSummaryReport" , acViewPreview
          DoCmd.Close acForm, Me.Name

          End Sub

          In the query field DisbursDate I added:
          >=[Forms]![frmMdlDisbursem entSummaryRepor t]![txtStartDate] And <=[Forms]![frmMdlDisbursem entSummaryRepor t]![txtEndDate]

          It is doing what I want it to do,

          Thanks for your suggestions.

          Comment

          • nico5038
            Recognized Expert Specialist
            • Nov 2006
            • 3080

            #6
            Your statement holds too many spaces as you have no surrounding quotes in the format statement, it should look like:
            Format(Me.txtSt artDate,"mm-dd-yyyy")
            instead of your:
            Format(Me.txtSt artDate.Value, mm - dd - yyyy)
            (Access now thinks it's a subtraction)

            Your solution will work too, but has one small disadvantage. When you rename the form the query will stop working.

            Nic;o)

            Comment

            Working...