Solved

Why does listbox contents disappear after trying to sort?

Posted on 2009-04-11
6
419 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
6 Comments
 
LVL 11

Assisted Solution

by:CraigYellick
CraigYellick earned 100 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 300 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 100 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now