Toggle Sort order on Form

D

Dave

I have a frame with radio buttons on a form that sorts the underlying
recordset.

I want to be able to toggle the sort order (ASC/DESC) based on subsequent
clicks of the radio buttons.

But I can't get the frame or radio buttons to register a subsequent click of
the same control. IOW, if the date button is selected and I click it again
to change the sort order, nothing happens.

Can somone tell me how to do this?

Thanks
Dave
 
D

Dirk Goldgar

Dave said:
I have a frame with radio buttons on a form that sorts the underlying
recordset.

I want to be able to toggle the sort order (ASC/DESC) based on
subsequent clicks of the radio buttons.

But I can't get the frame or radio buttons to register a subsequent
click of the same control. IOW, if the date button is selected and I
click it again to change the sort order, nothing happens.

Can somone tell me how to do this?

The problem is that the option frame's AfterUpdate and Click events only
fire when the value of the option group changes, so clicking again on
the currently selected button has no effect.

I usually do this sort of thing by using a set of labels instead of an
option group. A function expression in each label's Click event
property calls a common function "SetSortOrder", which is defined like
this:

'------ start of code ------
Function SetSortOrder(FieldName As String)

If Me.OrderBy Like FieldName & "*" _
And Me.OrderByOn = True _
Then
If Split(Me.OrderBy & " ASC", " ", , vbTextCompare)(1) = "ASC"
Then
Me.OrderBy = FieldName & " DESC"
Else
Me.OrderBy = FieldName & " ASC"
End If
End If

Me.OrderByOn = True

Me.Recalc

End Function
'------ end of code ------

So for each label, the event property will call the function and pass
the name of the appropriate field, such as
=SetSortOrder("CustID")
or
=SetSortOrder("CustName")

The line, "Me.Recalc", at the end of the function, is to force the
recalculation of another function that displays the current sort order
as a little up or down arrow next to the fieldname label. You may not
need that, if you take a different approach.

If you really want to use the option group instead of the approach I
outlined above, you can probably et the effect you want by using
independent radio buttons, not part of an option frame, and using code
in each button's BeforeUpdate event to determine whether the indicated
sort field is already current. If it is, you cancel the actual update
but change between ascending and descending; if it isn't, you allow the
update, change the sort order, and clear all the other buttons. That
ought to work.
 

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