Solved

How can I add a sort button to my form?

Posted on 2009-07-07
5
371 Views
Last Modified: 2013-04-29
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.
0
Comment
Question by:Rwardlow
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:darbid73
ID: 24795188
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

0
 
LVL 10

Accepted Solution

by:
therealmongoose earned 500 total points
ID: 24795207
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]"
    Else
        Me.OrderBy = "[Your Field Name] desc"
    End If
 
    
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:Rwardlow
ID: 31600635
Thanks a lot, this is perfect.
0
 
LVL 1

Author Comment

by:Rwardlow
ID: 24795644
Thank you both for your solutions. I think both will work but the one I selected seemed to work better for me.
0
 
LVL 19

Expert Comment

by:darbid73
ID: 24795762
That is an interesting decision.  May I suggest you have a little bit of a look at how functions work and their flexibility.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question