• Using VBA to get the NT Login ID

    Author
    Topic
    #354533

    In MS Access I would like to use the NT login ID in a parameter I am sending to SQL Server via ODBC Direct to execute a stored procedure. I know I can ask the user to enter it again but if I can use the authenticated user ID from NT, security obviously goes up several notches.

    My goal is be able to check for it when a submit button is clicked on the form, which then asks for the stored procedure to execute and return a record set to Access. The ODBC thing is working now I need the NT ID to go with it as a parameter.

    I belive I am going to have to call a win api to get this done which will all be new to me, so any and all help as to how this would be done would be greatly appreciated.

    Thanks

    Viewing 0 reply threads
    Author
    Replies
    • #521046

      Hi,

      You could try
      Environ(“Username”)

      • #521074

        I thought about that, its an easy solution, but win98 and win95 users won’t have an username environment variable.

        For anybody reading this that has a pure NT environment environ(“USERNAME”) works but USERNAME must be caps.

        Still Looking

        • #521075

          try the following (It works on w2k pro and the allapi guide seems to state that it will work with w95 or later.)

          I haven’t tried it from access but it works from excel.

          Private Declare Function w32_WNetGetUser Lib “mpr.dll” Alias “WNetGetUserA” _
          (ByVal lpszLocalName As String, ByVal lpszUserName As String, lpcchBuffer As Long) As Long
          ‘##################################
          Sub GetUserName()
          Dim lpUserName As String, lpnLength As Long, lResult As Long
          ‘Create a buffer
          lpUserName = String(256, Chr$(0))
          ‘Get the network user
          lResult = w32_WNetGetUser(vbNullString, lpUserName, 256)
          If lResult = 0 Then
          lpUserName = Left$(lpUserName, InStr(1, lpUserName, Chr$(0)) – 1)
          MsgBox “The user’s Network Logon Name is ” + lpUserName + “.”, vbInformation + vbOKOnly
          Else
          MsgBox “No user found !”, vbExclamation + vbOKOnly
          End If
          End Sub

          Brooke

        • #521077

          The following should work with Win9x and Win NT, but not tested on NT

          Public Declare Function GetUserName Lib “advapi32.dll” _
          Alias “GetUserNameA” (ByVal lpBuffer As String, nSize As Long) As Long

          Function GetUser() As String
          Dim lpBuff As String * 255
          Dim ret As Long, UserName As String
          ret = GetUserName(lpBuff, 255)
          GetUser = Left(lpBuff, InStr(lpBuff, Chr(0)) – 1)
          End Function

          Andrew C

          • #560307

            I want to use this get user function in a Word document — it detects the net user ID whenever the document is opened. Question is, where do I place the declaration of the GetUserName function call to the DLL? Nothing I do seems to work.

            • #560403

              The API declaration should be place at the top of the module before any Functions or Subs are defined.

              If you are using it in the ThisDocument object, you need to replace Public Declare Function… with Private Declare Function…

              Hope that resolves your problem.

              Andrew C

    Viewing 0 reply threads
    Reply To: Reply #560307 in Using VBA to get the NT Login ID

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel