none
Access 2013 identifying user for new records RRS feed

  • Question

  • Hello,

    My first post so please be gentle :)

    I have an Access program where three different computers/ users enter new records into a table. I would like to know the best way to tag each of these records with the user in a field, so that the table can be filtered to show just one user's entries. I know it can be done manually with a field that they would enter but it would be nicer if it was done automatically based on the computer doing the input. Can a variable be entered when they start their program and stored to reference when needed?

    Thanks in advance for your suggestions

    Dave

    Thursday, October 5, 2017 2:23 PM

All replies

  • The easy method would be to grab it from the Windows environment variable:

    UserName = Environ("USERNAME")

    You can also use the Windows API to retrieve the Windows authenticated user:

    https://stackoverflow.com/questions/677112/how-to-get-logged-in-users-name-in-access-vba


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, October 5, 2017 2:39 PM
  • I'm not big on using Environ because it can be spoofed and prefer, for this, using the API approach

    http://www.theaccessweb.com/api/api0008.htm

    '******************** 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 **************************


    Daniel Pineault, 2010-2017 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net

    Thursday, October 5, 2017 4:53 PM