Create function that identifies current user's network login

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hkeiner
    New Member
    • Aug 2008
    • 1

    Create function that identifies current user's network login

    I have an Access 2003 MDB database that uses the "currentuse r()" function extensively (in queries and macros) to control what a particular user can see and do. In Access 2007 (which no longer uses user level security) the current user function now always returns "admin" , which pretty much breaks my database functionality. This database is used in a business environment and all PCs are connected to a common wired Novell network. I found instructions on the web on how to create a custom function that will return a current user's network login ID, which I think I can adapt to my database. However, I can not seem to get it to work.

    I copied the below text in to a new module and named the module "fOSUserNam e". When I add a control to a form with the source as "=fOSUserName() ", I get the following result: #Name?

    I am new to VBA and modules so mistake may be something simple. I have read a lot of info on VBA and modules but have not learned enought to figure out my problem. Any advice?
    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
    Attached Files
    Last edited by NeoPa; Mar 17 '10, 10:57 PM. Reason: Please use the [CODE] tags provided
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    Welcome to Bytes

    Please remember to enclose your code in the code tags: [Code][/Code]
    Try this:
    Code:
    Environ("UserName")
    The Environ function will return varius envrironment variables, such as username. I also sometimes use it to get Environ("Comput erName"). It can be used for alot of other things as well.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      The Function Definition must be 'Public' or else it will be out of Scope and not accessible (See Line #1). The actual API Declaration, however, can remain Private as long as the Function itself resides in the same Module as the API Declaration.
      Code:
      Public Function fOSUserName() As String
      ' Returns the network login name
      Dim lngLen As Long
      Dim 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

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32656

        #4
        Function to Return UserName (NT Login) of Current User may help.

        Comment

        Working...