Results 1 to 5 of 5
  1. #1
    Dyee4613 is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    4

    VBA error with Keyword Searchbox

    Hi. I tried to post this in a different forum but it didn't look like my post went through and this looks more like the appropriate forum. I am trying to create a search box that allows me to filter data for multiple rows with whatever data was entered. I used this video as reference. https://www.youtube.com/watch?v=N0X8Hg-Sm6A&t=46s



    I was successfully able to do this for items so I tried to expand it. Here is my code.
    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Sub BtnSearch_Click()
    Dim SQL As String
    SQL = "SELECT Assets.ID, Assets.Truck, Assets.Yard, Assets.Time, Assets.Program, Assets.VehicleCondition, Assets.Defects" _
    & "FROM Assets " _
    & "Where [Truck] LIKE '*" & Me.TxtKeywords & "*' " _
    & "OR [Yard] LIKE '*" & Me.TxtKeywords & "*' " _
    & "OR [Program] LIKE '*" & Me.TxtKeywords & "*' " _
    & "OR [VehicleCondition] LIKE '*" & Me.TxtKeywords & "*' " _
    
    
    Me.SubCustomerList.Form.RecordSource = SQL
    Me.SubCustomerList.Form.Requery
    
    
    
    
    
    
    
    
    End Sub
    The error I get ts "Run-time error '3075':

    Syntax error (missing operator) in query expression 'Assets.DefectsFROM Assets Where [Truck] Like '*B91* OR [Yard] LIKE '*B91*' OR [Program] LIKE '*B91' OR [VehicleCondition] LIKE '*B91*. When I click debug, it highlights the bolded line of code (Me.SubCustomerList.Form.RecordSource = SQL)

    Thanks for your help!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    The syntax error is at
    Code:
    Assets.DefectsFROM
    you need a space before FROM.

    When Access highlights an error, it can be in the line or structure just before the highlighted line.

    Good luck and welcome to the forum.

  3. #3
    Dyee4613 is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    4
    Wow. What a silly error. It's working perfectly now. Thank you!

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    My personal preference when building sql strings is to put the space at the beginning of the line, rather than the end of the preceding line - becomes much more obvious whether or not you have included a space

  5. #5
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It looks like you also have an unneeded line continuation character after the last "Or"
    Code:
    & "OR [VehicleCondition] LIKE '*" & Me.TxtKeywords & "*' " _ <<-- right here
    Be aware that "SQL" is a reserved word in Access and shouldn't be used for object/variable names.
    See http://allenbrowne.com/AppIssueBadWord.html#S

    "Time" is also a reserved wordin Access and shouldn't be used for object/variable names.
    See http://allenbrowne.com/AppIssueBadWord.html#T



    I use "sSQL".


    I also put the spaces at the beginning, but use a different method for building the SQL.
    Code:
    sSQL = "SELECT Assets.ID, Assets.Truck, Assets.Yard, Assets.Time, Assets.Program, Assets.VehicleCondition, Assets.Defects"
    sSQL = sSQL & " FROM Assets"
    sSQL = sSQL & " Where [Truck] LIKE '*" & Me.TxtKeywords & "*'"
    sSQL = sSQL & " OR [Yard] LIKE '*" & Me.TxtKeywords & "*'"
    sSQL = sSQL & " OR [Program] LIKE '*" & Me.TxtKeywords & "*'"
    sSQL = sSQL & " OR [VehicleCondition] LIKE '*" & Me.TxtKeywords & "*';"
    This makes it easier (for me) to comment out or add an option.

    Instead of changing the form/sub form record source all of the time, I will set a filter, which is better IMHO. Just sayin....

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

Similar Threads

  1. Searchbox to find record by IssueID
    By kchm2000 in forum Access
    Replies: 6
    Last Post: 04-05-2017, 01:38 AM
  2. Syntax error in Keyword Search code
    By bsegge in forum Programming
    Replies: 10
    Last Post: 10-03-2016, 12:40 PM
  3. SearchBox and Datasheet View
    By gio25 in forum Forms
    Replies: 3
    Last Post: 03-23-2011, 06:07 PM
  4. Help Creating Searchbox with "suggestions"
    By cnstarz in forum Forms
    Replies: 3
    Last Post: 10-11-2010, 06:14 PM

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