Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2012
    Posts
    28

    Unanswered: Capture last modified user | Ashish API

    I'm trying to capture the network ID user name of the person that last modifies a record in an access form. I'm using access 2010.

    I already use this API to get the user name that enters a new record
    API: Get Login name
    Code:
    ' This code was originally written by Dev Ashish.
    ' It is not to be altered or distributed,
    ' except as part of an application.
    ' You are free to use it in any application,
    ' provided the copyright notice is left unchanged.
    '
    ' Code Courtesy of
    ' Dev Ashish
    '
    Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
        "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Function fOSUserName() As String
    ' Returns the network login name
    Dim lngLen As Long, lngX As Long
    Dim strUserName As String
        strUserName = String$(254, 0)
        lngLen = 255
        lngX = apiGetUserName(strUserName, lngLen)
        If ( lngX > 0 ) Then
            fOSUserName = Left$(strUserName, lngLen - 1)
        Else
            fOSUserName = vbNullString
        End If
    End Function
    I have tried modifying it so many times to capture the last modified user but I have not been successful.

    Has anyone gotten it to work for that purpose?
    I appreciate it any help or suggestions.

    Thanks!

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    why would you modify the supplied code?
    it works.
    all you need to do is insert the code into a code module
    then when ever you need the network logon of the user call the function and assign the value to a suitable variable/ control.
    what have you tried so far?
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Dec 2012
    Posts
    28
    Quote Originally Posted by healdem View Post
    why would you modify the supplied code?
    it works.
    all you need to do is insert the code into a code module
    then when ever you need the network logon of the user call the function and assign the value to a suitable variable/ control.
    what have you tried so far?

    Sorry, I wasn't clear. I did not modify the code.
    I created a Module with his code and then I created a "NetworkID" Field in my forms that has a default value of "=fOSUserName()"

    I don't have the test file on me but I believe I created a new field named something like "lastmodifiedID" and tried a very simliar set up as above.
    Thanks again

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so what isn't working?
    have you tried putting a breakpoint on the on the code and making certain the fucntion runs
    then in the immedite window type
    Code:
    ?fOSUserName()
    I would expect this sor tof item to be set in the forms before update event
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Dec 2012
    Posts
    28
    I'm not following your suggestions:

    Here are the steps i followed that give me the NetworkID per each new entry:

    1. Create field in form named "NetworkID"
    2. Set default value (Under field properties under the Data tab) for the "NetworkID" field to
    Code:
    =fOSUserName()
    3. Create a module named "GetUserID"
    4. Code for Module "GetUseID"
    Code:
    '******************** Code Start **************************
    ' This code was originally written by Dev Ashish.
    ' It is not to be altered or distributed,
    ' except as part of an application.
    ' You are free to use it in any application,
    ' provided the copyright notice is left unchanged.
    '
    ' Code Courtesy of
    ' Dev Ashish
    '
    Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
        "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Function fOSUserName() As String
    ' Returns the network login name
    Dim lngLen As Long, lngX As Long
    Dim strUserName As String
        strUserName = String$(254, 0)
        lngLen = 255
        lngX = apiGetUserName(strUserName, lngLen)
        If (lngX > 0) Then
            fOSUserName = Left$(strUserName, lngLen - 1)
        Else
            fOSUserName = vbNullString
        End If
    End Function
    '******************** Code End **************************
    That's all you have to do and it works great.

    Now, I already have a last date and last time modififed fields that work great. But that's not what I need.

    I need a way to use that function to give me the network ID of the user that last modified the record.

    I already created a new field named "LastUser" but I don't know how apply concept to make it happen.

    Thanks for the help

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    sorry all you can do is record who last made changes
    you cannot magically find who changed the last record unless you store it.

    creating a last edited by column means that all you know who is made the last change, not who made what changes when

    to record who changed the row
    create a text box, set the value of that text box to be the column that stores the userid.
    set that control so it cannot be edited by the user
    in the forms before update event set the value of the control to the fOSUername function

    the refienment is to check if chnages have been made (check each controls original value and new value and if changes have been made then change the userid).

    . if you need more then consider creating an audit log
    also if there is a legal implication then you need to look at more than just the last user edit. lawyers like to have a trail that indicates who, where and when so storing the timestamp of the edit AND on what computer is relevant. also your network policies need to ensure that there can only ever be one computer a user is logged in as. you also need to ensure your HR policies make leaving your comlouter logged on but un attended is a discplinary
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Dec 2012
    Posts
    28
    Thanks for the response quick response and insight!

    Quote Originally Posted by healdem View Post
    creating a last edited by column means that all you know who is made the last change, not who made what changes when
    Correct, that's exactly what I want.
    Quote Originally Posted by healdem View Post
    to record who changed the row
    create a text box, set the value of that text box to be the column that stores the userid.
    set that control so it cannot be edited by the user
    in the forms before update event set the value of the control to the fOSUername function
    ^^I'll give that a shot

  8. #8
    Join Date
    Dec 2012
    Posts
    28
    Got it working, thank you.

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    Me.LastUser = fOSUserName()
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •