473,407 Members | 2,326 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,407 software developers and data experts.

Order Command for Fields in Listbox

Hello,

How should I write an "on click" code for a cmd button to tell a field in a listbox to order itself in ascending?
Oct 12 '07 #1
10 5900
puppydogbuddy
1,923 Expert 1GB
Hello,

How should I write an "on click" code for a cmd button to tell a field in a listbox to order itself in ascending?

See this link for a step by step example with source code.

http://www.databasedev.co.uk/sort_listbox_columns.html
Oct 12 '07 #2
I attempted the code explained, but for some reason it isn't working with my form.

I also tried this, but the listbox goes blank -

Private Sub CmdButtonSortByName_Click
Dim strSQL as String
strSQL = me.listbox.rowsource & "ORDER BY FieldName;"
me.listbox.rowsource = strSQL
End Sub

Any reason you can see?
Oct 12 '07 #3
puppydogbuddy
1,923 Expert 1GB
I attempted the code explained, but for some reason it isn't working with my form.

I also tried this, but the listbox goes blank -

Private Sub CmdButtonSortByName_Click
Dim strSQL as String
strSQL = me.listbox.rowsource & "ORDER BY FieldName;"
me.listbox.rowsource = strSQL
End Sub

Any reason you can see?
Yes. On this line you need to put the actual name of the field in your application in place of FieldName.

strSQL = me.listbox.rowsource & "ORDER BY FieldName;"
Oct 12 '07 #4
Jim Doherty
897 Expert 512MB
Hello,

How should I write an "on click" code for a cmd button to tell a field in a listbox to order itself in ascending?
There are various techniques to do this.

A Listbox can have X number of columns and you might wish to sort on any one of them 'ascending' or 'descending'. In this simple case ascending could be represented by the value of TRUE conversely descending could be represented by FALSE

If we take the case of a listbox having its "Row Source Type" property set to "Table/Query" then we could have its "Rowsource" set to an SQL statement that gets fed to it dynamically at runtime.

If we take a simple example of a three column listbox where the columns values are represented by the following SQL Statement

"SELECT DISTINCTROW CustomerID, CustomerName, City FROM tblCustomers ORDER BY CustomerID ASC;"

We see here that statement would provide a list sorting the list by the CustomerID field. The idea then is to amend the ORDER BY clause at runtime so that it reflect the order in which we wish to list to be in either ASC for ascending or DESC for descending.

In addition it might not be the CustomerID field we wish to sort on but the CustomerName ...again either ASC or DESC.

The idea then is to manipulate and change the "ORDER BY.....ASC" block string to reference the field we wish to sort on. So how can this be done.

We could set a variable at form level to represent the TRUE or FALSE value of ASC or DESC and then 'switch' the value of the variable each time the button is clicked.

But how do we know which field to sort on? we can do this by mounting a dropdown (combobox) control on the form and setting its "Row Source Type" to 'Field List'. You might place this combobox above the listbox so that when the user clicks on the combox they see the same fieldnames as shown in the listbox. Immediately to the right of this combobox you could have a small command button the default caption of which says "Sort" on opening the form.

Each time we click the command button then... we want the list to sort dependant on the value of the fieldname selected in the combobox. In addition each 'subsequent' click of the command button we would want it to alternately sort the column value ascending or descending every time.

The code behind the button would be relying on a 'function' to do the work of manipulating the list. (This function COULD be generic by nature if we wanted it to be, but for the purposes of clarity I am going to show you as dealing with one particular SQL statement and displaying the resultant dataset.)

So lets make this happen.....


1) Create a table called tblCustomers with the fieldnames CustomerID,CustomerName,City

2) Populate it with some data

3) Create a form and mount on it a Listbox with the name lstMyList and set the column count property for the listbox to 3
4) In the "row source type" of the listbox set it to "Table/Query"
5) In the "row source" of the listbox paste in the following

"SELECT DISTINCTROW CustomerID, CustomerName, City FROM tblCustomers ORDER BY CustomerID ASC;"

