At the moment I have something like this (subroutines called via the onClick event in an Access 2003 DB):
VB Code:
Private Sub cmdSortAsc_Click() On Error GoTo cmdSortAsc_Click_Err DoCmd.Echo False, "Sorting records ..." Screen.ActiveForm.AllowAdditions = False DoCmd.GoToControl "txtCustomerID" DoCmd.RunCommand acCmdSortAscending Me![cmdSortDesc].Visible = True Me![cmdSortDesc].SetFocus Me![cmdSortAsc].Visible = False DoCmd.Echo True, "" Exit Sub cmdSortAsc_Click_Exit: DoCmd.GoToRecord , , acFirst Exit Sub cmdSortAsc_Click_Err: Resume cmdSortAsc_Click_Exit End Sub Private Sub cmdSortDesc_Click() On Error GoTo cmdSortDesc_Click_Err DoCmd.Echo False, "Sorting records ..." Screen.ActiveForm.AllowAdditions = False DoCmd.GoToControl "txtCustomerID" DoCmd.RunCommand acCmdSortDescending Me![cmdSortAsc].Visible = True Me![cmdSortAsc].SetFocus Me![cmdSortDesc].Visible = False DoCmd.Echo True, "" Exit Sub cmdSortDesc_Click_Exit: DoCmd.GoToRecord , , acFirst Exit Sub cmdSortDesc_Click_Err: Resume cmdSortDesc_Click_Exit End Sub Private Sub cmdSortAscComp_Click() On Error GoTo cmdSortAscComp_Click_Err DoCmd.Echo False, "Sorting records ..." Screen.ActiveForm.AllowAdditions = False DoCmd.GoToControl "txtCompanyName" DoCmd.RunCommand acCmdSortAscending Me![cmdSortDescComp].Visible = True Me![cmdSortDescComp].SetFocus Me![cmdSortAscComp].Visible = False DoCmd.Echo True, "" Exit Sub cmdSortAscComp_Click_Exit: DoCmd.GoToRecord , , acFirst Exit Sub cmdSortAscComp_Click_Err: Resume cmdSortAscComp_Click_Exit End Sub
I don't like this, because it's very similar code for each cmdButton placed on the form and my gut tells me that I could have one routine that handles all of the desired buttons.
This being the case, I need to be able to determine which object caused the event and I'm kinda going along these lines, but I have no idea if this is correct or not:
VB Code:
Private Sub cmdSort(ByVal sender As AccessObject) On Error GoTo cmdSort_Err ' do stuff cmdSort_Exit: DoCmd.GoToRecord , , acFirst Exit Sub cmdSort_Err: Resume cmdSort_Exit End Sub
Each button does essentially the same thing, just sorting different columns through this type of code (which then varies on a per button/routine basis):
VB Code:
DoCmd.GoToControl "txtCompanyName" DoCmd.RunCommand acCmdSortAscending Me![cmdSortDescComp].Visible = True Me![cmdSortDescComp].SetFocus Me![cmdSortAscComp].Visible = False
Two buttons are utilized and toggled in visibility to sort in one direction or another, each calls different routines, and then this is repeated per column that can be sorted.
I can't help but feel that this is cludgey ... surely one routine could ascertain which button was pressed, therefore which column to sort and the requirement for buttons for each sort direction could be removed simply with the use of global variables that remember what's currently sorted and in what direction.
All of that probably makes little sense as I tend to use 1,000 words where 100 will do, but can anyone help me tidy this code as I feel that it can be?




Reply With Quote