Passing Form Objects To Reduce Coding Requirements
Many Access applications have form textboxes or other objects which repeat themselves many times, such as the case of a textbox where the user can enter many of the same type of items. For example, a form may have textboxes which can be populated with a total sales number.
Each of these multiple textbox contents must be edited, and the same editing code must be repeated unless the textbox objects are passed to an editing subroutine.
The example below shows a textbox / label combination repeated 5 times which must be edited in an identical manner. The example first shows how to reduce code by passing the textbox and label objects to the editing subroutine. Further down in the example illustrates the raw coding that must be completed if editing is done within each textbox AfterUpdate event.
Program Code
Option Compare Database Option Explicit ' ************************************************************************* ' Define The Two Objects That Will Be Passed to the Editing Subroutine ' ************************************************************************* Dim tbxTransferTextbox As TextBox Dim lblTransferLabel As Label ' ************************************************************************* ' The Next 5 Routines Show a Concise Method of Editing Form Objects ' By Passing the objects to a single editing routine ' ************************************************************************* Private Sub txtTransfer01_AfterUpdate() Set tbxTransferTextbox = Me.txtTransfer01 Set lblTransferLabel = Me.lblTransferComment01 Call EditTransfers(1) End Sub Private Sub txtTransfer02_AfterUpdate() Set tbxTransferTextbox = Me.txtTransfer02 Set lblTransferLabel = Me.lblTransferComment02 Call EditTransfers(2) End Sub Private Sub txtTransfer03_AfterUpdate() Set tbxTransferTextbox = Me.txtTransfer03 Set lblTransferLabel = Me.lblTransferComment03 Call EditTransfers(3) End Sub Private Sub txtTransfer04_AfterUpdate() Set tbxTransferTextbox = Me.txtTransfer04 Set lblTransferLabel = Me.lblTransferComment04 Call EditTransfers(4) End Sub Private Sub txtTransfer05_AfterUpdate() Set tbxTransferTextbox = Me.txtTransfer05 Set lblTransferLabel = Me.lblTransferComment05 Call EditTransfers(5) ' ************************************************************************* ' Edit The TextBox and Label Passed As An Object ' ************************************************************************* Private Sub EditTransfers(intIndex As Integer) If IsNull(tbxTransferTextbox.Value) Or tbxTransferTextbox.Value = "" Then lblTransferLabel.Visible = False dblWHTWeight(intIndex) = 0 Exit Sub End If If Not IsNumeric(tbxTransferTextbox.Value) Then lblTransferLabel.Visible = True Me.txtInvoiceNo.SetFocus tbxTransferTextbox.SetFocus dblWHTWeight(intIndex) = 0 Exit Sub End If lblTransferLabel.Visible = False dblWHTWeight(intIndex) = tbxTransferTextbox.Value End Sub ' ************************************************************************* ' Code Without Passing The Objects to an Editing Routine ' ************************************************************************* ' ************************************************************************* Private Sub txtTransfer01_AfterUpdate() If IsNull(Me.txtTransfer01) Or Me.txtTransfer01 = "" Then Me.lblTransferComment01.Visible = False dblWHTWeight(1) = 0 Exit Sub End If If Not IsNumeric(Me.txtTransfer01) Then Me.lblTransferComment01.Visible = True Me.txtInvoiceNo.SetFocus Me.txtTransfer01.SetFocus dblWHTWeight(1) = 0 Exit Sub End If Me.lblTransferComment01.Visible = False dblWHTWeight(1) = Me.txtTransfer01 End Sub ' ************************************************************************* Private Sub txtTransfer02_AfterUpdate() If IsNull(Me.txtTransfer02) Or Me.txtTransfer02 = "" Then Me.lblTransferComment02.Visible = False dblWHTWeight(2) = 0 Exit Sub End If If Not IsNumeric(Me.txtTransfer02) Then Me.lblTransferComment02.Visible = True Me.txtInvoiceNo.SetFocus Me.txtTransfer02.SetFocus dblWHTWeight(2) = 0 Exit Sub End If Me.lblTransferComment02.Visible = False dblWHTWeight(2) = Me.txtTransfer02 End Sub ' ************************************************************************* Private Sub txtTransfer03_AfterUpdate() If IsNull(Me.txtTransfer03) Or Me.txtTransfer03 = "" Then Me.lblTransferComment03.Visible = False dblWHTWeight(3) = 0 Exit Sub End If If Not IsNumeric(Me.txtTransfer03) Then Me.lblTransferComment03.Visible = True Me.txtInvoiceNo.SetFocus Me.txtTransfer03.SetFocus dblWHTWeight(3) = 0 Exit Sub End If Me.lblTransferComment03.Visible = False dblWHTWeight(3) = Me.txtTransfer03 End Sub ' ************************************************************************* Private Sub txtTransfer04_AfterUpdate() If IsNull(Me.txtTransfer04) Or Me.txtTransfer04 = "" Then Me.lblTransferComment04.Visible = False dblWHTWeight(4) = 0 Exit Sub End If If Not IsNumeric(Me.txtTransfer04) Then Me.lblTransferComment04.Visible = True Me.txtInvoiceNo.SetFocus Me.txtTransfer04.SetFocus dblWHTWeight(4) = 0 Exit Sub End If Me.lblTransferComment04.Visible = False dblWHTWeight(4) = Me.txtTransfer04 End Sub ' ************************************************************************* Private Sub txtTransfer05_AfterUpdate() If IsNull(Me.txtTransfer05) Or Me.txtTransfer05 = "" Then Me.lblTransferComment05.Visible = False dblWHTWeight(5) = 0 Exit Sub End If If Not IsNumeric(Me.txtTransfer05) Then Me.lblTransferComment05.Visible = True Me.txtInvoiceNo.SetFocus Me.txtTransfer05.SetFocus dblWHTWeight(5) = 0 Exit Sub End If Me.lblTransferComment05.Visible = False dblWHTWeight(5) = Me.txtTransfer05 End Sub