PC Review


Reply
Thread Tools Rate Thread

How do I sort a listbox by field

 
 
Bruce Rodtnick
Guest
Posts: n/a
 
      11th May 2010
I have a listbox that I've populaed using the row source

SELECT DISTINCTROW [FirstName] & " " & [LastName] AS Name, Personnel.Email,
Personnel.[Voice Part], Personnel.LastName, Personnel.FirstName
FROM Personnel
WHERE (((Personnel.Email) Is Not Null) AND ((Personnel.Status)="Active"))
ORDER BY Personnel.LastName, Personnel.FirstName;

I have three columns and I would like to be able to sort by the columns. In
other words, sort by Name, then later sort by Email or [Voice Part]. I've
seen a bunch of examples and tried them but nothing seems to work.

Bruce



 
Reply With Quote
 
 
 
 
Jeff Boyce
Guest
Posts: n/a
 
      11th May 2010
Bruce

One approach is to add command controls "above" the listbox and use the
Click event on each to set the Listbox's source to a different SQL statement
(i.e., ORDER BY x -- whatever the command button is the 'label/header' for).

Good luck!

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

"Bruce Rodtnick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a listbox that I've populaed using the row source
>
> SELECT DISTINCTROW [FirstName] & " " & [LastName] AS Name,
> Personnel.Email, Personnel.[Voice Part], Personnel.LastName,
> Personnel.FirstName
> FROM Personnel
> WHERE (((Personnel.Email) Is Not Null) AND ((Personnel.Status)="Active"))
> ORDER BY Personnel.LastName, Personnel.FirstName;
>
> I have three columns and I would like to be able to sort by the columns.
> In other words, sort by Name, then later sort by Email or [Voice Part].
> I've seen a bunch of examples and tried them but nothing seems to work.
>
> Bruce
>
>
>



 
Reply With Quote
 
Bruce Rodtnick
Guest
Posts: n/a
 
      11th May 2010
That's what I had in mind...but I'm not addressing it right...I made some
buttons with this code:

Me!lstMailTo.RowSource.OrderBy = Email
Me!lstMailTo.OrderByOn = True
Order By Email

'QuickList.RowSource = TheConstantValue
'Me.QuickList.Requery

Didn't work...

Another :

Dim strSQL As String

'set row source for list box
strSQL = ("[FirstName] & " " & [LastName] AS Name, Personnel.Email,
Personnel.[Voice Part]) _
FROM Personnel
WHERE (((Personnel.Email) Is Not Null) And ((Personnel.Status) = "Active"))
Me.lstMailTo.Recordset OrderBy = "Personnel.LastName",
"Personnel.FirstName";

Me!lstMailTo.RowSource = strSQL
Me!lstMailTo.Requery

but I'm getting all kinds of errors.

B


"Jeff Boyce" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Bruce
>
> One approach is to add command controls "above" the listbox and use the
> Click event on each to set the Listbox's source to a different SQL
> statement (i.e., ORDER BY x -- whatever the command button is the
> 'label/header' for).
>
> Good luck!
>
> Regards
>
> Jeff Boyce
> Microsoft Access MVP
>
> --
> Disclaimer: This author may have received products and services mentioned
> in this post. Mention and/or description of a product or service herein
> does not constitute endorsement thereof.
>
> Any code or pseudocode included in this post is offered "as is", with no
> guarantee as to suitability.
>
> You can thank the FTC of the USA for making this disclaimer
> possible/necessary.
>
> "Bruce Rodtnick" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I have a listbox that I've populaed using the row source
>>
>> SELECT DISTINCTROW [FirstName] & " " & [LastName] AS Name,
>> Personnel.Email, Personnel.[Voice Part], Personnel.LastName,
>> Personnel.FirstName
>> FROM Personnel
>> WHERE (((Personnel.Email) Is Not Null) AND ((Personnel.Status)="Active"))
>> ORDER BY Personnel.LastName, Personnel.FirstName;
>>
>> I have three columns and I would like to be able to sort by the columns.
>> In other words, sort by Name, then later sort by Email or [Voice Part].
>> I've seen a bunch of examples and tried them but nothing seems to work.
>>
>> Bruce
>>
>>
>>

>
>



 
Reply With Quote
 
Jeff Boyce
Guest
Posts: n/a
 
      11th May 2010
You provided the code you use to "fill" the listbox, ORDERed BY
[PersonLastName], [PersonFirstName].

Copy that SQL statement, but change the field you use in the ORDER BY clause
to reflect the column on which you wish to sort.

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