4) Save the Form
5) Create a combobox and place it nearto the listbox name it Combo1
6) In the "row source type" of the combo box set it to "Field List"
7) In the "row source" of the combobox select tblCustomer
8) Mount a small command button immediately to the right of the combobox. Set the Name property of the command button to cmdsort
9) Set the caption property of the command button to "Sort"

In form design go into the code module for the newly created form (menubar...view...code)

Immediately below the statement that says "Option Compare Database" paste the following code:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim SORT_FLAG As Boolean
  3. Private Function ListOrder(c As Control, str As String) As Integer
  4. On Error Resume Next
  5. Dim mystr As String
  6. If IsEmpty(SORT_FLAG) Then SORT_FLAG = True
  7. mystr = "SELECT DISTINCTROW OrderID, Client, PositionTitle "
  8. mystr = mystr & "FROM Orders "
  9. mystr = mystr & "ORDER BY " & str & " " & IIf(SORT_FLAG, "ASC", "DESC")
  10. c.RowSource = mystr
  11. c.Requery
  12. Screen.ActiveControl.Caption = IIf(SORT_FLAG, "v", "^")
  13. SORT_FLAG = Not SORT_FLAG
  14. End Function
  15. Private Sub cmdsort_Click()
  16. On Error Resume Next
  17. Dim x
  18. If IsNull(Me!Combo1) Then
  19. MsgBox "You must select a fieldname from the list", vbExclamation, "System Message"
  20. Exit Sub
  21. End If
  22. x = ListOrder(Me!List0, Me!Combo1)
  23. End Sub
Click on Save and close the module window. and save the form. If you have done this correctly you will see the words "Event procedure" in the onclick properties for the command button cmdsort in design view.

Open the form in form view and you should be seeing a list of customers and a selectable list of fields from the drop down combobox. Each time you click on the command button it should sort the list dependant on the field name value you have selected from the dropdown.

This example is by no means the only way or even the most efficient way of doing things or indeed the most functional way but you can see that for a couple of clicks worth you have a sortable list of all of your selectable fields as opposed to just one hard coded into a single event

Hope this helps you

Regards

Jim :)
Oct 12 '07 #5
Yes. On this line you need to put the actual name of the field in your application in place of FieldName.

strSQL = me.listbox.rowsource & "ORDER BY FieldName;"

The code I showed you was the original. I did change the field name as you suggested, but it doesn't work.
Oct 12 '07 #6
puppydogbuddy
1,923 Expert 1GB
The code I showed you was the original. I did change the field name as you suggested, but it doesn't work.
You did not provide much info, but I think your problem is that you did not download the sample database from the link at the bottom of the web page to get the code for the button click event, which is not shown on the web page at all. The button click event contains the code that calls and passes a parameter to the private function code shown in the link. The passed parameter identifies which button was clicked. So you need to download the sample database from the link shown at the bottom of the page and get the code for the button click event.

If you have any problem after downloading the sample database, let me know.
Oct 12 '07 #7
You did not provide much info, but I think your problem is that you did not download the sample database from the link at the bottom of the web page to get the code for the button click event, which is not shown on the web page at all. The button click event contains the code that calls and passes a parameter to the private function code shown in the link. The passed parameter identifies which button was clicked. So you need to download the sample database from the link shown at the bottom of the page and get the code for the button click event.

If you have any problem after downloading the sample database, let me know.
Actually I did download the sample database provided in the link. I wasn't able to get that to work on my db.

No worries though. I got it to work this way....

1. Copy the Row Source from the Listbox Properties.

2. Create a command button.

3. Create an "On Click" Event for that button.

4. Enter the following into the module - listboxName.RowSource = "****Paste the Row Source data here (removing the semicolon if there is one)**** Order By [tblName].[fldName]"

5. If you want it in descending order, just add DESC to the end of the script before the quotation.
Oct 12 '07 #8
puppydogbuddy
1,923 Expert 1GB
Actually I did download the sample database provided in the link. I wasn't able to get that to work on my db.

No worries though. I got it to work this way....

1. Copy the Row Source from the Listbox Properties.

2. Create a command button.

3. Create an "On Click" Event for that button.

4. Enter the following into the module - listboxName.RowSource = "****Paste the Row Source data here (removing the semicolon if there is one)**** Order By [tblName].[fldName]"

