Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable number of controls on a userform 2

Status
Not open for further replies.

maxxia

Technical User
Jan 1, 2004
8
US
I'm trying to construct a userform that has fields which, when filled, form an equation. Each field is a separate part of the equation and the number of parts is variable, thus I've had to write code creating the necessary number of controls for any given equation. This I've written at the Userform_Initialize() event.

What I'd like to do is have the equation show up as each part is entered. My problems is that I need to modify the equation each time one of the fields is changed. I know how to use the TextBox_change() event for a control that is already on the Userform, but my problem is that at the time of initialization, I've created an array of controls (e.g. Equationparts(1 To 10)) and now I can't reference them in any way that's recognizable to form an event Sub (i.e. Sub Equationsparts(1)_Change() doesn't work).

I hope I'm making sense. . . .

Alternatively, I could create controls that aren't in an array, avoiding the problem of the parenthetical array-referencing which apparently doesn't work in defining events, but I don't know how to create a variable number of controls that are uniquely named. Is there a way of creating objects that have a name that includes a variable (e.g. Set object"c" = Userform.Controls.Add("Forms.textbox.1") where "c" is an integer variable? In other words, can I write a For loop with a variable number of iterations, say i, and then have "i" somewhere in the name of a new object created within the loop?

Any help much appreciated!
 
If you give the first control on the form (in design time) an index of zero, an array will be created. This means that the "index" argument of your event handlers will specify the number of the control who fired the event.

Greetings,
Rick
 
Thanks, Rick, sounds good if I can do it.

What is the syntax for creating an array of events? And once I have the event array, how can I write a For loop that will work when a particular one fires (given that there will be a variable number of them?

Thanks again, Max
 
In design time you should put the first control on the usercontrol. Set the Index property to "0". This will make it an array. The eventhandlers of the control array will include an argument, named "Index". This is the one identifying the control that fired the event.

Now I don't know if you want to dynamically load the controls or if you're gonna put them all at design time on the usercontrol. In the latter case just copy and paste the control, it will then automatically generate an arry for you (if you choose "yes" on the question being asked).

Because of the "Index" argument in the eventhandlers, it is not necessary to use a for loop to get to the one that actually fired the event, nor will it be necessary to create an array of events.....

Greetings,
Rick
 
To create a control at runtime, add to the array, you can use the Load statement.

Code:
Load txtArray(<NextIndex>)

Take Care,

zemp
 
Thanks for all the help so far. . . maybe I just don't get it. . .

I am able to create the array of controls, which are TextBoxes, at runtime. I need them to be dynamically created at runtime because the number of them is dependent on a variable integer.

I can change properties for these textboxes by using property handles like textbox(i).height (I can't, however, use textbox.Index).

My problem is in trying to define the event textbox_Change(). Because I have an array of textboxes, I need to specify which textbox the event goes with, using it's index. The textbox(i)_Change() syntax, however, doesn't work.

I'm guessing there might be some argument you could pass to the Change event as the index, like textbox_Change(index), but I don't know the syntax. Besides that, even if I can define the textbox_Change() event for one of my textbox controls, I need to do it for all of them in the array, whether there are 10 or 100, at runtime.
That's why I'd like to use a For loop, only I can't figure out how to define an event Sub within a for loop.
In other words, I need to define Sub TextBox_Change() for an array of textboxes.

I appreciate the help so far.
Max
 
When You have an event for a control array it is shown with an 'Index' argument.

Code:
Text1_Change(Index as Integer)

This argument is automatically passed to the event and is the index of the control for which the event is fired. ie. if text1(0) fires the event then the value of Index will be 0.

Any code in the event should use this 'Index' argument in order to reference the correct control. This way the event will work for all controls in the control Array.

Code:
Private Sub Text1_Change(Index as Integer)
   Text1(Index).Text=Ucase(Text1(Index).Text)
End Sub

Hope this sheds some light.



Take Care,

zemp
 
Thanks Zemp. Just one more question:

The way I've done it, I declare the dynamic array of Textbox controls as objects:

Dim textbox() As Object

and then add a new textbox control in the Initialization event with:

Set textbox(c) = userform.Controls.Add(&quot;Forms.Textbox.1&quot;)

so I can reference properties of the controls with
textbox(c).Text, for instance.

The problem with:

Private Sub textbox_Change(Index as Integer)
gettext = textbox(c).Text
End Sub

is that VBA doesn't recognize the textbox_Change(Index as Integer) event as referring to all controls in the textbox control array, and never fires.
I can't use
Dim newobject as Object
Set newobject = Load textbox(c)
because don't I need to specify what type of object I'm adding to the array?

Am I declaring the array of controls wrong or adding new controls to the Object array wrong?

Thanks,
Max
 
Using VB I would declare it as a textbox and not an object. To add a control to the array i would just use the load statement as in my earlier post. I am not certain of the best VBA syntax.

To get the exact syntax for VBA you would be best to post in a forum for VBA. If it is MS Access VBA then try forum705. You can check the forum list (link at top left of page) for the specific VBA forum that suits you best.



Take Care,

zemp
 
What about using the Controls collection that all control containers have. This has a member ADd or Append ( I forget which ), that can add controls without creating a controls array. You need to know the ProgID of the control ( obtain from Object Browser ). You can assign any symbolic name to such controls and even include them in sub-containers e.g. a fram-box of a form. Beware but the method is not properyly documented in MSDNL- I had to use the Search tab to find info on it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top