Consulting

Results 1 to 3 of 3

Thread: determining user domain account (AD) from VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Here is a routine I half-inched from Randy Birch some years ago

    [vba]

    Option Explicit

    Private Declare Function LookupAccountName Lib "advapi32" Alias "LookupAccountNameA" ( _
    ByVal lpSystemName As String, _
    ByVal lpAccountName As String, _
    Sid As Byte, _
    cbSid As Long, _
    ByVal DomainName As String, _
    cbDomainName As Long, _
    peUse As Long) As Long


    Public Sub TestIt()
    Dim Account As String 'account name of interest
    Dim System As String 'specifying the system
    Dim Domain As String 'domain validating user
    Dim Details As String

    Account = Environ("Username")
    System = ""
    Domain = ""

    Select Case ValidateUser(Account, Domain, System)
    Case True: Details = Account & " - " & Domain
    Case False: Details = "User not found."
    End Select

    MsgBox Details

    End Sub


    Public Function ValidateUser(Optional ByRef AccountName As String, _
    Optional ByRef DomainName As String, _
    Optional ByVal SystemName As String) As Boolean
    Dim success As Long
    Dim cbSid As Long
    Dim cbDomainName As Long
    Dim peUse As Long
    Dim bSID() As Byte

    If AccountName = "" Then AccountName = Environ("Username")
    DomainName = vbNullString
    cbDomainName = 0

    If Len(SystemName) = 0 Then SystemName = vbNullString
    success = LookupAccountName(SystemName, AccountName, 0&, cbSid, DomainName, cbDomainName, peUse)
    If (success = 0) And (cbSid > 0) Then

    DomainName = Space$(cbDomainName)
    ReDim bSID(0 To cbSid - 1)
    success = LookupAccountName(SystemName, AccountName, bSID(0), cbSid, DomainName, cbDomainName, peUse)
    If success > 0 Then

    If cbDomainName > 0 Then DomainName = Left$(DomainName, cbDomainName)
    End If
    End If

    ValidateUser = success

    End Function
    [/vba]
    Last edited by Bob Phillips; 05-21-2008 at 02:50 PM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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