Results 1 to 7 of 7
  1. #1
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287

    Where is the error?


    Code:
    Private Sub NewRunMontage_Click()
        Dim db As DAO.Database
        Dim qdf As DAO.QueryDef
        Dim strSQL As String
            Set db = CurrentDb()
            Set qdf = db.QueryDefs("Q_Bucket_Matt")
        
        Select Case Me.WhichMontage
            Case 1
                strSQL = "SELECT DISTINCT MERCH_INV_MNR.MNR_CD FROM MERCH_INV_MNR;"
                qdf.SQL = strSQL
            Case 2
                strSQL = "SELECT DISTINCT MERCH_INV_MNR.MNR_CD FROM MERCH_INV_MNR " & _
                        "WHERE (((MERCH_INV_MNR.MNR_CD) Not In ('8300','8310','8413','8500,'8501','8502','8503','8504','8900','8510','8900','8700')));"
                qdf.SQL = strSQL
            Case 3
                strSQL = "SELECT DISTINCT MERCH_INV_MNR.MNR_CD FROM MERCH_INV_MNR " & _
                        "WHERE (((MERCH_INV_MNR.MNR_CD)='8300' Or (MERCH_INV_MNR.MNR_CD)='8310' Or (MERCH_INV_MNR.MNR_CD)='8413' " & _
                        "Or (MERCH_INV_MNR.MNR_CD)='8500' Or (MERCH_INV_MNR.MNR_CD)='8501' Or (MERCH_INV_MNR.MNR_CD)='8502' " & _
                        "Or (MERCH_INV_MNR.MNR_CD)='8503' Or (MERCH_INV_MNR.MNR_CD)='8504' Or (MERCH_INV_MNR.MNR_CD)='8900' " & _
                        "Or (MERCH_INV_MNR.MNR_CD)='8700'));"
                qdf.SQL = strSQL
        End Select
        Set db = Nothing
        Set qdf = Nothing
        
    End Sub
    Case 1 and 3 work great. case 2 gives me a syntax error. Click image for larger version. 

Name:	syntax.PNG 
Views:	17 
Size:	5.5 KB 
ID:	15467

    Can't figure out what is wrong. I created the 3 different cases with the query builder, then just brought them over to the VBA.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,954
    I just tested that syntax and it works.

    8900 is duplicated in the set.

    Don't repeat the qdf.SQL = strSQL within the SELECT CASE. It should be outside the Select.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287
    Its a text field. Guess you just edited your post.

    I'll pull out that duplicate 8900, thanks for noticing that.

    That worked for you as is? Or moving the qdf.SQL=strSQL out of the select fixed it?

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I don't know if you have fixed it , but there is a missing single quote
    Not In ('8300','8310','8413','8500,'8501','8502','8503','8504','8900','8510','8900',' 8700')));"
    Should be
    Code:
    Not In ('8300','8310','8413','8500','8501','8502','8503','8504','8900','8510','8900','8700')));"
    I think that is what is causing the syntax error.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,954
    Good eye, Steve!

    My test of syntax was with my data, did not construct table similar to your data and so did not copy/paste your SQL and did not catch the missing apostrophe, although should have seen it. If you copy/pasted from Access query, that query should have failed.

    My suggestions would not fix the issue but were just better coding.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287
    Perfect! Thanks Steve! June, I moved the qdf line out of the select as well. I appreciate the help, as always! Not sure how that apostrophe get omitted, oh well.

  7. #7
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287
    Ohhhhh going from design view to SQL view uses " (quotes) around all of the criteria, and VBA gave me an error so I switched them all to apostrophes. So it was my bad. Doh!

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

Similar Threads

  1. Replies: 3
    Last Post: 01-23-2014, 07:49 AM
  2. Replies: 3
    Last Post: 03-05-2013, 11:17 AM
  3. Replies: 0
    Last Post: 07-16-2012, 05:42 AM
  4. Replies: 6
    Last Post: 05-30-2012, 12:32 PM
  5. Replies: 1
    Last Post: 05-11-2012, 10:59 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