5. If you want it in descending order, just add DESC to the end of the script before the quotation.

Glad you got it resolved and thanks for posting back. If I understand you correctly, the reason you could not get the code from the link (see below) to work was that you did not have anything defined for the row source of the listbox, and because the row source was empty, an empty string was assigned to the strSQL variable.
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdButtonSortByName_Click
  2. Dim strSQL as String
  3. strSQL = me.listbox.rowsource & "ORDER BY FieldName;"
  4. me.listbox.rowsource = strSQL
  5. End Sub
Oct 12 '07 #9
NeoPa
32,556 Expert Mod 16PB
Glad you got it resolved and thanks for posting back. If I understand you correctly, the reason you could not get the code from the link (see below) to work was that you did not have anything defined for the row source of the listbox, and because the row source was empty, an empty string was assigned to the strSQL variable.
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdButtonSortByName_Click
  2. Dim strSQL as String
  3. strSQL = me.listbox.rowsource & "ORDER BY FieldName;"
  4. me.listbox.rowsource = strSQL
  5. End Sub
...Not to mention you're not leaving a space before the "ORDER BY..." part. This will tend to muck up the resulting SQL ;)
Oct 12 '07 #10
Jim Doherty
897 Expert 512MB
There are various techniques to do this.

A Listbox can have X number of columns and you might wish to sort on any one of them 'ascending' or 'descending'. In this simple case ascending could be represented by the value of TRUE conversely descending could be represented by FALSE

If we take the case of a listbox having its "Row Source Type" property set to "Table/Query" then we could have its "Rowsource" set to an SQL statement that gets fed to it dynamically at runtime.

If we take a simple example of a three column listbox where the columns values are represented by the following SQL Statement

"SELECT DISTINCTROW CustomerID, CustomerName, City FROM tblCustomers ORDER BY CustomerID ASC;"

We see here that statement would provide a list sorting the list by the CustomerID field. The idea then is to amend the ORDER BY clause at runtime so that it reflect the order in which we wish to list to be in either ASC for ascending or DESC for descending.

In addition it might not be the CustomerID field we wish to sort on but the CustomerName ...again either ASC or DESC.

The idea then is to manipulate and change the "ORDER BY.....ASC" block string to reference the field we wish to sort on. So how can this be done.

We could set a variable at form level to represent the TRUE or FALSE value of ASC or DESC and then 'switch' the value of the variable each time the button is clicked.

But how do we know which field to sort on? we can do this by mounting a dropdown (combobox) control on the form and setting its "Row Source Type" to 'Field List'. You might place this combobox above the listbox so that when the user clicks on the combox they see the same fieldnames as shown in the listbox. Immediately to the right of this combobox you could have a small command button the default caption of which says "Sort" on opening the form.

Each time we click the command button then... we want the list to sort dependant on the value of the fieldname selected in the combobox. In addition each 'subsequent' click of the command button we would want it to alternately sort the column value ascending or descending every time.

The code behind the button would be relying on a 'function' to do the work of manipulating the list. (This function COULD be generic by nature if we wanted it to be, but for the purposes of clarity I am going to show you as dealing with one particular SQL statement and displaying the resultant dataset.)

So lets make this happen.....


1) Create a table called tblCustomers with the fieldnames CustomerID,CustomerName,City

2) Populate it with some data

3) Create a form and mount on it a Listbox with the name lstMyList and set the column count property for the listbox to 3
4) In the "row source type" of the listbox set it to "Table/Query"
5) In the "row source" of the listbox paste in the following

"SELECT DISTINCTROW CustomerID, CustomerName, City FROM tblCustomers ORDER BY CustomerID ASC;"

4) Save the Form
5) Create a combobox and place it nearto the listbox name it Combo1
6) In the "row source type" of the combo box set it to "Field List"
7) In the "row source" of the combobox select tblCustomer
8) Mount a small command button immediately to the right of the combobox. Set the Name property of the command button to cmdsort
9) Set the caption property of the command button to "Sort"

In form design go into the code module for the newly created form (menubar...view...code)

