Results 1 to 6 of 6
  1. #1
    tonygg is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    115

    Updating a field with a comma separated list of values from within VBA

    Hi. Sorry in advance as I am sure this has been answered before but I just cant find it anywhere. It is a question about formatting a query string in VBA to correctly handle comma separated values.

    I have a field I need to update in a table in a 3rd party application (I did not design it). The value I need to insert is a string containing comma separated values.



    For example:
    30s Playtime 2017 Scarlet Dotty Daisy 33214-21, 30s Playtime 2017, 30s Playtime 2017 fabric, Moda 30s Playtime 2017, Chloe's Closet 30s Playtime 2017,
    Just to be clear this is a single value of a field. It is meta data to be added to a page on a website.

    I create the contents of the field called Me.ProductMeta in VBA and want to run a query to update the value in the product table:
    Code:
       strUpdate = "UPDATE Product" & vbCrLf & _
        "SET " Product.sSPPMetaKeyWords = " & Chr$(39) & Me.ProductMeta & Chr$(39) & "," & vbCrLf & __
        " WHERE Product.[Product Reference] = " & Chr$(39) & ProductRefAsString & Chr$(39) & ";"
    
    
       db.Execute strUpdate, dbFailOnError

    Access is complaining as it is seeing the commas in my string Me.ProductMeta as field separators rather than data to be inserted into the field.

    Please could someone tell me how I get Access to see them correctly in the update query?

    Many thanks

    Tony

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    I DONT THINK YOU NEED THE vbcrLf,
    try
    strUpdate = "UPDATE Product SET Product.sSPPMetaKeyWords = '" & Chr$(39) & Me.ProductMeta & Chr$(39) & "', WHERE Product.[Product Reference] = '" & Chr$(39) & ProductRefAsString & Chr$(39) & "'";

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I agree with ranman you definitely do not need the vbCrLf.
    Why i there a comma at the end of you sample string?

    Since we don't know what ProductRefAsString is, this is a best guess (untested)

    strUpdate = "UPDATE Product SET Product.sSPPMetaKeyWords = '" & Me.ProductMeta & "' WHERE Product.[Product Reference] = '" & ProductRefAsString & "''";

  4. #4
    tonygg is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    115

    Cool

    Quote Originally Posted by orange View Post
    I agree with ranman you definitely do not need the vbCrLf.
    Why i there a comma at the end of you sample string?

    Since we don't know what ProductRefAsString is, this is a best guess (untested)

    strUpdate = "UPDATE Product SET Product.sSPPMetaKeyWords = '" & Me.ProductMeta & "' WHERE Product.[Product Reference] = '" & ProductRefAsString & "''";
    Hi Both. Thank you

    Sorry about the extra comma. The real SQL statement updates a bunch of other fields. I removed these for clarity and accidentally left in the comma.

    I made the change and it works perfectly. Thank you Didnt think it was this easy. Particularly since I thought I needed to use char(39)s.

    Just to check I am usually using char(39) around strings. Can I just use '" & xxxx & "' (single quote then double quote then space then & then space then variable then space then & then space then single quote then double quote)?

    Many thanks again

    Tony

  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,931
    If you used orange's suggested code structure and it works, I expect that answers your question. It's the structure I always use. I have never used Char(39).

    BTW, the semi-colon should be within the quote marks (as you originally had) or just eliminated as it is not even necessary for action SQL.
    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
    tonygg is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    115
    Quote Originally Posted by June7 View Post
    If you used orange's suggested code structure and it works, I expect that answers your question. It's the structure I always use. I have never used Char(39).

    BTW, the semi-colon should be within the quote marks (as you originally had) or just eliminated as it is not even necessary for action SQL.
    Yes. Thank you

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

Similar Threads

  1. Replies: 6
    Last Post: 02-27-2017, 09:06 AM
  2. Replies: 4
    Last Post: 05-07-2015, 04:02 PM
  3. Replies: 8
    Last Post: 06-04-2014, 10:41 AM
  4. Replies: 1
    Last Post: 04-16-2014, 07:28 PM
  5. Replies: 2
    Last Post: 04-07-2011, 10:15 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