Sorting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way on a form to put code on a label that when someone clicks on
the label it sorts the information on the form by that field. Example: If
you have 3 columns (First Name, Last Name, Employee ID). I want to be able
to click on the label Last Name and it will sort (without using the sort icon
- want to make it easy for my users) by that field, if I click on employee ID
it will sort on that field.

Is there a way to do this? I can't seem to find anything.

Thanks.
 
Labels do not have events, so they will not respond to a click. You can use
a text box instead and make it appear to be a label. To do that, make it's
Locked property Yes and it's Tab Stop property no. Make it's Default Value
the name of the field. Then you can use the Click event to change the sort.
In the example below

Me.Orderby = Me.txtField
Me.Orderbyon = True
 
Lisa,

Labels don't raise events, so you'll need to create a command button to
do the work. Set the command button's transparent property to Yes and
put the label behind it.

In the command button's click event, set the form's OrderBy property.
Someting like this:

Private Sub SortNameFirst_Click()
Me.OrderByOn = True
Me.OrderBy="[FirstName] ASC"
End Sub

You do the same thing for the other fields.

HTH,
Barry
 
Place a label above the EmployeeID field.
Create the Click event procedure for that label.
In that event procedure, put the following code:

me.orderby = "[EmployeeID]"
if not me.orderbyon then me.orderbyon = true

Close the form module.
Close the form design view.
Run the form.

Now when you click that label, the form should instantly re-sort by
Employee ID.

You can add other labels to sort by other fields. Naturally you would
put each label over or near to the field to which it applied.

You might choose toggle buttons instead of labels. Toggle buttons give
a visible indication of whether they've been clicked, or not.

You'd put the toggle buttons into an Option Group control, so the user
could only click one of them at a time.

Alternatively, you could amend the code so the user could click
/several/ buttons, and the form would sort successively by each field
selected; eg. by Forname, then Surname, or whatever.

Or you could enhance the code so if the use clicked a label or button
/twice/, it would sort in the reverse order: me.orderby="[EmployeeID]
DESCENDING".

HTH,
TC (MVP Access)
http://tc2.atspace.com
 
Guys, what are you on about?

Labels have a Click event (amongst others).

You do mean a Label control on a form, right? I just added a label to a
new blank form in Access 97. Then I double-clicked it (in Design view)
to see its properties. I selected the Events tab, and voila: events -
including the Click event.

I haven't checked in Access 2000, but there is no way those events
would not be present there as well, IMHO.

Yes? No?

HTH,
TC (MVP Access)
http://tc2.atspace.com
 
If a label is a child of a text (or other control), it won't have events. If
you "detach" the label from the parent control, it will then have events.
 
Sheesh, the obvious staring us all in the face!

When I proposed my solution, I had an unattached label in mind - but I
did not make that clear. (OTOH, neither did I say to attach the label
to the field.) The others clearly thought that I meant an attached
label. We should all have realized, sooner, that we were all making
different assumptions :-)

Lisa - if you're till here - my suggestion was fine, give it a go.

Cheers,
TC (MVP Access)
http://tc2.atspace.com
 
Back
Top