Returning active directory user information in VB

MrDeveloper
MrDeveloper used Ask the Experts™
on
I'm trying to connect to a specific OU in MyCompany.com 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:

?rs.Fields(0).Value
LDAP://xitech.com/DC=MyCompany,DC=com

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://MyCompany.com>;" _
            & "(objectClass=*);ADsPath,objectClass,cn;subtree")
           

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

    conn.Close
Comment
Watch Question

Do more with

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

title : Query Active Directory for Information About a User
source : http://www.freevbcode.com/ShowCode.Asp?ID=710

----------
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

Author

Commented:
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:

"LDAP://CN=Users,DC=MyCompany,DC=COM"

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.


Regards,
Pravarakhya

Author

Commented:
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