Hi hoen,
The concept you are describing is known as a control array. Technically speaking,
VB is the only MS language that provides intrinsic support for them. If you place 3 TextBox controls on a
VB form and give them all the same name,
VB initializes them in an array of TextBoxes as:
Textbox(0)
Textbox(1)
Textbox(2)
You can then do stuff like:
Code:
Private Sub TextBox_Change(Index As Integer)
â The textbox that fired the event gets its text property changed.
TextBox (Index).Text = "something"
End Sub
Or:
Code:
For i = 1 To 6
TextBox(i).Text = "TextBox"
Next
VB.Net abandoned this type of control array, but replaces it with the ability to create event handlers that handle multiple control events with syntax like:
Code:
Private Sub AllControlButtins_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click, _
Button2.Click, _
Button3.Click
The "sender" argument identifies the control that fired the event.
VBA doesn't support control arrays either. To emulate this behavior, however, you can to design a class to store a reference to each TextBox control on your form in a module level object variable declared WithEvents. You declare and event handler in the class, like:
Private Sub m_TextBox_Change()
Then give the event handler the ability to notify a specific textboxâs parent form which textbox fired the event by calling a method on the form.
This is as close as you can get in VBA to having a single event sink for multiple controls:
Code:
=====================================================
Module For frmParent:
frmParent has two TextBox controls on it: txtTextBox1 and txtTextbox2
=====================================================
Option Compare Database
' Collection to store instances of clsTextBox
Private colTextBoxes As New Collection
Private ctlTextBox As clsTextBox
Public Sub WhichTextBox(ctl As TextBox)
' Notify user which TextBox fired the change event.
MsgBox "ChangeEvent fired by: " & ctl.Name
End Sub
Private Sub Form_Load()
' Instantiate new clsTextBox objects.
' Each object holds a reference to one
' of the textbox controls on the form
' and a reference to the controls parent
' form. The newly instantiatied object is
' then added to the colTextBoxes collection.
' Create a class to reference txtTextBox1
Set ctlTextBox = New clsTextBox
ctlTextBox.Initialize txtTextBox1, Me
colTextBoxes.Add ctlTextBox
' Create a class to reference txtTextBox2
Set ctlTextBox = New clsTextBox
ctlTextBox.Initialize txtTextBox2, Me
colTextBoxes.Add ctlTextBox
End Sub
=====================================================
Class Module for clsTextBox:
Sinks Change events for all clsTextBox objects
=====================================================
Option Compare Database
Option Explicit
' Holds a reference to one TextBox control on
' frmParent. It is declared WithEvents so it
' can sink events for the TextBox control it
' refers to.
Private WithEvents m_TextBox As TextBox
Private Const mstrEventProcedure = "[Event Procedure]"
' Holds a reference to the parent form that hosts
' the TextBox control.
Private m_Form As Form_frmParent
' Initialize a new clsTextBox object, passing it
' references to a TextBox control and its parent
' form
Public Sub Initialize(ctl As TextBox, frm As Form_frmParent)
Set m_TextBox = ctl
'Set the OnChange property of the control to [Event Procedure]
m_TextBox.OnChange = mstrEventProcedure
Set m_Form = frm
End Sub
' This event handler will handle the Change event for each
' TextBox control referenced by a clsTextBox object in
' colTextBoxes.
Private Sub m_TextBox_Change()
' Call the WhichTextBox method of the parent form, passing
' it the instance of the TextBox that fired the Change event.
m_Form.WhichTextBox m_TextBox
End Sub
' Garbage collection.
Private Sub Class_Terminate()
Set m_TextBox = Nothing
Set m_Form = Nothing
End Sub
NOTE:
You can also use a controls Tag Property to emulate some control array type behavior:
Code:
Private Sub cmdButton1_Click()
Dim ctl As Control
Dim i As Integer
For Each ctl In Me.Controls
If Not ctl.Tag = "" Then
For i = 1 To 3 ' Effects controls whose tag property is in the range 1 to 3
If ctl.Tag = i Then
ctl.Enabled = True
End If
Next
End If
Next
End Sub
HTH,
Bob