Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 2 / March 2007

Tip: Looking for answers? Try searching our database.

Make a sort button

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dig - 28 Mar 2007 14:20 GMT
I have a form that I would like to add a sort button to. I tried the command
button wizard but I did not see any choice for sort.

What is the best (easiest) way to make a sort button that I can put on a form?

Thanks
Allen Browne - 28 Mar 2007 15:01 GMT
You can sort a form by setting its OrderBy property, remembering to turn on
OrderByOn as well.

If you paste the function below into a module, you can use it in any form.
For example, to sort by the City field, add this line of code to the Click
event procedure of your button:
   Call SortForm(Me, "City")

If the form was already sorted by this field, it swaps direction (z to a.)

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:
   MsgBox "Error " & Err.Number & ": " & Err.Description
   Resume Exit_SortForm
End Function

Signature

Allen Browne - Microsoft MVP.  Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

>I have a form that I would like to add a sort button to. I tried the
>command
[quoted text clipped - 4 lines]
>
> Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.