01 Function SearchAD(userName As String) As String
02 Dim objConnection As Variant
03 Dim objCommand As Variant
04 Dim objRecordSet As Variant
05 Const ADS_SCOPE_SUBTREE = 2
06
07 On Error Goto errHandler
08 Set objConnection = CreateObject("ADODB.Connection")
09 Set objCommand = CreateObject("ADODB.Command")
10 objConnection.Provider = "ADsDSOObject"
11 objConnection.Open "Active Directory Provider"
12 Set objCommand.ActiveConnection = objConnection
13 objCommand.Properties("Page Size") = 1000
14 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
15 ' Note: 使用ADS中属性查询
16 ' 查询语句,相关语句可查询MSDN中相关对象
18
19 Set objRecordSet = objCommand.Execute
20 If ( objRecordSet.RecordCount = 0 ) Then
21 SearchLDAP="0"
22 Else
23 SearchLDAP="1"
24 End If
25 objRecordSet.close
26
27 Exit Function
28errHandler:
29 Msgbox "Error is : " & Error$ & " at line number : " & Cstr(Erl) & " in SearchAD() " & " while processing user : " & userName
30 Exit Function
31End Function