Results 1 to 3 of 3
  1. #1
    cjohnston is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Posts
    3

    Syntax Error in FROM clause

    Good Morning! I am currently a little bit stumped with a query that I am building. To simplify it, I have a database for tracking jobs. In order to filter the jobs I am using a multiselect list box as one of my controls. If the length of the box is less than a certain value it equates to showing all ships having jobs based on criteria from an option group. If it exceeds that value, then only the selected ships output data. My issue is that the 'ALL' function has a syntax error in the From clause, whereas if I select any number of ships, there is no error. My code is below. thank you for any assistance you guys may be able to provide.

    Code:
    Private Sub btnSrchRecord_Click()
        'Declarations
         Dim strWhere As String     'Criteria String
         Dim ctl As Control
         Dim lngLen As Long         'Length of the criteria string to append to.
         Dim db As DAO.Database
         Dim qdf As DAO.QueryDef
         Dim strSQL As String
         Dim stDocName As String     'Name of document to be opened
         Dim varItem As Variant      'Selected items in multi-select
         Dim strDescrip As String    'Description of WhereCondition
         Dim strDelim As String      'Delimiter for this field type.
         Set db = CurrentDb
         Set qdf = db.QueryDefs("qrySearch")
         Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
         
         '**********************************Query criteria**********************************************
         For Each varItem In Me!lstShip.ItemsSelected
        strWhere = strWhere & ",'" & Me!lstShip.ItemData(varItem) & "'"
     Next varItem
     
     'test box
     'MsgBox Len(strWhere)
     
     
        If Len(strWhere) = 0 Then
                             MsgBox "You did not select anything from the list" _
                             , vbExclamation, "Nothing to find!"
                             Exit Sub
                        End If
        If Len(strWhere) < 7 Then
            Select Case Me.optionSearch
                Case 1
                    ' Remove the leading comma from the string
                        strWhere = Right(strWhere, Len(strWhere) - 1)
                    ' Build the new SQL statement incorporating the string
                        strSQL = "SELECT data.* FROM data" & _
                         "WHERE (data.[Turned_On]) IS NULL " & _
                         "ORDER BY data.[Ship];"
                Case 2
                    ' Remove the leading comma from the string
                        strWhere = Right(strWhere, Len(strWhere) - 1)
                    ' Build the new SQL statement incorporating the string
                        strSQL = "SELECT data.* FROM data" & _
                         "WHERE (data.[funded]) IS NULL " & _
                         "ORDER BY data.[Ship];"
                Case 3
                    ' Remove the leading comma from the string
                        strWhere = Right(strWhere, Len(strWhere) - 1)
                    ' Build the new SQL statement incorporating the string
                        strSQL = "SELECT data.* FROM data" & _
                         "WHERE (data.[Turned_On]) IS NULL " & _
                         "AND (data.[funded]) IS NULL " & _
                         "ORDER BY data.[Ship];"
                Case 4
                    ' Remove the leading comma from the string
                        strWhere = Right(strWhere, Len(strWhere) - 1)
                    ' Build the new SQL statement incorporating the string
                        strSQL = "SELECT data.* FROM data" & _
                         "ORDER BY data.[Ship];"
                End Select
            Else
                Select Case Me.optionSearch
                    Case 1
                        ' Remove the leading comma from the string
                        strWhere = Right(strWhere, Len(strWhere) - 1)
                        ' Build the new SQL statement incorporating the string
                        strSQL = "SELECT data.* FROM data " & _
                         "WHERE (data.[Turned_On]) IS NULL " & _
                         "AND data.[Ship] IN(" & strWhere & ")" & _
                         "ORDER BY data.[Ship];"
                     Case 2
                        ' Remove the leading comma from the string
                        strWhere = Right(strWhere, Len(strWhere) - 1)
                        ' Build the new SQL statement incorporating the string
                        strSQL = "SELECT data.* FROM data " & _
                         "WHERE (data.[funded]) IS NULL " & _
                         "AND data.[Ship] IN(" & strWhere & ")" & _
                         "ORDER BY data.[Ship];"
                     Case 3
                        ' Remove the leading comma from the string
                        strWhere = Right(strWhere, Len(strWhere) - 1)
                        ' Build the new SQL statement incorporating the string
                        strSQL = "SELECT data.* FROM data " & _
                         "WHERE (data.[funded]) IS NULL " & _
                         "WHERE (data.[Turned_On]) IS NULL " & _
                         "AND data.[Ship] IN(" & strWhere & ")" & _
                         "ORDER BY data.[Ship];"
                     Case 4
                        ' Remove the leading comma from the string
                        strWhere = Right(strWhere, Len(strWhere) - 1)
                        ' Build the new SQL statement incorporating the string
                        strSQL = "SELECT data.* FROM data " & _
                         "WHERE data.[Ship] IN(" & strWhere & ")" & _
                         "ORDER BY data.[Ship];"
                 End Select
             End If
            
                               
               
                           
             
      
         '******************************End query criteria**********************************************
         
         qdf.SQL = strSQL 'Sends criteria to query
         'Reset query
         If CurrentProject.AllForms("frmSearch").IsLoaded Then
             Forms!frmSearch.Requery
         End If
         
    
       'Open required document
        stDocName = "frmData"
        DoCmd.OpenForm stDocName, acViewForm
        'Reset Form after search
        Set qdf = Nothing
        Set db = Nothing
        strWhere = ""
        
        
        
        For Each ctl In Me.Section(acDetail).Controls
            Select Case ctl.ControlType
            Case acTextBox, acListBox
                ctl.Value = Null
            Case acRadioButton
                ctl.Value = False
            End Select
        Next
        
    End Sub


  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,847
    It appears that you NEED A SPACE at the end of some of your strings

    Get into the habit of endiing such strings with an extra space or two. It's a common problem.

    For example, this partial string will look like

    strSQL = "SELECT data.* FROM data" & _ "WHERE (data.[Turned_On]) IS NULL " & _

    SELECT data.* FROM dataWHERE (data.[Turned_On]) IS NULL

    You could put some debug.print statements in your code to show you what your SQL SELECT statements look like.

  3. #3
    cjohnston is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Posts
    3

    Solved

    That solved it. Thank you very much....This has been driving me nuts for over an hour.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Syntax error on WHERE clause, possible type conflict?
    By shubhamgandhi in forum Programming
    Replies: 3
    Last Post: 07-21-2011, 01:23 PM
  2. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 AM
  3. Where clause syntax
    By stupesek in forum Reports
    Replies: 7
    Last Post: 09-03-2010, 02:26 PM
  4. Replies: 4
    Last Post: 03-12-2010, 05:42 PM
  5. FROM Clause error message
    By skylite in forum Queries
    Replies: 2
    Last Post: 10-23-2008, 05:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums