How to create combo a-z to sort list box? (1 Viewer)

Spam808

Registered User.
Local time
Today, 12:57
Joined
Dec 3, 2018
Messages
55
I would like to create a combo box that sorts a-z or z-a? How should I go about this to sort data in a list box?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:57
Joined
Oct 29, 2018
Messages
21,358
If you only have two options, it might be better to use a radio or toggle button. Just a thought...
 

Spam808

Registered User.
Local time
Today, 12:57
Joined
Dec 3, 2018
Messages
55
How is that response a help?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:57
Joined
Oct 29, 2018
Messages
21,358
Hi. It was an attempt to help make the problem easier if not the solution itself. For example, if you think about the amount of effort to create a combobox with two options as compared to using a single toggle button, which do you think would be easier? As I said though, it was just a thought. I wasn’t trying to tell you that it’s the only way to go and you must do it. As my sig below says, it’s just my 2 cents...
 

Spam808

Registered User.
Local time
Today, 12:57
Joined
Dec 3, 2018
Messages
55
How do you do this?

If I was to help, I would say "click here, and select option a. Type code to make the combo box work."

That is helping someone
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:57
Joined
May 21, 2018
Messages
8,463
If I was to help, I would say "click here, and select option a. Type code to make the combo box work."
That is helping someone
You need to shut up. You have provided nothing to this forum. Look at your Thanks (0) versus DBGuy. You want nothing but to be spoon fed. That is not the way it works. He provided good advice. So do some work or go away. You are a leach.
 

essaytee

Need a good one-liner.
Local time
Tomorrow, 06:57
Joined
Oct 20, 2008
Messages
512
I would like to create a combo box that sorts a-z or z-a? How should I go about this to sort data in a list box?

So far, all good suggestions offered.

Create an unbound combo box,
Row Source: "A-Z", "Z-A" (whatever, could be Asc, Desc)
Row Source Type: Value List

Write code in the AfterUpdate event of the combo box, that depending on the selection made, will reset the RowSource of the listbox, then in same AfterUpdate event, requery the said listbox.

This should get you started.
 

Spam808

Registered User.
Local time
Today, 12:57
Joined
Dec 3, 2018
Messages
55
Try looking up spam musubi, that what is spam.
 

Micron

AWF VIP
Local time
Today, 15:57
Joined
Oct 20, 2018
Messages
3,476
If you only have two options, it might be better to use a radio or toggle button. Just a thought...
Wondering if a lot of what follows your comment stems from a misunderstanding. I read it as "sort A to Z or sort Z to A" meaning inclusive and either ascending or descending. Then again, I have no history with the OP so please ignore this if I'm out of place.
The combo row source (sql or query with ORDER BY clause) can do the sorting, assuming the combo row source isn't just a value list.
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 12:57
Joined
Oct 29, 2018
Messages
21,358
Hi Micron. You could be correct. The way I read the OP is there’s a desire to use a dropdown with only two items in it (Ascending and Descending) with the intent to sort a Listbox based on the selection made in the Combobox. Are you saying the request is to actually have an item for each letter in the alphabet (26 rows, at least)?
 

Micron

AWF VIP
Local time
Today, 15:57
Joined
Oct 20, 2018
Messages
3,476
To me, A-Z just meant alphabetical sort rather than numeric. I figured listbox just meant combo list, otherwise I don't see a connection between a combo and a list box. Details are vague.
 

Spam808

Registered User.
Local time
Today, 12:57
Joined
Dec 3, 2018
Messages
55
theDBguy, that is correct. The combo box would be just two items base on a value list of a-z or z-a to sort a listbox alphabetically. I am assuming the only way to accomplish this would be with a requery. Is there an example or tutorial out there for this solution?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:57
Joined
Oct 29, 2018
Messages
21,358
theDBguy, that is correct. The combo box would be just two items base on a value list of a-z or z-a to sort a listbox alphabetically. I am assuming the only way to accomplish this would be with a requery. Is there an example or tutorial out there for this solution?
Hi. In the AfterUpdate event of the combobox, you could try something like:
Code:
If Me.ComboboxName="a-z" Then
    Me.Listbox.RowSource="SELECT * FROM TableName ORDER BY FieldName
Else
    Me.Listbox.RowSource="SELECT * FROM TableName ORDER BY FieldName DESC
End If
But as I was saying initially, if you use a Toggle button instead, your Click event might look something like this:
Code:
If Me.ToggleButton Then
    Me.Listbox.RowSource="SELECT * FROM TableName ORDER BY FieldName
Else
    Me.Listbox.RowSource="SELECT * FROM TableName ORDER BY FieldName DESC
End If
The difference being adding a Toggle button just seems simpler than setting up a Combobox. And for the user, it's one click versus two. Again, it's just a thought...
 

June7

AWF VIP
Local time
Today, 11:57
Joined
Mar 9, 2014
Messages
5,423
Can have code in combobox (or radio button Option Group or Toggle button) AfterUpdate event that sets the listbox RowSource. Like:

Me.listbox.RowSource = "SELECT ID, field FROM table ORDER BY field " & IIf([Combo49] = "Ascending", "", "DESC") & ";"

But what purpose does this serve? How many items are in this listbox?
 
Last edited:

Micron

AWF VIP
Local time
Today, 15:57
Joined
Oct 20, 2018
Messages
3,476
I'd use something above the listbox and cycle the sort via click or double click. Maybe a button and alter its caption accordingly. Dont see the sense in a combo with a list just to apply a sort.
 

Spam808

Registered User.
Local time
Today, 12:57
Joined
Dec 3, 2018
Messages
55
Thank you everyone for your help, and thank you theDBguy
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:57
Joined
Oct 29, 2018
Messages
21,358
Thank you everyone for your help, and thank you theDBguy
Hi. You’re welcome. We’re all happy to assist. Even if sometimes it doesn’t appear so, we do try to help. Cheers!
 

Users who are viewing this thread

Top Bottom