"Bruce Rodtnick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> That's what I had in mind...but I'm not addressing it right...I made some
> buttons with this code:
>
> Me!lstMailTo.RowSource.OrderBy = Email
> Me!lstMailTo.OrderByOn = True
> Order By Email
>
> 'QuickList.RowSource = TheConstantValue
> 'Me.QuickList.Requery
>
> Didn't work...
>
> Another :
>
> Dim strSQL As String
>
> 'set row source for list box
> strSQL = ("[FirstName] & " " & [LastName] AS Name, Personnel.Email,
> Personnel.[Voice Part]) _
> FROM Personnel
> WHERE (((Personnel.Email) Is Not Null) And ((Personnel.Status) =
> "Active"))
> Me.lstMailTo.Recordset OrderBy = "Personnel.LastName",
> "Personnel.FirstName";
>
> Me!lstMailTo.RowSource = strSQL
> Me!lstMailTo.Requery
>
> but I'm getting all kinds of errors.
>
> B
>
>
> "Jeff Boyce" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Bruce
>>
>> One approach is to add command controls "above" the listbox and use the
>> Click event on each to set the Listbox's source to a different SQL
>> statement (i.e., ORDER BY x -- whatever the command button is the
>> 'label/header' for).
>>
>> Good luck!
>>
>> Regards
>>
>> Jeff Boyce
>> Microsoft Access MVP
>>
>> --
>> Disclaimer: This author may have received products and services mentioned
>> in this post. Mention and/or description of a product or service herein
>> does not constitute endorsement thereof.
>>
>> Any code or pseudocode included in this post is offered "as is", with no
>> guarantee as to suitability.
>>
>> You can thank the FTC of the USA for making this disclaimer
>> possible/necessary.
>>
>> "Bruce Rodtnick" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>I have a listbox that I've populaed using the row source
>>>
>>> SELECT DISTINCTROW [FirstName] & " " & [LastName] AS Name,
>>> Personnel.Email, Personnel.[Voice Part], Personnel.LastName,
>>> Personnel.FirstName
>>> FROM Personnel
>>> WHERE (((Personnel.Email) Is Not Null) AND
>>> ((Personnel.Status)="Active"))
>>> ORDER BY Personnel.LastName, Personnel.FirstName;
>>>
>>> I have three columns and I would like to be able to sort by the columns.
>>> In other words, sort by Name, then later sort by Email or [Voice Part].
>>> I've seen a bunch of examples and tried them but nothing seems to work.
>>>
>>> Bruce
>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      11th May 2010
On Tue, 11 May 2010 13:25:26 -0500, "Bruce Rodtnick" <(E-Mail Removed)>
wrote:

>Me!lstMailTo.RowSource.OrderBy = Email
>Me!lstMailTo.OrderByOn = True
>Order By Email


A listbox (unlike a Form) does not HAVE an OrderBy property.

Do as Jeff suggested: use your code to change the listbox's RowSource property
to a SQL string which sorts as you want it.
--

John W. Vinson [MVP]
 
Reply With Quote
 
Bruce Rodtnick
Guest
Posts: n/a
 
      11th May 2010
And that's what I'm TRYING to do...This is what I have now:

Dim strSQL As String

strSQL = Me.lstMailTo.RowSource

strSQL = strSQL & " " & ORDER By Personnel.Email

Me!lstMailTo.RowSource = strSQL
Me!lstMailTo.Requery

***********

strSQL is picking up the proper RowSource but when I try to add the ORDER BY
to the strSQL I'm getting a two ORDER BYs in my strSQL...the old one and the
new one...and so I get NOTHING in the ListBox lstMailTo

B

"John W. Vinson" <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in message
news(E-Mail Removed)...
> On Tue, 11 May 2010 13:25:26 -0500, "Bruce Rodtnick"
> <(E-Mail Removed)>
> wrote:
>
>>Me!lstMailTo.RowSource.OrderBy = Email
>>Me!lstMailTo.OrderByOn = True
>>Order By Email

>
> A listbox (unlike a Form) does not HAVE an OrderBy property.
>
> Do as Jeff suggested: use your code to change the listbox's RowSource
> property
> to a SQL string which sorts as you want it.
> --
>
> John W. Vinson [MVP]



 
Reply With Quote
 
fredg
Guest
Posts: n/a
 
      11th May 2010
On Tue, 11 May 2010 14:47:05 -0500, Bruce Rodtnick wrote:

