View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Doug Glancy Doug Glancy is offline
external usenet poster
 
Posts: 770
Default Addressing specific textboxes on a form

Chris,

The chief problem is that form controls in a class don't have Exit or
AfterUpdate events, so validation is difficult, because you have to use the
Change event. Beyond that, I think I was able to get what you want:

Assuming:
UserForm1 and UserForm2: with TextBox1 ... TextBox100 and Label1...
Label100, respectively
A class module called "FormTboxClass"
A standard module

In the FormTboxClass this code:

Public WithEvents FormTbox As msforms.TextBox
Private Sub FormTbox_Change()

With FormTbox
If IsNumeric(.Value) Then
If .Value = 1 And .Value <= 26 Then
UserForm2.Controls("Label" & Mid(FormTbox.Name, 8)).Caption =
Chr(FormTbox.Value + 64)
End If
End If
End With
End Sub

In UserForm1 this code:

Dim myControls As Collection ' note this is above any subroutines
Option Explicit
Private Sub UserForm_Initialize()

Dim tmpctl As Control
Dim ctl As FormTboxClass

Set myControls = New Collection
For Each tmpctl In Me.Controls
If TypeOf tmpctl Is msforms.TextBox Then
Set ctl = New FormTboxClass
Set ctl.FormTbox = tmpctl
myControls.Add ctl
End If
Next

End Sub

In the standard module this code:

Sub test()

UserForm1.Show modeless
UserForm2.Show modeless

End Sub

Run the "test" macro. Move Form2 to reveal Form1. You should get the
actions you described. Notice that if you go outside the range 1 to 26 or
enter non-numeric characters, nothing happens. I tried to do more with
invalid entries, but couldn't find a good solution. I think the forms have
to be modeless for this to work, but not positive.

hth,

Doug

"inquirer" wrote in message
u...
I have a userform with 100 textboxes on it and another form with 100
labels. The textboxes are filled with numbers 1 to 26.

I want to change the label captions on the second form to the alpha
equivalent of the number in the textbox, ie if textbox 27 has 5 in it, the
caption for label 27 should be E.

Also if I change say textbox 45 from 7 to 22, I want this cahnge to be
reflected in the caption for label 45.

I understand this could be done through control arrays but I can't get
them to work for textboxes.

Could someone help please?
Thanks
Chris