How to get users from Active Directory using LDAP?

Hi

I'm using Access 2010 to populate a table with all users information from active directory.  I use the following code but somehow this does not seem to work.  Help please.

Function PopulateADSData()

Const ADS_SCOPE_SUBTREE = 2
Dim objRecordSet As Object

' ADODB Connection to AD
Dim objConnection As ADODB.Connection
Dim objCommand As ADODB.Command

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Sort On") = "Name"

objCommand.CommandText = "<<a href="ldap://dc=enterprisenet,dc=org>;(objectCategory=person">LDAP://dc=enterprisenet,dc=org>;(objectCategory=person);" & "adsPath;subtree"
    '"SELECT Name FROM 'LDAP://dc=enterprisenet,dc=org' WHERE objectClass='user' AND objectCategory='Person'"
Set objRecordSet = objCommand.Execute

'MsgBox objRecordSet.RecordCount


'objRecordSet.MoveFirst

Dim DB As DAO.Database
Dim qry1 As DAO.QueryDef
Dim rst1 As DAO.Recordset
   
Set DB = CurrentDb

Set qry1 = DB.CreateQueryDef("")
qry1.SQL = "DELETE * FROM ADUsers"
qry1.Execute

qry1.SQL = "Select * FROM ADUsers"
Set rst1 = qry1.OpenRecordset
 
 Do Until objRecordSet.EOF
   
    Set objItem = GetObject(objRecordSet.Fields("Adspath"))
    rst1.AddNew
    rst1.Fields("Display Name") = objItem.DisplayName
    rst1.Fields("LANID") = objItem.samaccountname
    rst1.Fields("Email") = objItem.mail
   
    rst1.Update
    'rst1.MoveNext

 Loop
 MsgBox "Completed"
End Function

March 15th, 2014 9:15am

You have a number of problems.

!) This is a scripting forum and not a Microsoft Access forum.
2) You fail to say what errors you are getting.
4) You have HTML code in your VBA code which will not work.
5) DAO is not longer used Access.  We would use ADO or database tables direct.

Start by looking at your first error message and take steps to remedy it.


Free Windows Admin Tool Kit Click here and download it now
March 15th, 2014 9:41am

Start here and make this work first"

Function GetADUsers() As ADODB.Recordset

    Dim objConnection As New ADODB.Connection
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    
    Dim objCommand As New ADODB.Command
    Set objCommand.ActiveConnection = objConnection
    objCommand.Properties("Page Size") = 1000
    objCommand.CommandText = "<ldap://dc=enterprisenet,dc=org>;(objectCategory=person);adsPath;subtree"
    
    Set GetADUsers = objCommand.Execute()
    
End Function
March 15th, 2014 9:50am

Start here and make this work first"

Function GetADUsers() As ADODB.Recordset

    Dim objConnection As New ADODB.Connection
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    
    Dim objCommand As New ADODB.Command
    Set objCommand.ActiveConnection = objConnection
    objCommand.Properties("Page Size") = 1000
    objCommand.CommandText = "<ldap://dc=enterprisenet,dc=org>;(objectCategory=person);adsPath;subtree"
    
    Set GetADUsers = objCommand.Execute()
    
End Function
Free Windows Admin Tool Kit Click here and download it now
March 16th, 2014 1:41pm

There is no MsgBox in the code I posted.  What are you talking about?

March 16th, 2014 2:23pm

One other thing to not.  You cannot get a count on an AD recordset.  The value is meaningless.

I posted the code to show you how to get a recordset.  The code you are trying to sue has some issues.   The code I posted is tested in Access 2013.

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2014 2:26pm

Start here and make this work first"

Function GetADUsers() As ADODB.Recordset

    Dim objConnection As New ADODB.Connection
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    
    Dim objCommand As New ADODB.Command
    Set objCommand.ActiveConnection = objConnection
    objCommand.Properties("Page Size") = 1000
    objCommand.CommandText = "<ldap://dc=enterprisenet,dc=org>;(objectCategory=person);adsPath;subtree"
    
    Set GetADUsers = objCommand.Execute()
    
End Function
March 16th, 2014 2:29pm

Here is a link to a complete VBA module that loads an Access table from the current AD domain.

http://1drv.ms/1eHJnhj

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2014 2:30pm

Sorry I missed that as I used your code even after I changed that in the test.

You should use the local defs as I posted in the link.

March 16th, 2014 2:32pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics