1

我正在尝试使用 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 & ")"

问题是由于更容易理解和更容易形成列表,我应该使用什么。有可能这样做吗?

4

1 回答 1

0

我找到了问题的答案——由于 LDAP 体系结构的限制,我想做的操作是不可能的。

有一篇非常好的文章,描述了查询 LDAP 时不应期待的内容:http: //mysqldump.azundris.com/archives/74-LDAP-is-not-relational.html

于 2013-07-14T14:47:14.093 回答