Results 1 to 5 of 5
  1. #1
    kagoodwin13 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    181

    SQL Query based off List Box

    I am trying to run an UPDATE SQL command based on items selected in a list box.



    The purpose of this form is to submit a document (ex. Purchase Order) based on a part number. The database user browses to the applicable document, and selects the appropriate part numbers. The database then saves the file location of the part number document, so that the link can later be opened with a report. The list box on the form has a database-controlled list of the part numbers, which is the primary key of the database.

    The purpose of the SQL command is to overwrite a part number to prevent part number errors. The Excel file that is being imported by this code is too prone to user formatting errors when referring to the part number, so I am trying to create an error-proof way of selecting the applied part number for each document.

    When I try to submit part number 1234567890, I am currently having a problem with this code:

    Code:
    For Each varItem In Me.listPART_NUMBER.ItemsSelected
                    strPathFile = Me.txtFILE_PATH
                    strTable = "tblPCP1_APPROVAL"
                    Sheet_Name = "DOCUMENT_APPROVAL" & "!"
                    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames, Sheet_Name
                    DoCmd.RunSQL ("UPDATE tblDOCUMENT_APPROVAL SET tblDOCUMENT_APPROVAL.PN = " & Me.listPART_NUMBER.ItemData(varItem) & " WHERE PN Is Null;")
                   Next varItem
    Run-Time error '3075': Syntax error (missing operator) in query expression '1234567890'
    I also tried this, but it was a syntax error:

    Code:
    For Each varItem In Me.listPART_NUMBER.ItemsSelected
                    strPathFile = Me.txtFILE_PATH
                    strTable = "tblPCP1_APPROVAL"
                    Sheet_Name = "DOCUMENT_APPROVAL" & "!"
                    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames, Sheet_Name
                    DoCmd.RunSQL ("UPDATE tblDOCUMENT_APPROVAL SET tblDOCUMENT_APPROVAL.PN = " '" & Me.listPART_NUMBER.ItemData(varItem) & "' " WHERE PN Is Null;")
                   Next varItem
    Any help?

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    If PN is a text string field, This would be the syntax:
    Code:
     DoCmd.RunSQL ("UPDATE tblDOCUMENT_APPROVAL SET tblDOCUMENT_APPROVAL.PN = '" & Me.listPART_NUMBER.ItemData(varItem) & "' WHERE PN Is Null;")
    Also, for what it's worth, I usually build the SQL into a local string variable a few lines before, so when I encounter issues like this, I can debug.print the field and look at the SQL myself.

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    Let me back up dal's post. Creating the string before you try to execute it and debug.printing it before you actually issue the command is going to save you a lot of time and aggravation, it's an extra line or two of code but it's well worth it.

    You may also be having a problem if PN is a unique key and you are trying to update your table when there is more than one record with a null PN field.

  4. #4
    kagoodwin13 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    181
    Dal, your post solved my syntax error. Thanks.

    I tried to save the SQL into strSQL and then use the command Currentdb.Execute strSQL, but that also did not work. I kept getting errors about "Too few parameters."

  5. #5
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    OKay, post the SQL from the debug.Print message here.

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

Similar Threads

  1. Replies: 8
    Last Post: 06-04-2014, 10:41 AM
  2. VBA for records based on cbo Value List
    By libraccess in forum Programming
    Replies: 3
    Last Post: 02-22-2013, 02:39 AM
  3. Replies: 2
    Last Post: 06-09-2012, 07:59 AM
  4. Sharepoint List Query Filter based upon User Login
    By Steven.Allman in forum SharePoint
    Replies: 5
    Last Post: 03-22-2012, 11:30 AM
  5. list box on a form based on query
    By nykedel in forum Forms
    Replies: 0
    Last Post: 06-15-2010, 01:12 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