Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reverse sort order in a combo box? please help (vb6)

Status
Not open for further replies.

Skorkpup

Programmer
Feb 17, 2005
11
Hi Guys,
I am trying to list dates in a combo box.
the dates are added, year / month / day like this..

2005 10 04
2005 10 06
2005 10 05


the combo box with (Sort = true) will display them as..

2005 10 04
2005 10 05
2005 10 06

This is all well and good, except the recent dates are added the the bottom of the list, which could get excessive.
I would like to reverse the order so the most recent date comes to the top. Any help would be appreciated greatly.



Skorkpup
 
Can you sort the dates in descending order before populating the combo box? This is how I would approach it.



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
sounds good george, but how?

Skorkpup
 
Where is the data coming from? Is it a database?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Sorry , no its just a variable with text

TodaysDate = Format(Now, "yyyy mm dd ")
Combo1.AddItem TodaysDate

Skorkpup
 
presumably, you want the user to be able to select various dates.

I suggest you use a DatePicker control. It provides a nice interface for selecting dates.

In your VB project Click Project -> Components
Scroll down to 'Microsoft Windows Common Controls-2 6.0'
Put a check next to it. Click OK.

You'll notice that there are a couple new controls you can put on the form.

Put a DTPicker control on your form.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Again soory, its not that simple.


TodaysDate = Format(Now, "yyyy mm dd ")
Combo1.AddItem TodaysDate

TodaysDate is also a .txt file containing text to populate a listview.

When a user clicks on a date displayed in the combo box the listview is populated with the text file to show the information.

Everything works fine, but the sort order is making me nuts.

Even If I could get the combo box to populate itself to the top would be fine but new entries are placed at the bottom of the combo list.



Skorkpup
 
In that case you could read all the data in the combo box into an array, sort the array, clear the combo box and add the reverse sort order back into the box.

I think that is the only way to do it with the Drop Down.

Another suggestion is to use a Data Grid and have it emulate a Drop down Combo. I have done this before. Basically you set the Datagrid with Data and reverse sort it. Then Size it to the size of a combo box, when the Datagrid gets focus you resize it larger (height). Then when it looses focus or they click something you make it small again.

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
I agree with Casper. When you need to add an item to the combo box, get the existing data, and your new item, sort the data descending, clear the combo, and then add the items.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks guys. Well, its back to the books to figure out how to do this.

Skorkpup
 
Rather than using an Array I would use a recordset object to do it.

Code:
Dim rs As ADODB.Recordset

Set rs = New ADODB.Recordset

rs.Fields.Append "MyDate", adVarchar, 10
rs.Open

rs.AddNew
rs("MyDate") = "2005 10 04"

rs.AddNew
rs("MyDate") = "2005 10 06"

rs.AddNew
rs("MyDate") = "2005 10 05"

rs.Sort = "MyDate DESC"

rs.MoveFirst
Combo1.Sort = False 'Make sure you have this set, likely at design time
Do while Not rs.EOF
    Combo1.AddItem rs("MyDate")
    rs.MoveNext
Loop

rs.Close
Set rs = Nothing
 
Since the combo is already sorted, you 'simply' need to insert an item at the top. Unless someone else knows a better way, here is how I would do it.

Code:
Public sub AddComboItem(byval ItemToAdd as string)
  Dim Items() as String
  Dim i as long

  Redim Items(combo.Listcount)
  Items(0) = ItemToAdd
  for i= 0 to combo.ListCount - 1
    items(i+1) = combo.list(i)
  next

  combo.Clear

  For i = lBound(Items) to uBound(Items)
    Call Combo.AddItem(Items(i))
  Next
End Sub

Of course bjd4jc's method is better because with my method, if an item is 'out of order' originally, it will stay out of order. With bjd4jc's method, you are guaranteed to get the proper sort order.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
bjd4jc's solution is almost 100% in line with what I was going to suggest ... s I'll just keep quiet about disconnected recordsets and how usefult they are ...
 
Just a question with the recordset method though. While deployment, Skorkpup will also have to deploy ADO along with his install package? I think that is a little bulky for just reversing sortorder in a combobox (If he is not using it already..).

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Thing is that MDAC has been part of the OS since the release of Windows 2000 (ADO 2.5), so you don't necessarily need to deploy it if your target machines are W2K, XP, W2K3, or Vista ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top