> And that's what I'm TRYING to do...This is what I have now:
>
> Dim strSQL As String
>
> strSQL = Me.lstMailTo.RowSource
>
> strSQL = strSQL & " " & ORDER By Personnel.Email
>
> Me!lstMailTo.RowSource = strSQL
> Me!lstMailTo.Requery
>
> ***********
>
> strSQL is picking up the proper RowSource but when I try to add the ORDER BY
> to the strSQL I'm getting a two ORDER BYs in my strSQL...the old one and the
> new one...and so I get NOTHING in the ListBox lstMailTo
>
> B
>
> "John W. Vinson" <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in message
> news(E-Mail Removed)...
>> On Tue, 11 May 2010 13:25:26 -0500, "Bruce Rodtnick"
>> <(E-Mail Removed)>
>> wrote:
>>
>>>Me!lstMailTo.RowSource.OrderBy = Email
>>>Me!lstMailTo.OrderByOn = True
>>>Order By Email

>>
>> A listbox (unlike a Form) does not HAVE an OrderBy property.
>>
>> Do as Jeff suggested: use your code to change the listbox's RowSource
>> property
>> to a SQL string which sorts as you want it.
>> --
>>
>> John W. Vinson [MVP]


Name is a reserved Access/VBA/Jet word and should not be used as a
field name.
For additional reserved words, see the Microsoft KnowledgeBase article
for your version of Access:

For a more complete list of reserved words, see:
http://www.allenbrowne.com/Ap****ueBadWord.html

I've taken the liberty to rename your "Name" field to "EmpName".

You also have some quotes and parenthesis in the wrong place.
Try it this way:
The SQL for the list box stays the same for all columns *Except* for
the OrderBy part?
Code each button you use to change the OrderBy like this:

Dim strSQL As String
strSQL = "Select [FirstName] & ' ' & [LastName] AS EmpName,
Personnel.Email, Personnel.[Voice Part] FROM Personnel
WHERE Personnel.Email Is Not Null And Personnel.Status = 'Active' "

Then add to each code the appropriate OrderBy clause.
If this is to order by the Email field add:
strSQL = strSQL & " OrderBy Personnel.Email;"

If this is to order by the [Voice Part] field, then use:
strSQL = strSQL & " OrderBy Personnel.[Voice Part];"

If this is to be used to order by the EmpName column:
strSQL = strSQL & " OrderBy Personnel.[LastName], Personnel.
[FirstName];"

No need to requery.

--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      12th May 2010
On Tue, 11 May 2010 14:47:05 -0500, "Bruce Rodtnick" <(E-Mail Removed)>
wrote:

>And that's what I'm TRYING to do...This is what I have now:
>
>Dim strSQL As String
>
>strSQL = Me.lstMailTo.RowSource
>
>strSQL = strSQL & " " & ORDER By Personnel.Email
>
>Me!lstMailTo.RowSource = strSQL
>Me!lstMailTo.Requery
>
>***********
>
>strSQL is picking up the proper RowSource but when I try to add the ORDER BY
>to the strSQL I'm getting a two ORDER BYs in my strSQL...the old one and the
>new one...and so I get NOTHING in the ListBox lstMailTo


The problem is that strSQL is *the whole thing* - it isn't a complex object
with an Order By property that you can replace; it's just a text string. VBA
won't have a clue what you mean by the ORDER By Personnel.Email text there;
that's SQL text, not valid VBA code.

I'd suggest using a saved query in your table, without *any* ORDER BY clause,
and use code like

strSQL = Currentdb.QueryDefs("lstMailToTemplateQuery").SQL
strSQL = strSQL & " ORDER BY Personnel.Email"
Me!lstMailTo.RowSource = strSQL

You won't need to requery it - setting its rowsource does the job.
--

John W. Vinson [MVP]
 
Reply With Quote
 
Bruce Rodtnick
Guest
Posts: n/a
 
      12th May 2010
This all make sence...but I tried it and I get a blank screen. The strSQL
shows that everything is right.

"SELECT [FirstName] & " " & [LastName] AS MbrName, Personnel.Email,
Personnel.[Voice Part]
FROM Personnel
WHERE (((Personnel.Email) Is Not Null) AND ((Personnel.Status)="active"));
ORDER BY Personnel.Email"

Is it the semi-colon before the ORDER BY that does it? How do I get rid of
that?

B


