Results 1 to 9 of 9
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,045

    strWhere syntax error

    Hello:



    I need some assistance with tweaking my strWhere clause. Once I modified it, the 2 lines (as part of *Option #3) result in a syntax error. Here's what I have:

    Option #1: Criteria for 2 fields [SOURCE_FILE] & [PRODUCT_TABLE]
    Code:
    strWhere = "WHERE [SOURCE_FILE] in (" & Left(strSource, Len(strSource) - 1) & ") AND [PRODUCT_TABLE] in (" & Left(strProduct, Len(strProduct) - 1) & ")"
    ... option #1 executes w/o any errors.


    Option #2: Criteria for 2 fields [SOURCE_FILE] & [PRODUCT_TABLE] with multiple rows WHERE 'EXTRA' needs to remain hard-coded
    Code:
    strWhere = "WHERE ((([01_tbl_ProductTables].SOURCE_FILE) in (" & Left(strSource, Len(strSource) - 1) & ")) And (([01_tbl_ProductTables].PRODUCT_TABLE) = 'DEMOGRAPHICS')) " & _
                    "Or ((([01_tbl_ProductTables].SOURCE_FILE) = 'EXTRA') And (([01_tbl_ProductTables].PRODUCT_TABLE) = 'DEMOGRAPHICS'))"
    ... option #2 executes w/o any errors.


    Option #3: Criteria for 2 fields [SOURCE_FILE] & [PRODUCT_TABLE] with multiple rows WHERE 'EXTRA' needs to remain hard-coded but 'Demographics' needs to be dynamic
    Code:
    strWhere = "WHERE ((([01_tbl_ProductTables].SOURCE_FILE) in (" & Left(strSource, Len(strSource) - 1) & ")) And (([01_tbl_ProductTables].PRODUCT_TABLE) in (" & Left(strProduct, Len(strProduct) - 1) & ") " & _
                   "Or ((([01_tbl_ProductTables].SOURCE_FILE) = 'EXTRA') And (([01_tbl_ProductTables].PRODUCT_TABLE) in (" & Left(strProduct, Len(strProduct) - 1) & ")"""
    ... option #3 results in syntax error upon execution. See attached JPG for details.


    My question: How do I need to modify the VBA (i.e., add/remove parenthesis and/or other characters) so that option #3 code will execute?

    Thank you,
    Tom
    Attached Thumbnails Attached Thumbnails Syntax Error.JPG  
    Attached Files Attached Files

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    I get an invalid attachment error when trying to download your test.zip file.

    Whats the output when you debug.print the strWhere clause? Thats where you'll see the error.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,045
    moke123 -- I am currently away from my desk. I will re-post file when I return (1-2 hrs).

    Thank you.

  4. #4
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,045
    Did the attached Jpg not include the error message/feedback?

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    In addition to Moke's advice, when you are building a SQL string you don't need the million (((('s that Access insists on adding in Where clauses built in the Query designer.

    Remove all but the ones you need, (to force an AND or OR clause) and you will see a clearer picture.
    I think you have an extra "" soemwhere but it's difficult to see.

    Also on really long SQL string I prefer to break them up into smaller chunks, it lets you see the query better.
    Code:
    strWhere = " Source = 1  " 
    strWhere = strWhere  &" AND firstName = 'John' " 
    strWhere = strWhere  &" AND MyDate = #" &  Me.Mystartdate & " # "
    etc etc
    This also has the benefit of being able to easily comment out any part of it to see where it's all gone wrong.

    I would also put Left(strSource, Len(strSource) - 1) and Left(strProduct, Len(strProduct) - 1) into there own variables to tidy thing up and reduce the length of it all.
    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 ↓↓

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725

  7. #7
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,045
    moke123 -- I compacted/repaired DB. See attached zip. Hopefully this one works for you.

    //

    All -- thank you for chiming in on this post... I'll check out the recommendations.
    Attached Files Attached Files

  8. #8
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,045
    Success!!! Removing the "million (((('s" helped tremendously! It now executes just fine.

    I'm still working on breaking up the WHERE clause but the original problem has been solved. Thank you everyone!

  9. #9
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Good to hear you have it sorted out.
    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 ↓↓

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

Similar Threads

  1. Replies: 15
    Last Post: 03-01-2021, 12:07 PM
  2. Replies: 1
    Last Post: 06-21-2012, 07:58 PM
  3. Extra ( in strWhere
    By TinaCa in forum Programming
    Replies: 2
    Last Post: 10-10-2011, 02:18 PM
  4. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 AM
  5. strWhere VB Code
    By chhinckley in forum Access
    Replies: 5
    Last Post: 08-25-2010, 12:56 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