Results 1 to 6 of 6
  1. #1
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151

    Question How to add a string variable to an Update statement


    Hi all

    Thanks in advance for your time.

    I have some code that I am using to make a copy of a record in a backup table before I update the original record. There are two tables involved. Customers and CustomersBackup

    The Customers table and CustomersBackup table are the same with the exception that the CustomersBackup table has an additional two fields, DateChanged and ChangedBy to save the date that the record was changed and by whom.

    The CustomersBackup.DateChanged field is updated automatically by using the Now() function when the record is written but I would like to have the CustomersBackup.ChangedBy record populated with the LoginUserName variable.

    This code works fine, except that I can't figure out how to add the LoginUserName to the Update code

    Code:
    Dim strSQL as String
    strSQL = "INSERT INTO [CustomersBackup] SELECT Customers.* FROM Customers WHERE (((Customers.CustomerID)=[Forms]![NewCustomerPopup]![CustomerID]))" ';"
    DoCmd.RunSQL strSQL
    Does anyone know how I might be able to accomplish this?

    I thought that I might have to run a separate Update on the CustomersBackup table after the record is written and add the LoginUserName variable to the CustomersBackup.ChangedBy field, but that doesn't seem right.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Untested but try:

    "...SELECT Customers.*, '" & LoginUserName & "' FROM ..."

    You may need to specify fields in both clauses, I don't know.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Thanks Paul

    I'm trying to understand how this would work if I do this

    Code:
    strSQL = "INSERT INTO [CustomersBackup] SELECT Customers.*, '" &  LoginUserName & "' FROM Customers WHERE  (((Customers.CustomerID)=[Forms]![New Customer Popup]![CustomerID]))"  ';"
    If I read the above correctly this is adding the LoginUserName to the Customers.* field list, but I don't see how the LoginUserName variable would be saved to the ChangedBy field in the CustomersBackup table.

    Also, since the CustomersBackup table also contains the DateChanged field, would I not have to include that in the INSERT INTO statement?

    BTW, I did try the above and received the error "No destination field name in INSERT INTO statement"

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    This is another shot in the dark:

    "...SELECT Customers.*, '" & LoginUserName & "' As ChangedBy FROM ..."

    Failing that you probably need to specify fields in both the INSERT clause and the SELECT clause.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    If you don't specify destination fields, they must be in same order in table design as corresponding fields in the SELECT query. If an autonumber ID field is the first field, this might cause an error, although, an INSERT action can insert data into autonumber field. So does destination table have an autonumber field?

    Are you using CurrentDb.Execute or DoCmd.RunSQL? With the first, the form control reference must be concatenated.

    Use the destination field name as an alias in the SELECT: , '" & LoginUserName & "' AS ChangedBy FROM

    And yes, if you need to include today's date, just use Date() function in the string: Date() AS DateChanged

    Syntax works for me.
    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
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Hi Paul and June7

    Thanks for the replies. I totally forgot about the AS operator that I have in fact used in the past Hopefully next time I'll remember. And that has resolved the issue.

    I am using DoCmd.RunSQL and the desitnation table does have an autonumber field (primary).

    So the code below worked very nicely.

    Code:
    strSQL = "INSERT INTO [CustomersBackup] SELECT Customers.*, Now() AS DateChanged, '" & LoginUserName & "' AS ChangedBy FROM Customers WHERE (((Customers.CustomerID)=[Forms]![New Customer Popup]![CustomerID]))" ';"
    DoCmd.RunSQL strSQL
    I'll continue tinkering with the whole backup routine. Now I'll need to implement a history query so users will be able to view a change history. Funny how one thing leads to another.

    Thank you both again.

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

Similar Threads

  1. Replies: 3
    Last Post: 07-20-2020, 01:04 PM
  2. Use Variable IN Update Statement
    By chalupabatman in forum Programming
    Replies: 2
    Last Post: 09-26-2017, 08:19 AM
  3. Replies: 2
    Last Post: 06-23-2017, 05:19 AM
  4. Getting whole value from a string variable
    By Always_Learning in forum Programming
    Replies: 4
    Last Post: 09-30-2016, 06:57 AM
  5. Replies: 3
    Last Post: 05-28-2013, 12:53 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