I built a form where the user can simply click on the header text and the
form sorts (either asc or desc). The trick I used is to make the text a
hyperlink with an "OnClick" event. NO BUTTONS NEEDED!
Make the properties of each column header the following:
Type: LABEL
Hyperlink Subaddress: "Form [your form name]"
ControlTipText: "Sort by [TextName] . . ."
On Click: [Event Procedure]
'**********************************************
Option Compare Database
Option Explicit
Dim iFlag As Integer
Private Sub btn_Open_Click()
strMSG = "Opening Existing Record"
strDocName = "frm_FormDetail"
strLinkCriteria = "[policyNumber] = " & Me.[policyNumber] & ";"
DoCmd.Close acForm, "frm_myForm"
sSQL = "SELECT [fill in your SQL Statement here...] " & _
"WHERE " & strLinkCriteria
End Sub
'THE FOLLOWING "Re-Sort" subroutines ar
Private Sub Resort_By_Name_Click()
If Label_SortNameAscending.Visible = False Then
iFlag = 1
Else
iFlag = 2
End If
strCaption = " Re-Sorted by Name "
fReSortData
End Sub
Private Sub ReSort_By_Loss_Date_Click()
If Label_SortDateAscending.Visible = False Then
iFlag = 3
Else
iFlag = 4
End If
strCaption = " Re-Sorted by Date of Loss "
fReSortData
End Sub
Private Sub ReSort_By_Prog_Click()
If Label_SortProgAscending.Visible = False Then
iFlag = 5
Else
iFlag = 6
End If
strCaption = " Re-Sorted by Program "
fReSortData
End Sub
Private Sub ReSort_By_Claim_Click()
If Label_SortClaimAscending.Visible = False Then
iFlag = 7
Else
iFlag = 8
End If
strCaption = " Re-Sorted by Policy Number "
fReSortData
End Sub
Public Function fReSortData()
On Error GoTo Err_ResortData
Refresh_Labels:
Label_SortNameAscending.Visible = False
Label_SortNameDescending.Visible = False
Label_SortDateAscending.Visible = False
Label_SortDateDescending.Visible = False
Label_SortProgAscending.Visible = False
Label_SortProgDescending.Visible = False
Label_SortClaimAscending.Visible = False
Label_SortClaimDescending.Visible = False
Select Case iFlag
Case Is = 1
Label_SortNameAscending.Visible = True
strSortOrderSQL = "ORDER BY [LastName] & ', ' & [FirstName];"
Case Is = 2
Label_SortNameDescending.Visible = True
strSortOrderSQL = "ORDER BY [LastName] & ', ' & [FirstName] DESC;"
Case Is = 3
Label_SortDateAscending.Visible = True
strSortOrderSQL = "ORDER BY [sysDate], [LastName];"
Case Is = 4
Label_SortDateDescending.Visible = True
strSortOrderSQL = "ORDER BY [sysDate] DESC, [LastName];"
Case Is = 5
Label_SortProgAscending.Visible = True
strSortOrderSQL = "ORDER BY [prog], [policyNumber];"
Case Is = 6
Label_SortProgDescending.Visible = True
strSortOrderSQL = "ORDER BY [prog] DESC, [policyNumber];"
Case Is = 7
Label_SortClaimAscending.Visible = True
strSortOrderSQL = "ORDER BY [policyNumber];"
Case Is = 8
Label_SortClaimDescending.Visible = True
strSortOrderSQL = "ORDER BY [policyNumber] DESC;"
End Select
Select Case iFlag
Case Is = 1, 3, 5, 7
strCaption = strCaption & " (Ascending)"
Case Else
strCaption = strCaption & " (Descending)"
End Select
Set_SQL:
sSQL = strBaseSQL & strCriteriaSQL & strSortOrderSQL
Refresh_Forms:
Forms![frm_ClaimListing].RecordSource = sSQL
Forms![frm_ClaimListing].[btn_Close].Enabled = True
Forms![frm_ClaimListing].[btn_Close].SetFocus
Forms![frm_ClaimListing].Caption = strCaption
Forms![frm_ClaimListing].Refresh
Exit Function
Err_ResortData:
fDisplayError
Exit Function
End Function
' EOM ********************************
Lesley said:
Hey that was my question too, but boss-man wants separate buttons on the form
to sort each field. In other words, a botton to sort Account, a botton to
sort Group and a button to sort Customer (each in ascending and descending
order). If that's whay he wants, that's what he'll get (SMILE). I do not
want the person to have to click on the field first. Just click the button
and it sorts the particular field automatically....
I hope that's a little more clear. Will the code you provided work in this
scenerio???
:
On Mon, 28 Aug 2006 16:34:01 -0700, Lesley wrote:
what's the best way to create sort buttons (in the design) of a form to sort
the various fields in either ascending or descending order???
"The various fields"? Your not giving us much to go on here.
This code in a command button click event will sort the named control
ascending:
[ControlName].SetFocus
DoCmd.RunCommand acCmdSortAscending
Change Ascending to Descending if wanted.
If you wish to be able to select the field by first clicking in it,
then:
Screen.PreviousControl.SetFocus
DoCmd.RunCommand acCmdSortAscending
Any reason why the built in tool buttons are not suitable?