Results 1 to 3 of 3
  1. #1
    jcright is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2022
    Posts
    21

    SQL with Where condition and like

    Hello all,

    I'm struggling to figure out how to create an SQL statement using where and like that checks a serial number entered on a form and returns certain info.

    Set dbQuick = CurrentDb
    Set rsQuick = dbQuick.OpenRecordset("SELECT Comms2.DateofComm, Comms2.LastName, " & _
    "Comms2.SerialNumber, Comms2.SP1 FROM Comms2 " & _
    "WHERE Comms2.SerialNumber='" & Me.SerialNumberTextBox & "';")

    While the code works, it only works if I have an exact serial number match. I want change it so that I get a close approximation. So if my recorded serial number is A54167 but I enter in 54167, then I get a match for that record. If my serial number is ever off, then it is usually that first character or it was never sold. I had thought that if I change the last line, then it should work. Btw, Me.SerialNumberTextBox refers to the text box on my form and that is the only fill-able textbox.

    "WHERE Comms2.SerialNumber Like '*' & Me.SerialNumberTextBox & '*' & "';")
    When I substitute that code in it tells me that I have a list separator problem. So I know I have some sort of problem with my quotes, maybe something else? I know this has to be an easy fix, but I can't figure out what that fix is.



    Thanks,
    John

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Debugging tip number 1 - put your SQL strings into a variable and debug.print it :

    Code:
    Dim strSQL as String
    
    Set dbQuick = CurrentDb
    
    strSQL = "SELECT Comms2.DateofComm, Comms2.LastName, " & _
    "Comms2.SerialNumber, Comms2.SP1 FROM Comms2 " & _
    "WHERE Comms2.SerialNumber Like '*' & Me.SerialNumberTextBox & '*' & "';")
    Debug.Print strSQL
    
    Set rsQuick = dbQuick.OpenRecordset(strSQL)
    This will show you exactly where your concatenation isn't quite right in the immediate window.

    If you are still stuck come back with a copy of the immediate window text.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    You're doing this in vba? Might be best if you posted the whole statement. Looks to me like you're missing ending quote - after the )" but also before the first '
    You don't need ; in vba constructed code. In you example, you don't need to separate it if you do use it: ... Me.SerialNumberTextBox & "'*'; )"

    I had to insert an extra space otherwise I get an emoticon.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Where condition
    By exupery in forum Macros
    Replies: 2
    Last Post: 06-05-2017, 05:54 PM
  2. Where condition
    By DavidTCH in forum Access
    Replies: 3
    Last Post: 03-25-2016, 09:07 PM
  3. Where Condition
    By NISMOJim in forum Programming
    Replies: 4
    Last Post: 04-09-2014, 11:10 PM
  4. Help with using the AND condition
    By ssturges in forum Access
    Replies: 1
    Last Post: 11-25-2012, 12:36 AM
  5. How to use IIF condition
    By nshaikh in forum Queries
    Replies: 4
    Last Post: 09-12-2008, 01:23 AM

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