NT User Name returns profile name "USER"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    NT User Name returns profile name "USER"

    Hi everyone

    I had an interesting problem today with a client.

    I have set up security and privilege on a database depending on NT User Login. However, I've only just realised that the code returns the profile name and not the login name. This company has the profile name "USER" generic to its profiles for some strange reason. It may be a group issue, I'm not network savvy.

    Does anyone know of a way of retrieving the actual login name.

    The code I am using is the standard code as below ...

    [code=vb]
    Declare Function GetUserName& Lib "advapi32.d ll" Alias "GetUserNam eA" (ByVal lpBuffer As String, nSize As Long)

    Function sys_OrigUserID( ) As String
    On Error GoTo Err_sys_OrigUse rID
    ' Returns the username of the current logged in user.
    Dim s$, cnt&, dl&
    Dim max_String As Integer
    Dim usename As String

    max_String = 30
    cnt& = 199
    s$ = String$(max_Str ing, 1)
    dl& = GetUserName(s$, cnt)
    usename = Trim$(left$(s$, cnt))
    usename = UCase(Mid(usena me, 1, Len(usename) - 1))
    sys_OrigUserID = usename

    Exit_sys_OrigUs erID:
    Exit Function
    Err_sys_OrigUse rID:
    Select Case Err
    Case 0 '.insert Errors you wish to ignore here
    Resume Next
    Case Else '.All other errors will trap
    Beep
    MsgBox Err.Description , , "Error in Function SystemCode.sys_ OrigUserID"
    Resume Exit_sys_OrigUs erID
    End Select
    Resume 0 '.FOR TROUBLESHOOTING
    End Function
    [/code]
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Did you try:
    Environ("userna me")

    Nic;o)

    Comment

    • MMcCarthy
      Recognized Expert MVP
      • Aug 2006
      • 14387

      #3
      Originally posted by nico5038
      Did you try:
      Environ("userna me")

      Nic;o)
      You mean change

      sys_OrigUserID = username

      to

      sys_OrigUserID = Environ("userna me")

      Comment

      • nico5038
        Recognized Expert Specialist
        • Nov 2006
        • 3080

        #4
        Yes, I don't even use a function, just this Environ() function "straight away".
        It can be used for all systemvariables available on the computer, so also:
        Environ("comput ername")

        Nic;o)

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          Originally posted by nico5038
          Yes, I don't even use a function, just this Environ() function "straight away".
          It can be used for all systemvariables available on the computer, so also:
          Environ("comput ername")

          Nic;o)
          Thanks Nico

          I've never tried using it in this way. I'll test it out.

          Mary

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            OK I've tested it and Environ seems to give the account name rather than the profile name which is what I need. I won't know for sure until I test it onsite and that won't be until next week but looking good.

            Thanks Nico.

            Mary

            Comment

            • Jim Doherty
              Recognized Expert Contributor
              • Aug 2007
              • 897

              #7
              Originally posted by mmccarthy
              OK I've tested it and Environ seems to give the account name rather than the profile name which is what I need. I won't know for sure until I test it onsite and that won't be until next week but looking good.

              Thanks Nico.

              Mary
              Supplemental to Nico Mary, look at this helpful wiki page you can reference the olther variables not just username to return what you need ie:userdomain,l ogonserver etc etc



              Jim

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Originally posted by Jim Doherty
                Supplemental to Nico Mary, look at this helpful wiki page you can reference the olther variables not just username to return what you need ie:userdomain,l ogonserver etc etc



                Jim
                Thats great Jim.

                Thank you very much.

                Comment

                • questionit
                  Contributor
                  • Feb 2007
                  • 553

                  #9
                  Hi
                  There are many system and user variables in windows that can be used in Access with Environ()

                  If you do this:

                  1. Right click My Computer and then click Properties.
                  2. Click the Advanced tab.
                  3. Click Environment variables.

                  You will see the available variables.

                  You can use them in Command Prompt to read thier values.
                  In command prompt type: echo %Variable-Name%

                  For example: echo %OS%

                  Regards
                  Qi

                  Originally posted by mmccarthy
                  Thats great Jim.

                  Thank you very much.

                  Comment

                  Working...