Returning active directory user information in VB

MrDeveloper used Ask the Experts™
I'm trying to connect to a specific OU in active directory.  Let's call the OU "MyClient"

First, I tried the code below.  It connects and forms the rs but returns an error Run-time error 13 Type Mismatch.  It blows up on the Debug.Print line.  In the immediate window, I check:


Any idea why this is not returning AD object information?  The rs shows a recordcount of 1000.

Specifically, I'm hoping to loop through AD user objects and update some of their properties.  Thanks very much for the help!

I'm using the following chunk of code:

  Dim conn As ADODB.Connection
      Dim rs As ADODB.Recordset

      Set conn = New ADODB.Connection
      conn.Provider = "ADSDSOObject"
      conn.Open "ADs Provider"

      Set rs = conn.Execute( _
            "<LDAP://>;" _
            & "(objectClass=*);ADsPath,objectClass,cn;subtree")

      While Not rs.EOF
         Debug.Print rs.Fields(0).Value, rs.Fields(1).Value, _

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006
a complete sample here

title : Query Active Directory for Information About a User
source :

Public Function UserInfo(LoginName As String) As String
'PURPOSE: Display information that is available in
'the Active Directory about a given user

'PARAMETER: Login Name for user

'RETURNS: String with selected information about
'user, or empty string if there is no such
'login on the current domain

'REQUIRES: Windows 2000 ADSI, LDAP Provider
'Proper Security Credentials.

'EXAMPLE: msgbox UserInfo("Administrator")


hope this helps a bit


Hey, thanks for the post...  I'm not having any luck getting it to return information from the ad.  I formed the ldap string as such:


But it blows up on "sDomain = oRoot.Get("defaultNamingContext")" (The active directory property cannot be found in the cache).

I actually have a test OU in the company AD, let's call it MyClient.  I would really like to be able to modify properties of the users within this OU
1. First check if there is any value set for the "defaultNamingContext", if its not set, you have to set the value before getting it. The error you got is not a fatal error, only you need to handle the situation where a value is not set for an attribute.



thanks for the help guys, got it working.  You were both helpful so I'm going to split the points.  Thanks again

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial