J
JB
Hi Folks,
I can do this in VB6 but can't find a way to do it using VBA userforms! Any
pointers?
J
I can do this in VB6 but can't find a way to do it using VBA userforms! Any
pointers?
J
J
The exact approach to this depends a bit on whether you know ahead of time how many controls you might want to add at runtime, or whether you need to be completely open ended.
In any case, you can add controls at run-time in VBA using the Add method of the form's Controls collection:
dim newControl as Control
set newControl = UserForm1.Controls.Add(some syntax here specifying the type of control)
You can then manipulate newControl just like you can any other control, including processing events.
If, like I did once, you need to be able to handle any arbitrary number of controls at runtime, and you need to be able to process events for them, it gets a little trickier but not much. You basically just need to create a "wrapper" class for the controls to get at their events.
There's pretty good documentation for adding a finite number of known controls at runtime in the Microsoft VB(A) knowledge base stuff on MSDN. Search for "runtime" and "controls".
If you need to handle any arbitrary number of controls, let me know here and I can dig up the details on how I did it.
eowen
know ahead of time how many controls you might want to-----Original Message-----
J
The exact approach to this depends a bit on whether you
using the Add method of the form's Controls collection:In any case, you can add controls at run-time in VBA
specifying the type of control)dim newControl as Control
set newControl = UserForm1.Controls.Add(some syntax here
other control, including processing events.You can then manipulate newControl just like you can any
arbitrary number of controls at runtime, and you need toIf, like I did once, you need to be able to handle any
number of known controls at runtime in the Microsoft VBThere's pretty good documentation for adding a finite
let me know here and I can dig up the details on how IIf you need to handle any arbitrary number of controls,
Class module and then instantiate-----Original Message-----
Hi Jerry Bodoff
What you do is put the event handling procedures in a
("Forms.TextBox.1", "txtTest" & lngIndex)the class and link the control to the class. Here's how:
<--------Code to go in your Form Module----------->
' Event handlers must Persist outside the procedure that creates them
Private colControls As Collection
Private Sub UserForm_Initialize()
Dim ctlTB As MSForms.TextBox
Dim ctlCB As MSForms.CheckBox
Dim ctbNew As clsTextBox
Dim ccbNew As clsCheckBox
Dim lngTop As Long
Dim lngIndex As Long
' Locate first control
lngTop = 12
Set colControls = New Collection
For lngIndex = 1 To 3
' New TextBox control
Set ctlTB = Me.Controls.Add
("Forms.CheckBox.1", "chkTest" & lngIndex)ctlTB.Top = lngTop
ctlTB.Left = 50
lngTop = lngTop + 30
' Create event handler wrapper and hook new TextBox control to wrapper
Set ctbNew = New clsTextBox
Set ctbNew.mtxtNew = ctlTB
' Keep the event handler alive
colControls.Add ctbNew, CStr(lngIndex)
Next
For lngIndex = 1 To 3
' New CheckBox control
Set ctlCB = Me.Controls.Add
Class module and then instantiate-----Original Message-----
Hi Jerry Bodoff
What you do is put the event handling procedures in a
("Forms.TextBox.1", "txtTest" & lngIndex)the class and link the control to the class. Here's how:
<--------Code to go in your Form Module----------->
' Event handlers must Persist outside the procedure that creates them
Private colControls As Collection
Private Sub UserForm_Initialize()
Dim ctlTB As MSForms.TextBox
Dim ctlCB As MSForms.CheckBox
Dim ctbNew As clsTextBox
Dim ccbNew As clsCheckBox
Dim lngTop As Long
Dim lngIndex As Long
' Locate first control
lngTop = 12
Set colControls = New Collection
For lngIndex = 1 To 3
' New TextBox control
Set ctlTB = Me.Controls.Add
("Forms.CheckBox.1", "chkTest" & lngIndex)ctlTB.Top = lngTop
ctlTB.Left = 50
lngTop = lngTop + 30
' Create event handler wrapper and hook new TextBox control to wrapper
Set ctbNew = New clsTextBox
Set ctbNew.mtxtNew = ctlTB
' Keep the event handler alive
colControls.Add ctbNew, CStr(lngIndex)
Next
For lngIndex = 1 To 3
' New CheckBox control
Set ctlCB = Me.Controls.Add
Class module and then instantiate-----Original Message-----
Hi Jerry Bodoff
What you do is put the event handling procedures in a
("Forms.TextBox.1", "txtTest" & lngIndex)the class and link the control to the class. Here's how:
<--------Code to go in your Form Module----------->
' Event handlers must Persist outside the procedure that creates them
Private colControls As Collection
Private Sub UserForm_Initialize()
Dim ctlTB As MSForms.TextBox
Dim ctlCB As MSForms.CheckBox
Dim ctbNew As clsTextBox
Dim ccbNew As clsCheckBox
Dim lngTop As Long
Dim lngIndex As Long
' Locate first control
lngTop = 12
Set colControls = New Collection
For lngIndex = 1 To 3
' New TextBox control
Set ctlTB = Me.Controls.Add
("Forms.CheckBox.1", "chkTest" & lngIndex)ctlTB.Top = lngTop
ctlTB.Left = 50
lngTop = lngTop + 30
' Create event handler wrapper and hook new TextBox control to wrapper
Set ctbNew = New clsTextBox
Set ctbNew.mtxtNew = ctlTB
' Keep the event handler alive
colControls.Add ctbNew, CStr(lngIndex)
Next
For lngIndex = 1 To 3
' New CheckBox control
Set ctlCB = Me.Controls.Add
you're trying to get the class to-----Original Message-----
Hi Jerry Bodoff
I think your actually missing a couple of bits. Firstly
So internally the classes controlreturn the control name before you hook the class up!
Secondly you're trying to use thepointer (the variable you declare WithEvents) is null.
have no reference to it. Thiscontrols event handler class (your ButtonClass) when you
syntax. The only way of findingleads on to this problem you declare:
Dim ButtonClass as clsCmdButton
as a pointer to a class module, you then try to use it as a collection object:
ButtonIndex = ButtonClass.Item()
Even if ButtonClass was a collection, you can't use that
iterate the collection. Which isthe numeric index value of an item in a collection is to
value (the control name in thisnormally rather slow and pointless is you have it's key