Results 1 to 8 of 8
  1. #1
    gustavoavila is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    6

    Passing textbox value into sql query parameter

    Hi folks,



    I'm having a very simple issue (I think) trying to pass a textbox value as parameter in a sql query.

    I have a multiline textbox and I use it to find several values in the database, so in this textbox all the values are pasted each per line and I have a small code to convert it to single line comma separated values.

    This is an example:

    The user enter the data:

    [Text0]
    A1C556CC3C-TNNN
    C010070H13

    The code convert this data to a single comma separated string and runs the query: ex: "A1C556CC3C-TNNN","C010070H13"

    Code:
    test = """" & Replace([Forms]![Search]![Text0], Chr(13) & Chr(10), """,""") & """"
    [Forms]![Search]![Text0].Value = test
    DoCmd.OpenQuery "FindPartNo", acViewNormal, acReadOnly
    In the SQL code I use the IN operator to find the exact value for each record:

    FindPartNo sql query:
    Code:
    SELECT Classifications.BU, Classifications.WisperPlantID, Classifications.PartNumber, Classifications.PartDesc, Classifications.US_CL_Code, Classifications.MX_CL_Code, Classifications.TARIC_CL_Code, Classifications.COEProject, Classifications.Supplier, Classifications.BrokerRequest, Classifications.CreatedBy
    FROM Classifications
    WHERE Classifications.PartNumber In ([Forms]![Search]![Text0]);

    The problem here is, the query doesn't return results but if I modifiy the query and I put:

    Code:
    WHERE Classifications.PartNumber In ("A1C556CC3C-TNNN","C010070H13");
    ... the query returns the correct results.

    What do you think? I'm passing the value incorrect into the sql code?

    Thank you for your help!

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Where are you running these slices of code?
    Have you run a debug.print SQLCode for the second slice to see if it's actually giving you the value you expect in your IN clause?

  3. #3
    gustavoavila is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    6
    The vba code runs when I click on the search button. If you see, the vba code only converts the data pasted in the textbox and excecute a query called FindPartNo (the query is not in the vba code is stored in the database).

    I'm running a debug.print in a test sub, with this code:

    Code:
    Private Sub Command146_Click()
    SQL = "SELECT Classifications.BU, Classifications.WisperPlantID, Classifications.PartNumber, Classifications.PartDesc, Classifications.US_CL_Code, Classifications.MX_CL_Code, Classifications.TARIC_CL_Code, Classifications.COEProject, Classifications.Supplier, Classifications.BrokerRequest, Classifications.CreatedBy FROM Classifications WHERE Classifications.PartNumber In (" & [Forms]![Search]![Text0] & ");"
    Debug.Print SQL
    End Sub
    And the debug console returns:

    Code:
    SELECT Classifications.BU, Classifications.WisperPlantID, Classifications.PartNumber, Classifications.PartDesc, Classifications.US_CL_Code, Classifications.MX_CL_Code, Classifications.TARIC_CL_Code, Classifications.COEProject, Classifications.Supplier, Classifications.BrokerRequest, Classifications.CreatedBy FROM Classifications WHERE Classifications.PartNumber In ("A1C556CC3C-TNNN","C010070H13");
    So, I think the query look good but still not running

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    can you provide a sample database with garbage data in it for analysis please. Just create a copy of your db, scrap all the data just leave enough for it to re-create your problem and disguise any data you don't want to share, then compact/repair and upload it to this site.

  5. #5
    gustavoavila is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    6
    Sure,

    Attached you can find an example.CL codes search tool.zip

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Try adding this field:

    Expr1: InStr([forms]![search]![text0],[partnumber])

    add a criteria of >0

    Remove the criteria from your PARTNUMBER field

  7. #7
    gustavoavila is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    6
    I tried this before but I want to return exact matches . With InStr returns the fields but if the data contains another record similar, the query will return both.

    For example:

    If in the database existe the part number:

    12345
    123
    345

    And I only search the part number 12345, the query will return all the matches. Attached screenshot.Click image for larger version. 

Name:	Ex.JPG 
Views:	12 
Size:	105.5 KB 
ID:	16221

  8. #8
    gustavoavila is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    6
    Finally I solve the issue creating the query with VBA code and works well. The code is very rude but works

    Code:
            test = """" & Replace([Forms]![Search]![Text0], Chr(13) & Chr(10), """,""") & """"
            strSQL = "SELECT Classifications.BU, Classifications.WisperPlantID, Classifications.PartNumber, Classifications.PartDesc, Classifications.US_CL_Code," & _
            "Classifications.MX_CL_Code, Classifications.TARIC_CL_Code, Classifications.COEProject, Classifications.Supplier, Classifications.BrokerRequest," & _
            "Classifications.CreatedBy FROM Classifications WHERE Classifications.PartNumber In (" & test & ");"
            
            
            Set qdf = CurrentDb.CreateQueryDef("FindPartNo", strSQL)
            DoCmd.OpenQuery qdf.Name
    Hope someone find this helpful. Thanks for your help rpeare

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

Similar Threads

  1. Query with parameter from Form Textbox
    By Juan4412 in forum Queries
    Replies: 1
    Last Post: 07-10-2013, 02:33 PM
  2. Replies: 2
    Last Post: 05-06-2012, 03:52 PM
  3. Parameter Passing
    By Juan4412 in forum Queries
    Replies: 1
    Last Post: 11-21-2011, 10:23 AM
  4. Passing List of Parameter
    By vignes10 in forum Access
    Replies: 3
    Last Post: 09-15-2011, 07:35 AM
  5. Macro passing a parameter
    By SlowPoke in forum Access
    Replies: 1
    Last Post: 09-26-2010, 09:57 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