我有一个2013 Access DB。使用VBA,我使用ADODB查询公司的AD以获取ADODB.Recordset。我想用这个ADODB.Recordset创建一个表。现在我必须迭代ADODB.Recordset,但对于许多记录来说这很慢。我想知道是否有更简单的方法将整个ADODB.Recordset转储到表中?
和/或 - 我看了但找不到任何东西 - 也许有一种方法可以使用Access直接查询AD,而不需要VBA,在这种情况下我可以写一个只会查询创建一个表......
' variables
Dim ADConnection As New ADODB.Connection
Dim ADCommand As New ADODB.Command
Dim Recordset As ADODB.Recordset
Dim UserID, Password As String
' login information
UserID = ""
Password = ""
' prepare connection
ADConnection.ConnectionTimeout = 600
ADConnection.Provider = "ADSDSOObject"
ADConnection.Properties("User ID") = UserID
ADConnection.Properties("Password") = Password
ADConnection.Properties("Encrypt Password") = True
ADConnection.Properties("ADSI Flag") = 1
ADConnection.Open "Active Directory Provider", UserID, Password
' prepare command
Set ADCommand.ActiveConnection = ADConnection
ADCommand.Properties("Page Size") = 1000
ADCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 'ADS_SCOPE_SUBTREE = 2
ADCommand.Properties("Timeout") = 600
ADCommand.Properties("Cache Results") = True
ADCommand.Properties("Chase referrals") = ADS_CHASE_REFERRALS_ALWAYS 'ADS_CHASE_REFERRALS_ALWAYS = 96
ADCommand.CommandTimeout = 600
' set query
ADCommand.CommandText = "<GC://DC=company,DC=com>;(&(ObjectCategory=person)(ObjectClass=user)(sAMAccountName=userid));name,mail;subtree"
' execute query
Set Recordset = ADCommand.Execute
' make sure we got something back
If Not Recordset Is Nothing Then
' move to the first record
Recordset.MoveFirst
' !!!!!
' !!!!!
' !!!!!
' !!!!!
' !!!!! loop over every record
' !!!!! this is the part that can be slow
' !!!!! instead of the loop, I'd rather just create a table with the Recordset
Do Until Recordset.EOF
' do something with the current recordset
Debug.Print Recordset.Fields("name").Value
Debug.Print Recordset.Fields("mail").Value
' move to the next record
Recordset.MoveNext
Loop
End If
' close the record
Recordset.Close
' clean up
Set Recordset = Nothing
If Not ADCommand.ActiveConnection Is Nothing Then
If ADCommand.ActiveConnection.State <> 0 Then
ADCommand.ActiveConnection.Close
End If
End If
Set ADCommand = Nothing