Immediately below the statement that says "Option Compare Database" paste the following code:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim SORT_FLAG As Boolean
  3. Private Function ListOrder(c As Control, str As String) As Integer
  4. On Error Resume Next
  5. Dim mystr As String
  6. If IsEmpty(SORT_FLAG) Then SORT_FLAG = True
  7. mystr = "SELECT DISTINCTROW OrderID, Client, PositionTitle "
  8. mystr = mystr & "FROM Orders "
  9. mystr = mystr & "ORDER BY " & str & " " & IIf(SORT_FLAG, "ASC", "DESC")
  10. c.RowSource = mystr
  11. c.Requery
  12. Screen.ActiveControl.Caption = IIf(SORT_FLAG, "v", "^")
  13. SORT_FLAG = Not SORT_FLAG
  14. End Function
  15. Private Sub cmdsort_Click()
  16. On Error Resume Next
  17. Dim x
  18. If IsNull(Me!Combo1) Then
  19. MsgBox "You must select a fieldname from the list", vbExclamation, "System Message"
  20. Exit Sub
  21. End If
  22. x = ListOrder(Me!List0, Me!Combo1)
  23. End Sub
Click on Save and close the module window. and save the form. If you have done this correctly you will see the words "Event procedure" in the onclick properties for the command button cmdsort in design view.

Open the form in form view and you should be seeing a list of customers and a selectable list of fields from the drop down combobox. Each time you click on the command button it should sort the list dependant on the field name value you have selected from the dropdown.

This example is by no means the only way or even the most efficient way of doing things or indeed the most functional way but you can see that for a couple of clicks worth you have a sortable list of all of your selectable fields as opposed to just one hard coded into a single event

Hope this helps you

Regards

Jim :)
Correction on my earlier code posting
My apologies The SQL block should NOT read

Expand|Select|Wrap|Line Numbers
  1.  
  2. mystr = "SELECT DISTINCTROW OrderID, Client, PositionTitle "
  3. mystr = mystr & "FROM Orders "
  4. mystr = mystr & "ORDER BY " & str & " " & IIf(SORT_FLAG, "ASC", "DESC")
  5.  
and should be amended to read

Expand|Select|Wrap|Line Numbers
  1.  
  2. mystr = "SELECT DISTINCTROW CustomerID, CustomerName, City "
  3. mystr = mystr & "FROM tblCustomers "
  4. mystr = mystr & "ORDER BY " & str & " " & IIf(SORT_FLAG, "ASC", "DESC")
  5.  
Oct 12 '07 #11

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

Similar topics

8
by: Bill | last post by:
I'm trying to create a wizardlike interface using a couple listboxes. I know you've seen it before. You double click on an item in one listbox and it "moves" it to the other. I used to approach...
1
by: melanie | last post by:
Hi, I open a recordset with a SQL query as its source. Then, i set the listbox.recordset = to the recordset i just opened. But, the fields appear in the listbox columns in a different order...
4
by: Hardy Wang | last post by:
Hi I have a ListBox control in my ASP.NET page. After I binding data to this control, I would like to be able to change this display order of items in this control. Just like change layout...
0
by: Jason Freeman | last post by:
Hi, (Cross-posted to microsoft.public.dotnet.framework.adonet) I've got a form with a listbox and a textbox on it. Both are bound to a DataView, which is sorted on the "Name" field of the...
0
by: Richard Fennell | last post by:
I am trying to use asp:listbox on a form, when a user selects a row in the asp:listbox a panel is displayed on the centre of the screen with a message, a bit like a model dialog on the form. The...
6
by: segue | last post by:
Hi; I need to change the order of a listbox array from a form app where I select order up or down. What's a good way to do that? Regards; Segue
13
by: Kevin | last post by:
Hi, I have used ASP for years using MS Access and have used MSSQL quite a lot as well. I have never came across something like this before. MSSQL table names and types: ProductName nvarchar...
3
by: oc20 | last post by:
I would like to know if theres a possible way to show two fields in a listbox. Right now i show one field of a table in the listbox (the values showed on the listbox depend on what the user type in...
1
by: aavelar | last post by:
I've created a listbox on a form to display a list of fields to be used to sort on a report. When users select from the listbox it sorts them in a number order. Is it possible to have it sort in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.