The code below seems to run without error, on a domain computer, but nothing happens.
I need a little help getting the data into a temporary table I can use for the needed feature. I have button that runs this function, and I get a message on a non-domain computer that a domain could not be contacted, and nothing happens on the domain computer.
Basically I would like to access First Name, Last Name, username, email, etc. from AD and compare that to the same information in my app, to support importing new users to my app where employee information is also maintained.
The end result will be a button that will compare the app employee data with AD data, alert the user there are x number of new AD users, and/or x number of users no longer in AD and then facilitate the addition/removal.
All I need help with is making the AD connection and accessing the data there. The app will also potentially be used by users who have multiple domain memberships. Am I inherently limited to information of the domain the computer/user is currently logged into?
Public Function ActDirConnect()
On Error GoTo Err_ActDirConnect
Dim adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN
' Setup ADO objects.
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.OPEN "Active Directory Provider"
Set adoRecordset = CreateObject("ADODB.Recordset")
Set adoRecordset.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,cn"
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
' Run the query.
adoRecordset.Source = strQuery
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values and display.
strName = adoRecordset.Fields("sAMAccountName").Value
strCN = adoRecordset.Fields("cn").Value
'Wscript.Echo "NT Name: " & strName & ", Common Name: " & strCN
' Move to the next record in the recordset.
' Clean up.
MsgBox Err.Description & " - ActDirConnect error"