Results 1 to 5 of 5

Thread: Listbox sorting

  1. #1
    Join Date
    Jun 2006
    Posts
    72

    Unanswered: Listbox sorting

    Access 2003

    I use the following code to populate a listbox with the forms in the database. Is there an easy way to sort this listbox alphabetically?

    Code:
        For i = 0 To CurrentProject.AllForms.Count - 1
            DoEvents
            Temp = Temp & CurrentProject.AllForms(i).Name & ";"
        Next
        lstForms.RowSource = Left(Temp, Len(Temp) - 1)

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    there is no reason why you cannot create a sort order on a list box... however there is no sort order in a list box. you provide the order by the sequence in which rows appear in the list box's row source

    so if the source for the list box is an SQL statement.. set your sort order in the SQL

    if you are providing the row source through code (say based on user intervention then you need to make sure you insert the row and set the index when you invoke .additem method.... you would need to do a scan through the existing items in the source collection and then .additem at teh appropriate position. If you don't apply a position then the record will be inserted at the end of the collection.

    if you want to change a sort order then you will need to copy all the records to another collection, delete the records in the listbox then add back the copied records in the new order

    HTH
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Jun 2006
    Posts
    72
    In that case, since I'm not big on the idea of writing a sorting algorithm specifically for this; I'll put all the form names into a table, set the listbox to be a sorted query on that table and update the table every time the form loads.

    Thank you for answering my question. Oh also what is HTH?

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Jun 2006
    Posts
    72
    In trying to do something else I've found a better way to solve my problem here. In case anyone is interested, I now use this code to fill the combo box (I'm using a combo box instead of a list box now) and don't need to create a table anymore.

    Code:
        For i = 0 To CurrentDb.Containers("Forms").Documents.Count - 1
            Temp = Temp & CurrentDb.Containers("Forms").Documents(i).Name & ";"
        Next
        cboForms.RowSource = Temp
    The difference is that "CurrentDb.Containers("Forms").Documents(i).Na me" comes out in alphabetic order automatically.

Posting Permissions

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