How to code and alpha sort

D

Dave

Access 2007

How do I code an A-Z sort on a form?
Say the field is "LastName".

I want to put this code on a button(s) so the used can sort one column or
another.

thanks

dave
 
F

fredg

Access 2007

How do I code an A-Z sort on a form?
Say the field is "LastName".

I want to put this code on a button(s) so the used can sort one column or
another.

thanks

dave

One column or the other. The Field (columns) names are fixed or do you
wish the user to select any field and sort by that (and why can't they
use the already in place A-Z or Z-A toolbuttons)?

If the Field name is fixed (LastName]....
Add a command button to the form.
Code it's Click event:
Me.OrderBy = "LastName"
Me.OrderByOn = True

If the user can select any field...
Me.OrderBy = Me.PreviousControl.Name
Me.OrderByOn = True

The user will place the cursor in the field to be sorted on.
Then clicks the command button.

Since Ascending is the default sort order, you do not need to
explicitly write it.
If you wish to sort Z-A then add to each of the above:
& " Desc"
 
D

Dave

Yep - I was not very clear in my explanation
BUT
You gave me exactly what I needed.

Thanks much

dave

fredg said:
Access 2007

How do I code an A-Z sort on a form?
Say the field is "LastName".

I want to put this code on a button(s) so the used can sort one column or
another.

thanks

dave

One column or the other. The Field (columns) names are fixed or do you
wish the user to select any field and sort by that (and why can't they
use the already in place A-Z or Z-A toolbuttons)?

If the Field name is fixed (LastName]....
Add a command button to the form.
Code it's Click event:
Me.OrderBy = "LastName"
Me.OrderByOn = True

If the user can select any field...
Me.OrderBy = Me.PreviousControl.Name
Me.OrderByOn = True

The user will place the cursor in the field to be sorted on.
Then clicks the command button.

Since Ascending is the default sort order, you do not need to
explicitly write it.
If you wish to sort Z-A then add to each of the above:
& " Desc"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top