Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    speciman_A is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    26

    Update Table after Record Update with Form

    Good evening,



    I am attempting to update a table with the date/time and user name after a record is updated in a form using an AfterUpdate event. The code I plugged in doesn't work (obviously, right?) and I am at wits end trying to debug it. ReturnUserName is a function that calls the login ID of the current user using the advapi32.dll system file. I know that function works because I use it to grant permissions based on whether the logged in user is on the Authorized Users List (a seperate table). Employee_Data is where the information is that the form manipulates and where the date/time and User ID information goes. LastUpdate is the field set aside for the date/time, and LastUpdateID is where the user id should go. The intent is that everytime a record is manipulated, I know exactly when and who edited the record. My experience with VBA is limited to that which I have just read in Microsoft Access 2010 VBA Macro Programming by Richard Shepard; also, I am (unfortunately) using Access 2007. Thank you in advance.

    Option Compare Database
    Option Explicit
    Private Sub Form_AfterUpdate()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim lu As Variant
    Dim lui As Variant
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM [Employee_Data] WHERE [EMP_ID] = '" & Me.EMP_ID & "'")
    lu = Now()
    lui = ReturnUserName
    db.Execute ("UPDATE [Employee_Data] SET [LastUpdate]= '" & lu & "' WHERE [EMP_ID] = '" & Me.EMP_ID & "'")
    db.Execute ("UPDATE [Employee_Data] SET [LastUpdateID]= '" & lui & "' WHERE [SM_ID] = '" & Me.EMP_ID & "'")
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    End Sub

    V/R

    Josh

  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
    What exactly does "doesn't work" mean? You're opening a recordset but not using it. The date value would need # as the delimiter instead of '. Or simply use Now() directly, without concatenation. You could also set both fields with one query; generically:

    UPDATE TableName SET Field1 = OneThing, Field2 = Another WHERE...
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    speciman_A is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    26
    pbaldy,
    Thank you for the very prompt response. It doesn't populate any data into the respective table; I'm a bit confused about the # delimiter, but if I understand correctly, I can put Now() directly into the query? Can I call the public function ReturnUserName directly from within the query? Also, how can I write this without the RecordSet? I'm going to play with it some more in an hour or so. Thank you for your patience.

    V/R

    Josh

  4. #4
    speciman_A is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    26
    I tried a different script, but I'm getting Error 3021: No Current Record. Maybe the SQL is wrong?

    Option Compare Database
    Option Explicit
    Private Sub Form_AfterUpdate()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim SQLstring As String
    Set db = CurrentDb
    SQLstring = "SELECT * FROM Employee_Data WHERE EMP_ID = '" & Me.EMP_ID & "'"
    Set rs = db.OpenRecordset(SQLstring, dbOpenDynaset)

    With rs
    .Edit
    !LastUpdate = Now()
    !LastUpdateID = ReturnUserName
    .Update
    End With
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    End Sub

    V/R

    Josh

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    My guess is that the SQL string isn't returning a record. Try this method to test it:

    BaldyWeb-Immediate window
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    speciman_A is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    26
    Interestingly, the code functions fine when I use the Last_Name field instead of the EMP_ID field in the SQL string. Is there a reason I can't reference the replication ID in my SQL, or does it have to be referenced differently since it is the primary key for the table?

    V/R

    Josh

  7. #7
    speciman_A is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    26
    Ok, so it looks like the Me.WhateverField in my SQL is actually calling the name of the control on the form, i.e. if I were to name the control on the form (as referenced in my previous post) Last_Name_txt, the SQL would bug out on me. Is there anyway to work around this so that the replication ID doesn't have to be on the form to be called in the script?

  8. #8
    speciman_A is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    26
    Ok, so it still won't let me use the replication ID, even if I add it as a control in my form (and set the control to not visible); it gives me Error 3021. Anyway to work around this without cloning the replication ID in another field?

    V/R

    Josh

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If it's a numeric field, you wouldn't want the ' delimiters. Try

    SQLstring = "SELECT * FROM Employee_Data WHERE EMP_ID = " & Me.EMP_ID
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    speciman_A is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    26
    Editing my code as prescribed produced Run-time error 3061, too few parameters. I will google the code and see if I can figure out what is going on.

    V/R

    Josh

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What does the Debug show the SQL to be now?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    speciman_A is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    26
    The intermediate window shows the following:

    SELECT * FROM Employee_Data WHERE EMP_ID = ????????

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    There are actually question marks? That's new. In any case, it would appear that it's not finding the proper value from Me.EMP_ID. Is that the correct name of a control?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    speciman_A is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    26
    That is the correct name of the control. I guess I will continue to Google the problem and see if I can't come up with something.

    V/R

    Josh

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Can you post the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 14
    Last Post: 08-12-2014, 06:33 AM
  2. Replies: 5
    Last Post: 02-16-2014, 02:50 PM
  3. Replies: 4
    Last Post: 03-06-2013, 06:55 AM
  4. Replies: 7
    Last Post: 09-21-2012, 11:09 AM
  5. Replies: 1
    Last Post: 01-22-2011, 12:23 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