Results 1 to 13 of 13

Thread: Sort Recordset

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    273

    Sort Recordset

    Hello everyone how are you?

    I wonder is anybody had a Sort sample to sort recordset in a database? I need to build a program to sort my recordset in diferent orders.

    Thank you, and have nice day!
    mannyso

  2. #2
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    "Select F1,F2,F3 from Table1 where F2=5 Order By F1"
    Roy

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    273
    May be you misunderstand me I need to build a code in visual basic in order that the user can use it any time
    mannyso

  4. #4
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    So, I just showed how tio do that. The only difference will be that you will need to replace F1, etc , Table1 with your real field/table name and open recordset using your SQL statement:
    rstMyRecordset.Open strMySQL, ...
    Roy

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    You can display the data in a grid or listview, and let the user sort the data at will...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  6. #6
    Addicted Member -=SC@RF@C3=-'s Avatar
    Join Date
    Apr 2002
    Location
    Somewhere in the middle
    Posts
    158
    select statement order by would do

  7. #7
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    if you insist on sorting a RECORDSET... then
    VB Code:
    1. MyRec.Recordsets(0).Sort="MyField"


    But
    1. It's slow
    2. It has a lot of limitations and considerations to be kept in view.. (like doesn't work on Forward-only recordset)

  8. #8
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    What the heck are talking about?! You just have to send SQL statement with Order By clause as I and SC@RF@C3 pointed out.
    Roy

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I agree with moinkhan there is no need to make another trip to the db when the recordset object has a sort method.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by moinkhan
    if you insist on sorting a RECORDSET... then
    VB Code:
    1. MyRec.Recordsets(0).Sort="MyField"


    But
    1. It's slow
    2. It has a lot of limitations and considerations to be kept in view.. (like doesn't work on Forward-only recordset)
    3. Can't sort on more than one field.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    I agree with moinkhan there is no need to make another trip to the db when the recordset object has a sort method.
    Sure, but it only works on 1 (one) field. Using Order By clause will give you flexibilty of sorting on the multiple fields and it's much faster anyway.
    Roy

  12. #12

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    273
    I got this example from microsoft, bu this is not working, it does not replace my fisical table.

    Dim dbs As Database
    Dim rs As Recordset
    Dim rsSort As Recordset

    Set dbs = OpenDatabase("C:\Address\Address.mdb")
    Set rs = dbs.OpenRecordset("A", dbOpenDynaset)

    With rs
    SortOutput "Original Recordset:", rs
    .Sort = "LastName, FirstName"
    ' Print report showing Sort property and record order.
    SortOutput _
    "Recordset after changing Sort property:", rs

    ' Open new Recordset from current one.
    Set rsSort = .OpenRecordset
    Set rs = rstSort

    ' Print report showing Sort property and record order.
    SortOutput "New Recordset:", rsSort


    rsSort.Close
    .Close
    End With




    dbs.Close

    End Sub

    Function SortOutput(strTemp As String, rstTemp As Recordset)

    With rstTemp
    Debug.Print strTemp
    Debug.Print " Sort = " & _
    IIf(.Sort <> "", .Sort, "[Empty]")

    .MoveFirst

    ' Enumerate Recordset.
    Do While Not .EOF
    Debug.Print " " & !LastName & _
    ", " & !FirstName
    .MoveNext
    Loop

    End With

    End Function
    mannyso

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width