Andy, add the code below to a standard module.
You can then set command button to sort by last name to:
=SortForm([Form], "[Last Name])
If it is already sorted by that field, it reverses the sort.
Function SortForm(frm As Form, ByVal sOrderBy As String) As Boolean
On Error GoTo Err_SortForm
'Purpose: Set a form's OrderBy to the string. Reverse if already
set.
'Return: True if success.
'Usage: Command button above a column in a continuous form:
' Call SortForm(Me, "MyField")
Dim sForm As String ' Form name (for error handler).
sForm = frm.Name
If Len(sOrderBy) > 0 Then
' Reverse the order if already sorted this way.
If frm.OrderByOn And (frm.OrderBy = sOrderBy) Then
sOrderBy = sOrderBy & " DESC"
End If
frm.OrderBy = sOrderBy
frm.OrderByOn = True
' Succeeded.
SortForm = True
End If
Exit_SortForm:
Exit Function
Err_SortForm:
Call LogError(Err.Number, Err.Description, conMod & ".SortForm()",
"Form = " & sForm & "; OrderBy = " & sOrderBy)
Resume Exit_SortForm
End Function
Use your own error handler, or grab this one:
http://allenbrowne.com/ser-23a.html
Can I add buttons to a form to sort different fields ascending /
descending
eg. I have a form with 3 fields -Last Name, First Name and Age. I
would like 2 buttons on each field to sort wither up or down on each
field i.e. a button to sort ascending on last name and another
fordescending on last name etc.