Results 1 to 7 of 7
  1. #1
    bignate is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2013
    Posts
    47

    Question Importing NT username into a table

    Hi,



    I have made a form and information that is entered into the form is then put into a table. I now want the nt username of the person that is logged in to the computer to appear in the table when information they have entered into the form has been submitted.

    I have searched for code that does this but I think I have been implementing it slightly wrong.For example there was API code that one website said I should use but an error came up every time I tried to open the form

    If you need any additional info such as any VBA code I am currently using then please ask.

    Thank You

  2. #2
    Subwind is offline Can Only Learn
    Windows XP Access 2010 32bit
    Join Date
    May 2012
    Location
    Portsmouth, UK
    Posts
    61
    Code:
    Option Compare Database
    Option Explicit
    
    Declare PtrSafe Function apiGetUserName Lib "advapi32" Alias "GetUserNameA" (ByVal buffer As String, BufferSize As Long) As Long
    Declare PtrSafe Function apiGetComputerName Lib "Kernel32" Alias "GetComputerNameA" (ByVal buffer As String, BufferSize As Long) As Long
    
    Function GetUserName() As String
        'Wrapper for GetUserName API call.
        '
        'Procedure Syntax
            'Dim strUserName As String
            'strUserName = GetUserName
        'Procedure Syntax
        '
        Dim strName As String
        Dim lngSize As Long
        Dim lngRetVal As Long
        strName = Space(15)
        lngSize = 15
        lngRetVal = apiGetUserName(strName, lngSize)
        GetUserName = Left$(strName, lngSize - 1)
    End Function
    
    Function GetComputerName() As String
        'Wrapper for GetComputerName API call.
        '
        'Procedure Syntax
            'Dim strComputerrName As String
            'strComputerName = GetComputerName
        'Procedure Syntax
        '
        Dim strName As String
        Dim lngSize As Long
        strName = Space(16)
        lngSize = 16
        If apiGetComputerName(strName, lngSize) Then
            GetComputerName = Left$(strName, lngSize)
        Else
            GetComputerName = vbNullString
        End If
    End Function

  3. #3
    Subwind is offline Can Only Learn
    Windows XP Access 2010 32bit
    Join Date
    May 2012
    Location
    Portsmouth, UK
    Posts
    61
    The above code works for me. In the field, =GetUserName() or =GetComputerName()

    ~Matt

  4. #4
    bignate is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2013
    Posts
    47
    Hi,

    Thanks for the quick reply. I made a new module and entered all of the code you gave me. I then put =GetUserName() into the default value for the 'Username' field in my table. However it says there is a syntax error and the lines

    Declare PtrSafe Function apiGetUserName Lib "advapi32" Alias "GetUserNameA" (ByVal buffer As String, BufferSize As Long) As Long
    Declare PtrSafe Function apiGetComputerName Lib "Kernel32" Alias "GetComputerNameA" (ByVal buffer As String, BufferSize As Long) As Long

    are in red so there is some problem with it.

    By the way is the function for getting the computer name necessary for me even though I only want the username?

    Thanks again

  5. #5
    Subwind is offline Can Only Learn
    Windows XP Access 2010 32bit
    Join Date
    May 2012
    Location
    Portsmouth, UK
    Posts
    61
    Try this article, explains it better.

    http://www.databasedev.co.uk/network_user.html

  6. #6
    bignate is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2013
    Posts
    47
    Thanks that works. What you told me to do in the first place was actually correct its just that you didn't say to put =GetUserName() into the default value field of the text box on the form . I also added to your rep for helping me solve my problem as well.

    Thanks,
    Nathan

  7. #7
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    Your problem has been already solved. Just try Environ("UserName") and Environ("ComputerName") functions in immediate window.

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

Similar Threads

  1. Replies: 14
    Last Post: 08-17-2015, 02:32 AM
  2. Replies: 6
    Last Post: 02-21-2013, 10:52 AM
  3. Username/Password From Table
    By mj-egerton in forum Programming
    Replies: 0
    Last Post: 04-03-2011, 04:46 AM
  4. Replies: 3
    Last Post: 09-29-2010, 08:31 AM
  5. Need help in importing table
    By sdondeti in forum Access
    Replies: 6
    Last Post: 11-16-2009, 09:38 AM

Tags for this Thread

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