Discussion: Pulling Info from Active Directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anoble1
    New Member
    • Jul 2008
    • 246

    Discussion: Pulling Info from Active Directory

    Hi,

    I am wanting to make a db that pulls user clients info from the network in Active Directory. Has anyone been able to search through Access and connect to Active Directory? Was wanting to do an number of things, like search by User ID, and pull address, phone and computer info, then remotely connect to PC etc.

    Thanks,
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I use an LDAP query when I need to pull data from AD.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32645

      #3
      I have some code that may help :
      Code:
      'GetUserObject() returns an IADs object representing either the LDAP: string
      '  if passed, or the logged-on user otherwise.
      Public Function GetUserObject(Optional ByVal strDN As String = "") As Object
          If strDN > "" Then
              Set GetUserObject = GetObject("LDAP://" & strDN)
          Else
              strDN = "LDAP://OU=MyBusiness," & _
                      GetObject("LDAP://RootDSE").Get("rootDomainNamingContext")
              Set GetUserObject = ProcessIAD(GetObject(strDN), GetLogonName)
          End If
      End Function
      
      'ProcessIAD() is called recursively and returns an object only when it is a user
      '  that matches strUser.
      Private Function ProcessIAD(ByRef iadVar As Object, strUser As String) As Object
          Dim iadWork As Object
      
          With iadVar
              Select Case IADType(iadVar)
              Case "User"
                  If .sAMAccountName = strUser Then Set ProcessIAD = iadVar
                  Exit Function
              Case "organizationalUnit"
                  For Each iadWork In iadVar
                      Set ProcessIAD = ProcessIAD(iadWork, strUser)
                      If Not ProcessIAD Is Nothing Then Exit Function
                  Next iadWork
              End Select
          End With
      End Function
      
      'IADType() returns whether the IAD should be treated as a container
      '  (organizationalUnit), a user (user), or simply ignored (group).
      Private Function IADType(iadVar As Object) As String
          Dim varWork As Variant
      
          With iadVar
              Call .GetInfo
              For Each varWork In .Get("objectClass")
                  Select Case varWork
                  Case "user", "group", "organizationalUnit"
                      IADType = varWork
                      Exit For
                  End Select
              Next varWork
          End With
      End Function
      The following procedure lists what it finds :
      Code:
      'ShowIADs() shows all users, groups and containers of the AD from strRoot.
      Public Sub ShowIADs(Optional ByRef iadVar As Object, _
                          Optional ByVal strRoot As String = "")
          Dim iadWork As Object
          Dim strWork As String
      
          If iadVar Is Nothing Then
              strWork = "LDAP://" & _
                        strRoot & _
                        "OU=MyBusiness," & _
                        GetObject("LDAP://RootDSE").Get("rootDomainNamingContext")
              Set iadVar = GetObject(strWork)
          End If
          With iadVar
              strWork = IADType(iadVar)
              If strWork > "" Then
                  Debug.Print strWork & "," & _
                              IIf(strWork = "user", .sAMAccountName, "") & "#" & _
                              .distinguishedName & "~";
              End If
              Select Case strWork
              Case "user"
                  Exit Sub
              Case "organizationalUnit"
                  For Each iadWork In iadVar
                      Call ShowIADs(iadWork)
                  Next iadWork
              End Select
          End With
      End Sub
      I don't think there are any calls in here except to standard procedures. I think most of this is found in the VBA library.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        I have attached a Sample Database that should clearly demonstrate how to retrieve Information from Active Directory. It also display AD Variables that can be used in this same context.
        Attached Files

        Comment

        • anoble1
          New Member
          • Jul 2008
          • 246

          #5
          Very Nice, thanks guys

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32645

            #6
            No worries. I'm happy to share it.

            Comment

            Working...