You are here:

Using MS Access/list box coding for ascending or descending numbers in the box

Advertisement


Question
I have the following code that creates a list box of numbers.  How do I change the code so that the numbers are in numerical order either ascending or descending?


Option Compare Database



Private Sub Form_Load()
lstQuoteNumbers.SetFocus
SendKeys "{down}"
End Sub

Private Sub lstQuoteNumbers_GotFocus()
Me.lstQuoteNumbers.ListIndex = Me.lstQuoteNumbers.ListCount - 1
End Sub
Private Sub cmdclosequotenumbersform_Click()
On Error GoTo Err_cmdclosequotenumbersform_Click


   DoCmd.Close

Exit_cmdclosequotenumbersform_Click:
   Exit Sub

Err_cmdclosequotenumbersform_Click:
   MsgBox Err.Description
   Resume Exit_cmdclosequotenumbersform_Click
   
End Sub


Answer
Elaine, Hello,

Try setting the record source for the listbox from a table direct (eg tblQuotes) to a select statement as follows:

SELECT QuoteNumber FROM tblQuotes ORDER BY QuoteNumber;

or

SELECT QuoteNumber FROM tblQuotes ORDER BY QuoteNumber DESC;

if you want the list by the Quote Number descending

Kind regards

Geoff :-)

Using MS Access

All Answers


Answers by Expert:


Ask Experts

Volunteer


Geoff

Expertise

I specialise in database analysis and design, SQL and database queries using QBE and VBA. In my work, I use MS Access together with MS SQL Server as ETL (Extraction - Transformation - Loading) tools for migrating data between business ERP systems and data stores. My forte is building bespoke functions and applications.


See my website for example apps and downloads

Experience

I am a chartered engineer with 30 years of engineering and business experience, member of the BCS and have been working specifically in database applications, including SQL Server (v7/8/2000) for the last 9 or so years. I previously taught a course in Database Analysis and Design, but am now a freelance consultant and systems analyst.

Commercial database design and development work undertaken.

©2012 About.com, a part of The New York Times Company. All rights reserved.