Results 1 to 9 of 9

Thread: Another syntax error in SQL

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Another syntax error in SQL

    I am getting the above error with the following:
    Code:
    strSQL = "SELECT * FROM tbl_Finances"
    strSQL = strSQL & " WHERE Finance_Date > #" & dtDateOne & "#"
    strSQL = strSQL & " AND < #" & dtDateTwo & "#"
    strSQL = strSQL & " ORDER BY Finance_Date"
    I cannot see anything wrong with the output below!
    debug.Print strsql
    SELECT * FROM tbl_Finances WHERE Finance_Date > #01/06/2007# AND < #30/06/2007# ORDER BY Finance_Date
    SELECT * FROM tbl_Finances WHERE Finance_Date > #01/06/2007# AND < #30/06/2007#
    SELECT * FROM tbl_Finances WHERE Finance_Date > #01/06/2007#
    SELECT * FROM tbl_Finances
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Another syntax error in SQL

    If you are not using between then you need to put the field name in twice like this:

    Code:
    strSQL = "SELECT * FROM tbl_Finances"
    strSQL = strSQL & " WHERE Finance_Date > #" & dtDateOne & "#"
    strSQL = strSQL & " AND Finance_Date < #" & dtDateTwo & "#"
    strSQL = strSQL & " ORDER BY Finance_Date"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Another syntax error in SQL

    Thanks Gary.

    How would the SQL look if I was using BETWEEN and which is best to use?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Another syntax error in SQL

    Code:
    WHERE Finance_Date BETWEEN #" & dtDateOne & "# AND " &  dtDateTwo & "#"

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Another syntax error in SQL

    As Hack showed. I perfer the Between but that is just my choice.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Another syntax error in SQL

    Quote Originally Posted by GaryMazzone
    As Hack showed. I perfer the Between but that is just my choice.
    And I agree. I prefer using the Between as well.

    Both ways work, but I just think it is cleaner using BETWEEN.

    However, as Gary points out, it is a individual, personal, preference, not Gospel.

  7. #7

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Another syntax error in SQL

    Thanks both of you

    Don't know if this should be in a new thread or not .....

    I have the following now and sometimes it returns the correct records, sometimes nothing, although it should be.
    Can you see why?
    Code:
        If blnSelectedDates Then
            'reset boolean
            blnSelectedDates = False
            
            strSQL = "SELECT * FROM tbl_Finances"
            strSQL = strSQL & " WHERE Finance_Date BETWEEN #" & dtDateOne & "# AND " & dtDateTwo & "#"
        Else
            Select Case NumOfRecordsDisplayed
                Case 0
                    'display last 30 records
                    'yet to do
                    strSQL = "SELECT * FROM tbl_Finances"
                    
                    strSQL = strSQL & " ORDER BY Finance_Date"
                Case 1
                    'display last 60 records
                    'yet to do
                    strSQL = "SELECT * FROM tbl_Finances"
                    
                    strSQL = strSQL & " ORDER BY Finance_Date"
            End Select
            
        End If
    End If
    
    rs.Open strSQL, cn, adOpenForwardOnly, adLockPessimistic, adCmdText
    
    With FLX
        'remove all rows except any headers, and the first data row (cannot be removed)
        .Rows = .FixedRows
        lngRow = 1  'one because fixed row = 0
        
        'Turn off screen updates (much faster to fill the data)
        blnOldRedraw = .Redraw
        .Redraw = False
     
          'Check if there is any data
        If rs.EOF Then
            'if there is no data, only allow the required blank row, and Hide it (height=0)
          .AddItem ""
          '.RemoveItem .FixedRows
          .RowHeight(.FixedRows) = 0
        Else
            Do While Not rs.EOF
                'Add the row (empty)
                .AddItem ""
                          
                'set values one cell at a time
                .TextMatrix(lngRow, 0) = lngRow
                
                .TextMatrix(lngRow, 1) = rs!Finance_Date.Value
                
                If rs!Finance_Cheque.Value = 0 Then
                    .TextMatrix(lngRow, 2) = ""
                Else
                    .TextMatrix(lngRow, 2) = rs!Finance_Cheque.Value
                End If
                
                .TextMatrix(lngRow, 3) = rs!Finance_Transaction.Value
                
                If rs!Finance_Expenditure = 0 Then
                    .TextMatrix(lngRow, 4) = ""
                Else
                    .TextMatrix(lngRow, 4) = Format(rs!Finance_Expenditure.Value, "#####0.00")
                End If
                
                dblExpenditure = dblExpenditure + Val(rs!Finance_Expenditure.Value)
                
                If rs!Finance_Income.Value = 0 Then
                    .TextMatrix(lngRow, 5) = ""
                Else
                    .TextMatrix(lngRow, 5) = Format(rs!Finance_Income.Value, "#####0.00")
                End If
                
                dblIncome = dblIncome + Val(rs!Finance_Income.Value)
                dblTotal = (dblIncome - dblExpenditure)
                
                If dblTotal < 0 Then
                    .TextMatrix(lngRow, 6) = Format(dblTotal, "#####0.00")
                    .Row = lngRow
                    .Col = 6
                    .CellForeColor = vbRed
                Else
                    .TextMatrix(lngRow, 6) = Format(dblTotal, "#####0.00")
                End If
                
                .TextMatrix(lngRow, 7) = rs!Finance_ID.Value
             
                'Increment our row counter
                lngRow = lngRow + 1
                
                'Move to the next row of data
                rs.MoveNext
            Loop
            
              'Remove the blank row we Left at the top
              '.RemoveItem 1
         
            End If
         
            'Re-enable screen updates (if was previously enabled)
            .Redraw = blnOldRedraw
         
            'Force a redraw of the grid
            .Refresh
    
    End With
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  8. #8
    Lively Member CompositeID's Avatar
    Join Date
    Nov 2004
    Location
    The Neutral Corner
    Posts
    112

    Re: Another syntax error in SQL

    You are not handling the Case Else portion of the method. You are checking if NumOfRecordsDisplayed is equal to 0 or 1; however, how do you intend to handle it if it is any other number? Essentially, it NumOfRecordsDisplayed equals 2 you are passing an empty query, which is the reason you aren't getting any records.
    Last edited by CompositeID; Jul 27th, 2007 at 12:32 PM.
    Dictionary
    Recursion - Until IsUnderstood; see Recursion.

  9. #9

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Another syntax error in SQL

    Thanks for the reply CompositeID

    I haven't get to the ESLE part of the code yet, I am still working on the part before.

    As for NumOfRecordsDisplayed, this is the value of an option button in the Options form.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width