Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2002
    Location
    Washington D.C.
    Posts
    164

    Unanswered: Getting Usernames from the OS

    How can I get the usernames using VBA from the operating system in NT if the db is on a citrix server.

    Thanks

  2. #2
    Join Date
    Nov 2002
    Location
    San Francisco
    Posts
    251

    Re: Getting Usernames from the OS

    did you try WSH?

    Set WshNetwork = CreateObject("WScript.Network")
    MsgBox "User Name = " & WshNetwork.UserName
    MsgBox "Domain = " & WshNetwork.UserDomain
    MsgBox "Computer Name = " & WshNetwork.ComputerName


    jiri

  3. #3
    Join Date
    Jan 2003
    Location
    Aberdeen, Scotland, UK
    Posts
    168

    OR...

    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 = ""
    End If

    Msgbox "Username = " & FOSUsername
    End Function

  4. #4
    Join Date
    Dec 2002
    Location
    Washington D.C.
    Posts
    164

    Re: OR...

    Thanks for your reply,
    but when I'm running your code I get the error :"sub or function not defined" do you know what the problem could be?

    Thanks




    Originally posted by johncameron
    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 = ""
    End If

    Msgbox "Username = " & FOSUsername
    End Function

  5. #5
    Join Date
    Jan 2003
    Location
    Aberdeen, Scotland, UK
    Posts
    168

    Re: OR...

    Sorry, my fault.

    Put this in a module:
    ------------------------

    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 = ""
    End If
    End Function


    Then put this code where you want to get the username, whenever you use fosusername it will run the module and give the current user name.

    msgbox "User name = " & fosusername
    J.

  6. #6
    Join Date
    Mar 2002
    Location
    Sacramento, CA
    Posts
    120
    Credit to Dev Ashish @
    http://www.mvps.org/access/api/api0008.htm

    Michael

  7. #7
    Join Date
    Jan 2003
    Location
    Aberdeen, Scotland, UK
    Posts
    168
    Well spotted. I should have said that. Wasn't trying to steal his glory. He's got loads of excellent code, check out the above link.
    J.

Posting Permissions

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