Results 1 to 7 of 7
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,080

    Criteria expression in Design Mode not getting what's desired

    I'm designing a query in design mode from which I capture the SQL to be used to create a DAO recordset in code. I want Jet to do as much of the work as possible, so I created the query this way:


    Click image for larger version. 

Name:	02.jpg 
Views:	14 
Size:	79.4 KB 
ID:	24067

    I've already gathered the ID1, ID2,,,,,,,ID6 into a single string and the idea is to refresh the DAO recordset with each number to be searched. I was hoping the InStr function would then limit the records to those where the number was found. As coded I get this:
    Click image for larger version. 

Name:	01.jpg 
Views:	14 
Size:	162.8 KB 
ID:	24068

    What I want in this example is for SQL to return ONLY the two records you see where I've highlighted with the arrows, namely the two strings that contain the value 180.

    Is there a syntax error in the InStr function?

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    if groupings is a table it looks like your data is not normalised so results in a complex and possibly unworkable query
    in sql parlance, # relates to date types so should not be used in this way
    instr returns a number, so using len (a string function) won't work

    It is not clear what you are trying to achieve so can only speculate what a solution may be. Suggest explain in more detail what 'the idea is to refresh the DAO recordset with each number to be searched. I was hoping the InStr function would then limit the records to those where the number was found.' really means - use examples and explain your table relationships

  3. #3
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    A few things:

    1. If a value is not found in the InStr function, it returns a 0. If you apply the Len function to 0, it returns a 1. So, if you are applying that sort of criteria, you do not actually want/need the Len function. Also, you do not want double-quotes around your field name [strID]. If you do, it is checking the literal text string [strID] and not the calculated field [strID]. So, the criteria would look something like (however, read my next part).
    Code:
    =InStr([strID],"#180#")>0
    2. I have had trouble in the past with having a calculated field in a query, and then try to apply criteria to that calculated field in the same query when trying to access it by the calculated field name, instead of the underlying calculation. So you might need it to look something like this:
    Code:
    =InStr("#" & [ID1] & "#" & [ID2] & "#" & [ID3] & "#" & [ID4] & "#" & [ID5] & "#" & [ID6] & "#","#180#")>0
    3. If it were me, I would not use either of those two criteria. I would just use this instead:
    Code:
    WHERE [ID1]="180" Or [ID2]="180" Or [ID3]="180" Or [ID4]="180" Or [ID5]="180" Or [ID6]="180"

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    on the basis the IDs are numbers you would use
    Code:
    WHERE [ID1]=180 Or [ID2]=180 Or [ID3]=180 Or [ID4]=180 Or [ID5]=180 Or [ID6]=180

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    on the basis the IDs are numbers you would use
    Good point. I shouldn't have assumed that those values were String/Text.

  6. #6
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,080
    Both
    Code:
    WHERE [ID1]="180" Or [ID2]="180" Or [ID3]="180" Or [ID4]="180" Or [ID5]="180" Or [ID6]="180"
    and

    Code:
    WHERE [ID1]=180 Or [ID2]=180 Or [ID3]=180 Or [ID4]=180 Or [ID5]=180 Or [ID6]=180
    Work as desired. The resulting SQL form of the query becomes a bit unwieldy in appearance. "180" was just my test value in trying to get the query to work testing for the existence of a particular value among the IDx fields in the GROUPINGS table. In the implementation of the app, there will be a VBA integer variable that will be what is being search.

    So, if my search value is say intSrch , then the WHERE clause looks like this:
    Code:
    WHERE (([ViewWith] = True) AND ("[ID1]=" & intSrch & Or "[ID2]=" & intSrch & Or "[ID3]=" & intSrch & Or "[ID4]=" & intSrch & Or "[ID5]=" & intSrch & Or "[ID6]=" & intSrch))
    I'll post the Function code that uses the query with a DAO recordset later today.

    Thanks,
    Bill

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,080
    The SQL SELECT string in code became a syntax nightmare. I had to move on with more pressing issues, so I relented and did the "strID" string search in code.

    Thanks for the thoughts given to this post.
    Bill

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

Similar Threads

  1. Replies: 5
    Last Post: 05-24-2016, 06:08 AM
  2. How to determine if in Design Mode
    By JrMontgom in forum Access
    Replies: 2
    Last Post: 01-02-2016, 10:24 PM
  3. Replies: 7
    Last Post: 10-03-2014, 05:50 PM
  4. Cannot open in design mode
    By kpalar in forum Forms
    Replies: 4
    Last Post: 08-01-2014, 12:51 PM
  5. Replies: 4
    Last Post: 01-14-2011, 10:37 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