473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to sort combobox data on the fly?

In Access 2003, I am trying to sort the combobox "Model". The "Model" rowsource is chosen based on the "Product Type" choice (i.e. Compressors, Actuators or Valves).

How can I code it so that each choice loaded as the rowsource will be sorted in ascending order.

I have the following code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Product_Type_AfterUpdate()
  2.     Select Case Product_Type.Value
  3.       Case "1. Compressors"
  4.          Model.RowSource = "tbl1"
  5.       Case "2. Actuators"
  6.          Model.RowSource = "tbl2"
  7.       Case "3. Valves"
  8.          Model.RowSource = "tbl3"
  9.     End Select
  10.  
  11. End Sub
  12.  
So an example is if "Compressors" is chosen in "Product Type" and "Model" rowsource = the contents of "tbl1", how can I sort those contents???

I was blindly trying to add into the code something like:

Model.RowSource = "tbl3", SORT BY

But it errors out.

Thanks!
May 11 '10 #1

✓ answered by Jim Doherty

@WannabePrgmr
A Listbox can be populated by an SQL statement that has an ORDER BY clause you use a field name of your choice in the table to sort by

This will sort ascending
Expand|Select|Wrap|Line Numbers
  1. Model.RowSource ="Select tbl1. * FROM tbl1 ORDER BY tbl1.YourChosenFieldname ASC;"
This will sort descending
Expand|Select|Wrap|Line Numbers
  1. Model.RowSource ="Select tbl1. * FROM tbl1 ORDER BY tbl1.YourChosenFieldname DESC;"
Obviously my reference to 'YourChosenFieldName' should be the actual name of the field in your table that you intend to sort by

Look at queries to see how they work and then look at how listboxes are populated conventionally using queries or SQL statements. You will realise that this can be controlled in code anyway you wish by manipulating the SQL string

6 6593
Are you trying to filter or sort one combobox based on the choice of another? Do you actually have 1 table or a table for each product type?
May 11 '10 #2
There is a table for each product type. So the case statements pull the correct table for "Model" whenever the product is selected in Product Type.

I tried to sort the tables themselves and they do sort in the table view, but not when they are listed in the combobox when they are called.

Thanks
May 12 '10 #3
Jim Doherty
897 Expert 512MB
@WannabePrgmr
A Listbox can be populated by an SQL statement that has an ORDER BY clause you use a field name of your choice in the table to sort by

This will sort ascending
Expand|Select|Wrap|Line Numbers
  1. Model.RowSource ="Select tbl1. * FROM tbl1 ORDER BY tbl1.YourChosenFieldname ASC;"
This will sort descending
Expand|Select|Wrap|Line Numbers
  1. Model.RowSource ="Select tbl1. * FROM tbl1 ORDER BY tbl1.YourChosenFieldname DESC;"
Obviously my reference to 'YourChosenFieldName' should be the actual name of the field in your table that you intend to sort by

Look at queries to see how they work and then look at how listboxes are populated conventionally using queries or SQL statements. You will realise that this can be controlled in code anyway you wish by manipulating the SQL string
May 12 '10 #4
NeoPa
32,556 Expert Mod 16PB
May I say that this problem is a result of non-normalised data. It is not recommended to have separate tables for storing data simply dependent on the values in the data. See Normalisation and Table structures.

With a properly designed database such filtering can more easily be managed by the filtering facilities provided. For more on this see Example Filtering on a Form.
May 12 '10 #5
Thanks Jim Doherty! That did it perfectly! I appreciate the help very much.

NeoPa, thank you for your input. I am learning on the fly and very much jumped in head first so there is a lot I have to learn to correctly set these databases up.
May 12 '10 #6
NeoPa
32,556 Expert Mod 16PB
You're welcome, and it's fine to ask for guidance after jumping in at the deep-end. We're always happy to help.

One thing I would stress though, is that the first link is a must-read for anyone in your position. You may (or may not actually) find it strange at first, but it is such good grounding for database comprehension that you won't regret any effort put into taking this on board.

Enjoy the process :)
May 13 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: deko | last post by:
I need to import dates from a linked table then export them out as text to an Excel spreadsheet. The problem is the dates need to be in ascending order when I dump them out to Excel - and there's...
0
by: Dmitry Karneyev | last post by:
Hi! I've got a binded combobox, currencymanager object and a SelectionChangeCommitted event handler. In event handler I retieve data from a binded field and fill other field using this...
6
by: Dany P. Wu | last post by:
Hi everyone, As usual, weekend is tinkering time for students and I'm playing with combobox databinding for the first time. Previously I have always iterated through the records and added each...
2
by: ksedran | last post by:
Hello, I have a combobox in a winform set up with the following code: da = New OracleDataAdapter("select curr_code || '=' || desc As curr_desc, curr_code from tablename", Conn) da.Fill(ds,...
8
by: sudhir | last post by:
hi My application receives daily records, I need to show it sorted if needed...but I am not finding the right direction to do that...
4
by: Peter | last post by:
C# .NET 1.1 How do I sort ComboBox in descending order? Thanks Peter
2
by: John | last post by:
Hi, Is there a way to sort a datareader based on certain columns? TIA
0
by: =?Utf-8?B?SmltSGVhdmV5?= | last post by:
I have written code to sort data retrieved from a database when using a Datagrid, but now I am wanting to sort a XML datasource and I'm not sure what I need to do this. I am guessing that I want...
1
by: JAnn81 | last post by:
I have a combo box created using xaml. I bind the item source to a xml document. <Items> <Item> <Name>David</Name> <Age>20</Age> </Item> <Item> <Name>Marcus</Name> <Age>25</Age>
2
by: shravanti88 | last post by:
How do I sort the data/records in the DataList using VB.NET? I am working on a .NET project and using a SQL database.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.