Logicwurks Home Page

Links To Excel Code Examples

Tracing VBA Statements
Range/Wkb/Wks Variables
Add Grand Totals Using Ranges
Using Range Offset Property
Using Range Find Method
ConvertCellAddressToRange
Set Conditional Formatting
Union Of Ranges
Parse Range Strings
Delete Duplicate Rows
Delete Rows And Columns
Worksheet Variables
TypeName And TypeOf
Loop Through Worksheets
Loop Through Open Workbooks
Form Button Magic
Command Button Magic
Add Worksheets Dynamically
ImportExternalWorksheets
Find Last Row Or Column
Copy And Paste Special
Copy To Specific Cell Types
Range Copy With Filter
ExcelFileOpenSaveClose
ExcelFileOpenSaveCSV
Open An Excel File
Open An Excel File w/Params
Open An Excel File On Web
Save A Workbook
Save A Workbook Using mso
Clone A Workbook
Test If WEB URL Exists
Parse Using Split Command
Using Classes in Excel
TypeStatementStructures
Color Management
Convert Cell Color To RGB
Sort Methods 2003 - 2010
Sort Alpha/Numeric In ASCII
Search Using Match Function
Search Using Vlookup Function
Search Using Xlookup Function
Using Find Instead of Vlookup
Remove String Non-Printables
Auto_Open And Auto_Close
Initialize Form At Open
Edit Numerics In UserForm
Load Combo And List Boxes
Floating Sheet Combo Boxes
Advanced User Form Coding
Excel Events
Worksheet Change Events
Binary Search Of Array
Typecast Constants
Excel Error Handling
Handling Optional Parameters
Data Validation Drop Downs
Insert Data Validation Sub
Read A Text File w/Handle
Write A Text File w/Handle
Read a Binary File w/Handle
Update a Binary File w/Handle
Binary File Copy and Update
Read A Text Fiile w/Script
Text File Processing Examples
Test For Exists Or Open
Splash Screen
Dynamically Load Formulas
PaymentStreamsByDate
Date Examples
Date Find Same Days
Convert Month To Number
Initialize Arrays
Load Arrays Using Evaluate
ChartsAndGraphsVBA
Redim An Array
Reassign Button Action
Timer Functions
Legacy Calendar Control
Excel 2010 Date Picker
Date Picker Alternative
Generate Multiple Worksheets
Read Access Data Into Excel
Send Outlook Email w/Attach
Copy AutoFilters To Sheets
Export A Text File
Get Windows User Name
VBA Format Statement
Manipulate Files via VBA
Dynamically Load Images
Loop Through Worksheet Objects
Loop Through Form Objects
Loop Through Files with DIR
Active-X Checkboxes
Add Forms Checkboxes Dynam
Paste Pictures Into Excel
Copy Pictures Sheet To Sheet
Copy Pictures Sheet To Sheet
Create Forms Buttons With VBA
Extract Filename From Path
Convert R1C1 Format to A1
Special Cells Property
Insert Cell Comments

Links To Access Code Examples

DAO Versus ADODB
SearchVBACodeStrings
Interface Excel With Access
Create Form Manually
Create Recordset With AddNew
Multi-Select List Boxes
Update Field(s) In A Recordset
Update Excel Pivot From Access
Import A Tab Delimited File
Export Excel FileDialog
Create Excel Within Access
Open Excel Within Access
Open Excel OBJ From Access
Format Excel From Access
Control Excel via Access VBA
On Error Best Practices
Import Tab Delim w/WinAPI
Initialize Global Variables
Using TempVars For Globals
Access Error Handling
Loop Through Form Controls
Insert A Calendar Control
Create A Filtered Recordset
Populate Combo Boxes
Bookmarks And Forms
Combo Box Multiple Sources
Passing Form Objects
Create VBA SQL Statements
Create Dynamic Queries
Display File Images On A Form
Manipulate Files via VBA
Manipulate Files via Scripting
Number Subform Records
Reference Subform Objects
Parse Delimited Fields
Parameterized Queries (VBA)
Manipulating QueryDefs In VBA
FindFirst On Combined Keys
Dlookup Command
Dlookup In Form Datasheet
Execute SQL Delete Records
Commit Form To Table
Report With No Data
Reference Form Objects
DSNLess Connections To MySQL
Print Active Form Record
Count Records in Linked Tables
Delete Empty Tables
Open Linked SQL Tables

 

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