?
Solved

Why does listbox contents disappear after trying to sort?

Posted on 2009-04-11
6
Medium Priority
?
430 Views
Last Modified: 2013-11-28
Hi,
I am working in Access 2007, and I am trying to order a listbox.  I followed an example I found online, but when I try the button used, the listbox contents disappear.

I am using a function to carry out the orderby functionality.

Here is my code:
Private Function lst_ProductClassDiscountsOrderBy(col As String) As Integer
Dim strSQL As String
 
    strSQL = "SELECT SELECT int_ProductClassPlantID, str_ProductClass, int_PlantID, Discount1, Discount2"
    strSQL = strSQL & "FROM tbl_ProductClassDiscounts "
    strSQL = strSQL & "ORDER BY " & col
    Me!lst_ProductClassDiscounts.RowSource = strSQL
    Me!lst_ProductClassDiscounts.Requery
 
End Function
 
 
Private Sub cmd_OrderByProductClass_Click()
 
Dim response As Integer
 
response = lst_ProductClassDiscountsOrderBy("str_ProductClass")
 
 
End Sub

Open in new window

0
Comment
Question by:bmav25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Assisted Solution

by:CraigYellick
CraigYellick earned 400 total points
ID: 24122828
If that code is literally what you are running, there are two SELECT keywords resulting in an invalid query.

strSQL = "SELECT SELECT int_ProductClassPlantID ..."
0
 

Author Comment

by:bmav25
ID: 24122856
Oh, sorry.  I edited that, but I am still getting the same result.
0
 
LVL 5

Accepted Solution

by:
Mike77 earned 1200 total points
ID: 24122906
There is also a space missing at the end of Discount2 :

Change :
    strSQL = "SELECT SELECT int_ProductClassPlantID, str_ProductClass, int_PlantID, Discount1, Discount2"

For :
    strSQL = "SELECT SELECT int_ProductClassPlantID, str_ProductClass, int_PlantID, Discount1, Discount2 "

str_ProductClass is the name of your Field right?
0
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 
LVL 5

Expert Comment

by:Mike77
ID: 24122913
I should have taken one of the 2 SELECTS lol ...
    strSQL = "SELECT int_ProductClassPlantID, str_ProductClass, int_PlantID, Discount1, Discount2 "
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 400 total points
ID: 24122915
As a side note, you do not need

Me!lst_ProductClassDiscounts.Requery

The list box will automatically be requeried when the Row Source is set.  This is redundant.

Make this change:

Private Function lst_ProductClassDiscountsOrderBy(col As Integer) As Integer
Dim strSQL As String

Pass the 'col' as a number ... and the number should be the number of the field - left to right - that you want to sort by.  A generic SQL that does this would look like:

SELECT Table1.FIELD1, Table1.FIELD2, Table1.FIELD3
FROM Table1
ORDER BY 2;

In this example, the sort would be by FIELD2.

mx


0
 

Author Comment

by:bmav25
ID: 24122946
Thanks Mike77.

I overlooked something very minor.  I appreciate your help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question