Looping through the Controls collection is fairly straight forward as it is a collection, and items can be referenced by Index number, or by key, with the key being the name of the control. This allows you to loop through the control collection in the following manner, only looking at controls named Ctl1, Ctl2, ... Ctl140
Code:
For Idx = 1 to 140
Set Ctl = Me.Controls("Ctl" & Trim(Idx))
With Ctl
...
End With
Next Idx
which is all fine and good, but I don't think it helps with the fundamental issue behind this application. In seems to me that the key element in this is knowing if and when the focus has changed from one control to another, without having specific event handlers for each and every control. One approach that might work, but does have some downsides, is the following:
Create another textbox on the form and call it "txtActiveControl", and set its visible property to false.
In the Form_Load event, place the following code:
[/code]
Private Sub Form_Load()
Me.TimerInterval = 500
txtActiveControl.Tag = vbNullString
End Sub
Code:
Then construct the Timer event as follows:
[code]
Private Sub Form_Timer()
If (Screen.ActiveControl.ControlType = acTextBox) Then
txtActiveControl = Screen.ActiveControl.Name
If (txtActiveControl <> txtActiveControl.Tag) Then
MsgBox "Textbox has changed to: " & txtActiveControl
txtActiveControl.Tag = txtActiveControl
End If
End If
End Sub
In place of the MsgBox, you will place your routine to extract the current textbox's name and do with it as you must. txtActiveControl will always contain the name of the textbox which last had, or currently has the focus. As it is, if you click on a button, or even a combo box, the value of txtActiveControl will not change. It will still contain the name of the last text box which had the focus. If you want to bring other control into the mix, you can adjust the
Code:
If (Screen.ActiveControl.ControlType = acTextBox) Then
to meet your needs.
The downsides: It is possible that you could skip over a control by clicking on one control and then another between Timer interval events. You will miss the intervening click. Secondly, become of the rapidity of the Timer interval, you will be chewing up lots of processer cycles. But in the meantime, until we can come up with something a little more robust and efficient, you might be able to use this to get the job done.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein