The attached contains some vba code I've used to search for useful information in the Active Directory.
Here are some additional comments from grafiksinc on the code in this example:
I only use a few parts of the entire thing specifically the part about getting user information from AD.
I quote another fellow
"realistically to get your import to run a bit faster , you'd probably be better off running an INSERT INTO SQL statement:"
INSERT INTO AD_User (UserName, FirstName, LastName, BusinessPhone, DisplayName, LogonName)
SELECT Name,givenName,SN,telephonenumber,displayname,sAMA ccountName
FROM 'LDAP://OU=,DC=,
WHERE objectCategory='user'"
It was true the way you wrote it worked but this did help it speed up. In my case I have about 2000+ users I am importing.
NOTE:********
I excluded the LDAP just know that you need to add in your own domain.
A few extra notes:
1. This could not work for me because the Value list in a combo box
is limited to like 30 something thousand characters.
and for 2000+ users this is reached.
But if your users are less than lets say 100 this should work fine for you.
2. I wanted to see 6 columns in AD but you can change that to whatever
suits. You can also and change the SQL to get other info that you need from AD
Private Sub lstUsers_GotFocus()
'*****************************************
'*Connects To AD and sets search criteria*
'*****************************************
'On Error Resume Next
Dim rs As ADODB.Recordset
Dim strSql As String
Const ADS_SCOPE_SUBTREE = 2
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
'************************************************* *********************
'*SQL statement on what OU to search and to look for User Objects ONLY*
'************************************************* *********************
objCommand.CommandText = _
"SELECT Name,givenName,SN,telephonenumber,displayname,sAMA ccountName " _
& "FROM 'LDAP://OU=,DC=' WHERE " _
& "objectCategory='user'"
'*************************************
'Adds records to list box
'*************************************
With Me!lstUsers
.RowSourceType = "Value List"
.ColumnCount = 6
End With
Set objrecordset = objCommand.Execute
With objrecordset
.MoveFirst
Do While Not .EOF
Me!lstUsers.AddItem .Fields("Name").Value & ";" & _
.Fields("GivenName").Value & ";" & _
.Fields("SN").Value & ";" & _
.Fields("telephonenumber").Value & ";" & _
.Fields("DisplayName").Value & ";" & _
.Fields("sAMAccountName").Value
.MoveNext
Loop
End With
objrecordset.Close
Set objrecordset = Nothing
End Sub
---------------------------------------------------------
Here is what this code will do,
On the form that you have now Add a text box for testing and name it
"last".
What the code does is, it looks at that field for the last name.
So, when you select the Combo it will search AD for only the users that
match the value that is in the text box names last.
It's faster and it only gets the data that is in use at the time.
There-fore no need to import AD users.
With all that said this is just one scenario but you could expand on
this potentially rather than have it say look at another text box but
make it look at itself possibly.
Code-------------------------------------------------------------------------------------------------------------
'*****************************************
'*Connects To AD and sets search criteria*
'*****************************************
'On Error Resume Next
Dim rs As ADODB.Recordset
Dim strSql As String
Const ADS_SCOPE_SUBTREE = 2
'On Error GoTo ADImportError
'Screen.MousePointer = 11
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
'************************************************* *********************
'*SQL statement on what OU to search and to look for User Objects ONLY*
'************************************************* *********************
objCommand.CommandText = _
"SELECT SN, GivenName, sAMAccountName " _
& "FROM 'LDAP://OU=DC=' WHERE " _
& "objectCategory='user'" _
& "AND SN='" & Me!last & "'"
'*************************************
'Adds records to list box
'*************************************
With Me!lstUsers
.RowSourceType = "Value List"
.ColumnCount = 1
End With
Set objrecordset = objCommand.Execute
With objrecordset
.MoveFirst
Do While Not .EOF
Me!lstUsers.AddItem .Fields("SN").Value
'& ";" & _
'.Fields("GivenName").Value & ";" & _
'.Fields("SN").Value & ";" & _
'.Fields("telephonenumber").Value & ";" & _
'.Fields("DisplayName").Value & ";" & _
'.Fields("sAMAccountName").Value
.MoveNext
Loop
End With
objrecordset.Close
Set objrecordset = Nothing
Exit_ADImport:
Screen.MousePointer = 0
Exit Sub
ADImportError:
MsgBox Err.Description & " - " & Err.Number & Chr(13) & Chr(13) _
& "Unable to produce picklist. Report the above error to ITD."
Resume Exit_ADImport
End Sub
See posts here:
Use form List Box to query Active Directory
for additional comments and details.
.
.