Retrieving User List From Active Directory

CharlesWh

Registered User.
Local time
Today, 02:50
Joined
Jan 19, 2006
Messages
36
I'm using the following code I've found to try and populate a combo box with a list of users from Active directory.

I get a runtime error "Table Does Not Exist" and fails at the following line:

Code:
Set objRecordset = objCommand.Execute

Any advice most welcome :o

Code:
Private Sub Form_Current()
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")

objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection

objCommand.CommandText = _
"<LDAP://dc=ad,dc=integrasoft,dc=local>;(objectcategory=user);sn,givenname;subtree"

Set objRecordset = objCommand.Execute

Me.ComboBox1.RowSourceType = "Value List"

While Not objRecordset.EOF

Debug.Print objRecordset.Fields("givenname") & "," & objRecordset.Fields("sn")

Me.ComboBox1.AddItem (objRecordset.Fields("givenname") & "," & objRecordset.Fields("sn"))

objRecordset.MoveNext

Wend

objConnection.Close

End Sub
 
Caveat: It's been a while since I did AD querying and I do remember LDAP is a bit funny provider - not everything we can do with a SQL can be done in LDAP so you have to tiptoe carefully.

I don't think command is strictly necessary, and would bet that the reason you're getting error is because it's expecting a SQL statement or at least needs CommandText to be set to table. I think it should be a "SELECT * FROM <<LDAP>>"

here's a snippet of my old code:

Code:
    Set rs = New ADODB.Recordset
    
    'Open LDAP recordset
    strSQL = "SELECT userPrincipalName, sAMAccountName, mail, telephoneNumber, otherTelephone " & _
             "FROM 'LDAP://dc=XXX,DC=XXX,DC=XXX'"

    rs.Open strSQL, "Provider=ADSDSOObject;", adOpenStatic, adLockReadOnly, adCmdUnspecified
 

Users who are viewing this thread

Back
Top Bottom