The code sets the OrderBy property of the form.
No need to do anything else except use the Click event of the button.
If you are being asked for a parameter value, it means Access does not
understand the name you asked for, so:
1. Check the name is exactly correct.
2. If it contains a space, inclose it in square brackets, e.g.:
=SortForm([Form], "[Global Title]")
3. Make sure the field you are sorting by is actually in the form's
RecordSource (the table/query the form is based on).
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
Farooq Sheri said:
Thanks for your response. How do I set the OrderBy property?. Also I
followed
your previous instructions yet when I click the command button a prompt
pops
up asking for "Enter Parameter value" for the field I want to sort.
Allen Browne said:
Use the OrderBy property of the form.
Don't forget to set OrderByOn as well.
Paste the function below into a standard module, and save.
Then set the On Click property of your command button to:
=SortForm([Form], "GlobalTitle")
etc.
Function SortForm(frm As Form, ByVal sOrderBy As String) As Boolean
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
End If
End Function
I would like to sort fields on a form by the click of a command button.
I
have a form frmGT which is based on a query qryGTSearch. Also there are
two
command buttons. The two text fields on the form are called GLOBALTITLE
and
TRLG. I would like to have the flexibility to sort (ascending) which
ever
field I want by the click of a command button.