Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Apr 2003
    Location
    Pennsylvania, USA
    Posts
    30
    Thanks
    0
    Thanked 3 Times in 1 Post

    Using VBA to get the NT Login ID

    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

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBA to get the NT Login ID

    Hi,

    You could try
    Environ("Username")
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Lounger
    Join Date
    Apr 2003
    Location
    Pennsylvania, USA
    Posts
    30
    Thanks
    0
    Thanked 3 Times in 1 Post

    Re: Using VBA to get the NT Login ID - almost

    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

  4. #4
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBA to get the NT Login ID - almost

    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

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Using VBA to get the NT Login ID - almost

    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

  6. #6
    Star Lounger
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBA to get the NT Login ID - almost

    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.

  7. #7
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Using VBA to get the NT Login ID - almost

    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 <font color=blue>Public Declare Function... </font color=blue>with <font color=blue>Private Declare Function...</font color=blue>

    Hope that resolves your problem.

    Andrew C

Posting Permissions

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