Results 1 to 10 of 10
  1. #1
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47

    Question How to make this sql RecordSource string

    I have a table called inventory as following
    id code qty
    1 -A-B- 10
    2 -G-H- 11
    3 -O-G- 4

    id is auto generated, code is text, qty is integer
    i use a form to list the record from the table, the only criterion is the code of the record must be Instr("-O-G-H-" ,[code])>0 , ie the code of the record must be a substring of "-O-G-H-" , how do i make the sql string?

    I tried this (just the crucial lines)


    nostr = "-O-G-H-"
    Me.RecordSource = "SELECT * FROM inventory WHERE ((InStr('" & nostr & "', code)) > 0);"

    It pull some record out but the records are wrong, the code is not substring of the nostr.

    Any idea?
    Last edited by zx3; 08-27-2011 at 11:03 PM.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    I believe you have the arguments of the InStr() function reversed.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    Quote Originally Posted by pbaldy View Post
    I believe you have the arguments of the InStr() function reversed.
    Thanks for the reply, and the arguments of the InStr function is not reversed in StInr('" & nostr & "', code) , code is the search string and nostr is the string, i want to see if code is part of nostr.

    If it is StInr(code '" & nostr & "'), then no record will return as nostr is always a longer string than string from the field code.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Correct me if I'm wrong, but "code" is the field to be searched and "nostr"contains the value you're searching for.
    Last edited by pbaldy; 03-02-2012 at 03:10 PM. Reason: Attachment removed to reduce board clutter
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    Quote Originally Posted by pbaldy View Post
    Correct me if I'm wrong, but "code" is the field to be searched and "nostr"contains the value you're searching for.
    Yes, you're correct.

    I search through the web, when people use InStr, in most case, they try to see if a string can be found in a field, but my case is to see if the field can be found in a specific string (nostr), if yes, list it on the form.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    I think I understand. Can you post the db to play with?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    Here's the database Attachment 4296, the record actually need to be pulled from 3 tables, I use the sql string to put them together before trimming it down with the InStr criterion.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    The UNION is the problem. I think you need the WHERE clause on all 3 parts:

    Me.RecordSource = "SELECT * FROM Location1 WHERE ((InStr('" & nostr & "', code)) > 0) UNION SELECT * FROM Location2 WHERE ((InStr('" & nostr & "', code)) > 0) UNION SELECT * FROM Location3 WHERE ((InStr('" & nostr & "', code)) > 0);"

    That appears to only pull records that match the test string.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    Quote Originally Posted by pbaldy View Post
    The UNION is the problem. I think you need the WHERE clause on all 3 parts:

    Me.RecordSource = "SELECT * FROM Location1 WHERE ((InStr('" & nostr & "', code)) > 0) UNION SELECT * FROM Location2 WHERE ((InStr('" & nostr & "', code)) > 0) UNION SELECT * FROM Location3 WHERE ((InStr('" & nostr & "', code)) > 0);"

    That appears to only pull records that match the test string.
    Thanks Paul, it works very well, thanks for your time.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    No problem; sorry I misunderstood the goal at first.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Setting Recordsource for Subforms
    By P5C768 in forum Forms
    Replies: 5
    Last Post: 11-16-2010, 05:01 AM
  2. Update Subform Recordsource
    By mystifier in forum Forms
    Replies: 8
    Last Post: 11-15-2010, 03:03 AM
  3. RecordSource help
    By mann2x in forum Access
    Replies: 3
    Last Post: 10-05-2010, 06:44 PM
  4. Replies: 2
    Last Post: 10-16-2009, 02:47 PM
  5. Recordsource with Multiple queries
    By darshita in forum Programming
    Replies: 1
    Last Post: 08-10-2009, 03:17 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