Sorting

  • Thread starter Thread starter Clay
  • Start date Start date
C

Clay

I need a button on my form to sort the records by a
field. When I click the button, I want the form to sort
by the field "lost" (yes or no field).

What code do I put into the On-click event to sort my
records this way?

Thanks,

CLay
 
Clay, can't remember where I got this code, so can't give credit where it's do. Basically you
design 2 identical command buttons and place one directly on top of the other. Set the visible
property of one of them to false. Now run the following code (replace names of controls/fields to
suit):

Private Sub cmdSortAscMyField_Click()
On Error GoTo cmdSortAscMyField_Click_Err

DoCmd.Echo False, "Sorting records ..."

Screen.ActiveForm.AllowAdditions = False
DoCmd.GoToControl "MyField"
DoCmd.RunCommand acCmdSortAscending

Me![cmdSortDescMyField].Visible = True
Me![cmdSortDescMyField].SetFocus
Me![cmdSortAscMyField].Visible = False
DoCmd.Echo True, ""
Exit Sub

cmdSortAscMyField_Click_Exit:
DoCmd.GoToRecord , , acFirst
Exit Sub

cmdSortAscMyField_Click_Err:
Resume cmdSortAscMyField_Click_Exit

End Sub
***************************************************************
Private Sub cmdSortDescMyField_Click()
On Error GoTo cmdSortDescMyField_Click_Err

DoCmd.Echo False, "Sorting records ..."

Screen.ActiveForm.AllowAdditions = False
DoCmd.GoToControl "MyField"
DoCmd.RunCommand acCmdSortDescending

Me![cmdSortAscMyField].Visible = True
Me![cmdSortAscMyField].SetFocus
Me![cmdSortDescMyField].Visible = False
DoCmd.Echo True, ""
Exit Sub

cmdSortDescMyField_Click_Exit:
DoCmd.GoToRecord , , acFirst
Exit Sub

cmdSortDescMyField_Click_Err:
Resume cmdSortDescMyField_Click_Exit

End Sub

Hope it helps!
 
And of course you can always move your cursor to the "lost" field and hit
the A-Z button on the toolbar to sort the records in your form by that
field. do you really need a button?

Tom
 
Back
Top