我正在尝试使用 VBA 从 Excel 查询 LDAP。当我使用 OR 关键字通过 CN 获取记录时,它可以正常工作,但是使用 IN 关键字它会失败:
失败(错误 80040e14):
Function GetUsersProperty(ByVal users As String, ByVal returnField As String)
Dim usersProperty As New Collection
Set cmd = CreateObject("ADODB.Command")
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open "Provider=ADsDSOObject;"
cmd.CommandText = _
"SELECT cn, " & returnField & _
" FROM 'LDAP://" & GetObject("LDAP://RootDSE").get("defaultNamingContext") & _
"' WHERE objectClass = 'User' AND objectCategory = 'Person' AND cn IN (" & users & ")"
'assuming there are multiple users names in colons inside 'users' separated with comma
cmd.ActiveConnection = cn
Set rs = cmd.Execute
'On Error Resume Next
While rs.EOF <> True And rs.BOF <> True
usersProperty.Add Item:=rs.Fields(returnField).Value, Key:=rs.Fields("cn").Value
rs.MoveNext
Wend
Set GetUsersProperty = usersProperty
Set cmd = Nothing
Set cn = Nothing
Set rs = Nothing
End Function
如果我用WHERE
下面替换就cmd.CommandText
可以了
"' WHERE objectClass = 'User' AND objectCategory = 'Person' AND (cn = " & user1 & " OR cn = " & user2 & ")"
问题是由于更容易理解和更容易形成列表,我应该使用什么。有可能这样做吗?