?
Solved

Why does listbox contents disappear after trying to sort?

Posted on 2009-04-11
6
Medium Priority
?
436 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:Craig Yellick
Craig Yellick 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

807 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