Results 1 to 3 of 3
  1. #1
    chalupabatman is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    114

    Use Variable IN Update Statement

    Good day - I am attempting to use variables in my VBA Update statement, but I can't get the syntax accurate. This is what I have, but is incorrect. Can someone show me how this syntax should be set-up?



    Code:
    Dim on As String, jn As String, sm As String, pn As String
    on = "Red"
    jn = "Mike"
    sm = "Total"
    pn = "Highlife"
    DoCmd.RunSQL ("UPDATE ET SET [jn] = jn,[on] = on,[sm] = sm,[pn] = pn")

  2. #2
    knarfreppep is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Feb 2015
    Location
    Adelaide, Australia
    Posts
    106

    Try this

    Quote Originally Posted by chalupabatman View Post
    Good day - I am attempting to use variables in my VBA Update statement, but I can't get the syntax accurate. This is what I have, but is incorrect. Can someone show me how this syntax should be set-up?

    Code:
    Dim on As String, jn As String, sm As String, pn As String
    on = "Red"
    jn = "Mike"
    sm = "Total"
    pn = "Highlife"
    DoCmd.RunSQL ("UPDATE ET SET [jn] = jn,[on] = on,[sm] = sm,[pn] = pn")


    DoCmd.RunSQL "UPDATE ET SET jn = '" & jn & "', on = '" & on & "', sm = '" & sm & "', pn = '" & pn & "'"

  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,725
    Try (untested)

    Code:
    Dim on As String, jn As String, sm As String, pn As String
    on = "Red"
    jn = "Mike"
    sm = "Total"
    pn = "Highlife"
    
    Debug.print "UPDATE ET SET [jn] = '" & jn  & "',[on] ='" &  on &  "',[sm] = '" & sm & "',[pn] = "" &  pn
    
    DoCmd.RunSQL ("UPDATE ET SET [jn] = '" & jn  & "',[on] ='" &  on &  "',[sm] = '" & sm & "',[pn] = "" &  pn)
    Why 2 character names?
    Always do a debug print before executing action queries to ensure Access is rendering the SQL you expect. Check for errors.
    Action queries can be very unforgiving.

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

Similar Threads

  1. Replies: 2
    Last Post: 06-23-2017, 05:19 AM
  2. Inserting a variable into a SQL statement
    By adacpt in forum Programming
    Replies: 3
    Last Post: 11-25-2012, 06:02 PM
  3. select statement with variable table name
    By dv89k in forum Queries
    Replies: 1
    Last Post: 05-26-2011, 10:54 PM
  4. Help Using Variable in DLookup Statement
    By bcmarshall in forum Access
    Replies: 9
    Last Post: 12-02-2010, 12:44 AM
  5. Replies: 5
    Last Post: 04-05-2010, 11:43 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