"John W. Vinson" <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in message
news:(E-Mail Removed)...
> On Tue, 11 May 2010 14:47:05 -0500, "Bruce Rodtnick"
> <(E-Mail Removed)>
> wrote:
>
>>And that's what I'm TRYING to do...This is what I have now:
>>
>>Dim strSQL As String
>>
>>strSQL = Me.lstMailTo.RowSource
>>
>>strSQL = strSQL & " " & ORDER By Personnel.Email
>>
>>Me!lstMailTo.RowSource = strSQL
>>Me!lstMailTo.Requery
>>
>>***********
>>
>>strSQL is picking up the proper RowSource but when I try to add the ORDER
>>BY
>>to the strSQL I'm getting a two ORDER BYs in my strSQL...the old one and
>>the
>>new one...and so I get NOTHING in the ListBox lstMailTo

>
> The problem is that strSQL is *the whole thing* - it isn't a complex
> object
> with an Order By property that you can replace; it's just a text string.
> VBA
> won't have a clue what you mean by the ORDER By Personnel.Email text
> there;
> that's SQL text, not valid VBA code.
>
> I'd suggest using a saved query in your table, without *any* ORDER BY
> clause,
> and use code like
>
> strSQL = Currentdb.QueryDefs("lstMailToTemplateQuery").SQL
> strSQL = strSQL & " ORDER BY Personnel.Email"
> Me!lstMailTo.RowSource = strSQL
>
> You won't need to requery it - setting its rowsource does the job.
> --
>
> John W. Vinson [MVP]



 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      12th May 2010
Yes. Get rid of the semi-colon (and make sure there's a space between the
closing parenthesis and the key word Order)

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
(no e-mails, please!)

"Bruce Rodtnick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> This all make sence...but I tried it and I get a blank screen. The strSQL
> shows that everything is right.
>
> "SELECT [FirstName] & " " & [LastName] AS MbrName, Personnel.Email,
> Personnel.[Voice Part]
> FROM Personnel
> WHERE (((Personnel.Email) Is Not Null) AND ((Personnel.Status)="active"));
> ORDER BY Personnel.Email"
>
> Is it the semi-colon before the ORDER BY that does it? How do I get rid
> of that?
>
> B
>
>
> "John W. Vinson" <jvinson@STOP_SPAM.WysardOfInfo.com> wrote in message
> news:(E-Mail Removed)...
>> On Tue, 11 May 2010 14:47:05 -0500, "Bruce Rodtnick"
>> <(E-Mail Removed)>
>> wrote:
>>
>>>And that's what I'm TRYING to do...This is what I have now:
>>>
>>>Dim strSQL As String
>>>
>>>strSQL = Me.lstMailTo.RowSource
>>>
>>>strSQL = strSQL & " " & ORDER By Personnel.Email
>>>
>>>Me!lstMailTo.RowSource = strSQL
>>>Me!lstMailTo.Requery
>>>
>>>***********
>>>
>>>strSQL is picking up the proper RowSource but when I try to add the ORDER
>>>BY
>>>to the strSQL I'm getting a two ORDER BYs in my strSQL...the old one and
>>>the
>>>new one...and so I get NOTHING in the ListBox lstMailTo

>>
>> The problem is that strSQL is *the whole thing* - it isn't a complex
>> object
>> with an Order By property that you can replace; it's just a text string.
>> VBA
>> won't have a clue what you mean by the ORDER By Personnel.Email text
>> there;
>> that's SQL text, not valid VBA code.
>>
>> I'd suggest using a saved query in your table, without *any* ORDER BY
>> clause,
>> and use code like
>>
>> strSQL = Currentdb.QueryDefs("lstMailToTemplateQuery").SQL
>> strSQL = strSQL & " ORDER BY Personnel.Email"
>> Me!lstMailTo.RowSource = strSQL
>>
>> You won't need to requery it - setting its rowsource does the job.
>> --
>>
>> John W. Vinson [MVP]

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort ListBox rn5a@rediffmail.com Microsoft ASP .NET 4 11th Jan 2007 12:52 AM
Sort a listbox by a nondisplayed object field?? Just Me Microsoft VB .NET 4 28th Aug 2004 03:23 PM
Listbox - How do I sort? Jason P Opdycke [MSFT] Microsoft ASP .NET 1 4th Jun 2004 04:35 AM
DataView sort fails to sort on fields with comma in field names Mone Hsieh Microsoft ADO .NET 1 14th May 2004 09:29 PM
listbox sort Hawk Microsoft Excel Programming 2 31st Mar 2004 05:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:46 PM.