• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

How can I add a sort button to my form?

I am new to using Access 2007 runtime. Since runtime users don't have access to the ribbon they can't click on a field on a form and click on the sort button to sort records Acending or decending as they would if they clicked the sort button on the ribbon.

How do I program a sort button on the form so my user can select one of several fields to sort records?

Thanks in advance for your help.
Robert Wardlow
Robert Wardlow
  • 2
  • 2
1 Solution
would this do the trick

Function SortForm(frm As Form, ByVal sOrderBy As String) As Boolean
'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")
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
End Function

Open in new window

I use the colume / field labels and put a sort process in on the on click event, but you could equally use buttons...
The syntax to sort by a field is as below:
The example below will sort ascending then decscending, depending on the current order...

Private Sub Label20_Click()
    If Me.OrderBy <> "[Your Field Name]" Then
        Me.OrderBy = "[Your Field Name]"
        Me.OrderBy = "[Your Field Name] desc"
    End If
End Sub

Open in new window

Robert WardlowPresidentAuthor Commented:
Thanks a lot, this is perfect.
Robert WardlowPresidentAuthor Commented:
Thank you both for your solutions. I think both will work but the one I selected seemed to work better for me.
That is an interesting decision.  May I suggest you have a little bit of a look at how functions work and their flexibility.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now