• Sorting Information in a RecordSet (Word 2003 VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Sorting Information in a RecordSet (Word 2003 VBA)

    Author
    Topic
    #409621

    Hi, I have a recordset that I pull in from ActiveDirectory into a combobox. This works great now, but it doesn’t come in in alphabetical order. There is a .Sort property which I thought would work, and is used in some sample code on MSDN, but when I try to compile it I get an error indicating Invalid Property. Following is the code I am using:

    Private Sub GetUserList()
    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim oRoot As IADs
    Dim oDomain As IADs
    Dim sBase As String
    Dim sFilter As String
    Dim sDomain As String
    Dim sAttribs As String
    Dim sDepth As String
    Dim sQuery As String
    Dim user As IADsUser

    On Error GoTo ErrHandler:

    Set oRoot = GetObject(“LDAP://rootDSE”)
    ‘Work in the default domain
    sDomain = oRoot.Get(“defaultNamingContext”)
    Set oDomain = GetObject(“LDAP://” & sDomain)
    sBase = “”
    ‘Get record set
    sFilter = “(&(objectCategory=person)(objectClass=user)(extensionattribute1=*))”
    sAttribs = “extensionattribute1,Description”
    sDepth = ADS_SCOPE_SUBTREE

    sQuery = sBase & “;” & sFilter & “;” & sAttribs & “;” & sDepth

    conn.Open “Data Source=Active Directory Provider;Provider=ADsDSOObject”

    Set rs = conn.Execute(sQuery)

    rs.CursorLocation = adUseClient
    rs.Sort “extensionattribute1” ‘(ASC is the default, which is what I want here)

    On Error Resume Next
    rs.MoveFirst
    While Not rs.EOF
    cboAuthor.AddItem rs.Fields(0).Value
    rs.MoveNext
    Wend

    RoutineExit:
    Exit Sub

    ErrHandler:

    On Error Resume Next
    If Not rs Is Nothing Then
    If rs.State 0 Then rs.Close
    Set rs = Nothing
    End If

    If Not conn Is Nothing Then
    If conn.State 0 Then conn.Close
    Set conn = Nothing
    End If

    Set oRoot = Nothing
    Set oDomain = Nothing

    Resume RoutineExit

    End Sub

    Any ideas why VBA won’t take the property which it even brings up using its AutoComplete feature? Arrgghh!

    Viewing 1 reply thread
    Author
    Replies
    • #874473

      (Edited by jscher2000 on 07-Sep-04 21:34. Fixed something dumb.)

      Try changing your sequence just a bit:

      Replace this:

        Set rs = conn.Execute(sQuery)

        rs.CursorLocation = adUseClient
        rs.Sort “extensionattribute1” ‘(ASC is the default, which is what I want here)
        [/list]With this:

        With rs
        .CursorLocation = adUseClient
        .Open sQuery, conn, adOpenStatic, adLockReadOnly, adCmdText
        .Sort “extensionattribute1” ‘(ASC is the default, which is what I want here)
        End with[/list]Does it help?
      • #876578

        Bummer, no, I tried it and it still stops on .Sort and gives me the ‘Invalid use of property’ error when I compile. Is it possible that I need a reference of some sort? I didn’t think so, but I’m grasping at straws now!

        • #876985

          Hi,
          As Sort is a property and not a method, you need to use:
          rs.Sort = “extensionattribute1”
          Hope that helps.

          • #877250

            Elvis (aka Rory) you’re beautiful!!! kiss
            Thank you SO much! It works wonderfully!

          • #877251

            Elvis (aka Rory) you’re beautiful!!! kiss
            Thank you SO much! It works wonderfully!

        • #876986

          Hi,
          As Sort is a property and not a method, you need to use:
          rs.Sort = “extensionattribute1”
          Hope that helps.

      • #876579

        Bummer, no, I tried it and it still stops on .Sort and gives me the ‘Invalid use of property’ error when I compile. Is it possible that I need a reference of some sort? I didn’t think so, but I’m grasping at straws now!

    • #874474

      (Edited by jscher2000 on 07-Sep-04 21:34. Fixed something dumb.)

      Try changing your sequence just a bit:

      Replace this:

        Set rs = conn.Execute(sQuery)

        rs.CursorLocation = adUseClient
        rs.Sort “extensionattribute1” ‘(ASC is the default, which is what I want here)
        [/list]With this:

        With rs
        .CursorLocation = adUseClient
        .Open sQuery, conn, adOpenStatic, adLockReadOnly, adCmdText
        .Sort “extensionattribute1” ‘(ASC is the default, which is what I want here)
        End with[/list]Does it help?
    Viewing 1 reply thread
    Reply To: Sorting Information in a RecordSet (Word 2003 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: