PROBLEM: Keywords;= Userform, Class Modules, ButtonGroup and Nomenclature

M

Myles

I have a Userform with 48 commanbuttons created on the fly via
*ClassModule *named *BClass* and using Array holder *Btns()*.

As we would have it, it is possible to refer to any of th
CommandButtons (say the 15th button) as, by way of example, * Btn
(15).ButtonGroup.Caption *. But this is valid only when such referenc
is made from a *UserForm Module *or *General Module*.

When it is used in a Class Module, * Btns (15).ButtonGroup.Caption
fails, as does, *BClass.Btns(15).ButtonGroup.Caption*. In m
frustration, I have had to get by using a long-winded approach as:

For each ctl in UserForm1.Controls
If TypeOf ctl Is MsForms.CommandButton then
If ctl.Width =60 Then 'optional line
If ct.name ="CommandButton15"
Msgbox *ctl.Caption*
End if
End if
End if
Next

Now, going by the example, what *nomenclature* can be employed t
invoke -the caption of CommandButton15 -*from a Class Module*? the wa
we would use * Btns (15).ButtonGroup.Caption * from a UserFrom modul
or General module?


TIA

Myle
 
B

Bob Phillips

Show the code that creates the buttons array, it may be re-usable.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
M

Myles

Bob,

As requested:

Dim Btns() as New BClass
Sub CreateButtonClass()
Dim ctl as Control

n=1
For each ctl in Me.Controls
If Typename(ctl)="CommandButton" Then
Redim Preserve Btns(1 to n)
Set Btns(n).ButtonGroup=ctl
n=n+1
End if
Next

End su
 
B

Bob Phillips

Myles,

I have just Googled back a bit as I thought this seemed familiar, and I
found and earlier post where you had a problem and Peter T and I responded.
Peter T said

'could include .Item(8) for clarity

but as I said ... You can't actually. I assumed that Myles was creating a
collection for the buttons within the form which he could then address with
Item, but he is not assigning the controls to an array of the class,
slightly different approach. I like the collection myself :)).

Moral. I suggest that you use a collection.


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
M

Myles

Hi Bob,

This is indeed a sequel to my earlier post which solicited for the way
one can simply refer to a control created as a new collection in a
Class Module. The solution you and Peter T. proffered works like charm!
The only limitation, as I discovered later , is that using an expression
like *Buttons(15).ButtonGroup.Caption *or
-Buttons.Item(15).ButtonGroup.Caption- works but only while you are in
a Userform Module or General Module. To attempt to invoke it *from a
Class Module *draws an error.

How do we get round this problem? I don't seem to have had the clue
yet.

Any ideas?


Myles.
 
P

Peter T

Hi Myles,

I also recall that thread and seem to remember the "array of class's" method
worked fine for you following some minor modifications, as expected (<g>).

Your current problem, as I understand, is you are trying to refer to the
array of class's that you have declared in a Userform. Instead you could
declare the array as public in a normal module, but if you want to keep you
array in your UF (and there are good reasons for doing so) then try
something like this -

' in Userform1 module
Dim Btns() as new BClass

' code to populate Btns

Public Function fnBClass(n As Long) As BClass
Set fnBClass = Btns(n)
End Function

' in BClass module
Private Sub ButtonGroup_Click()
MsgBox UserForm1.fnBClass(3).ButtonGroup.Caption
End Sub

The code you posted suggests Btns() is in the Userform, so I don't follow
how you could access it in a "General Module", as you say you can, without
doing something as above.

Regards,
Peter T
 
B

Bob Phillips

Myles,

You are not using a collection, but an array, which is why you can reference
that array by index in the userform, but the array cannot be made available
to the class.

One way around would be to use a collection, as that can be made public in
the userform.

However, the simplest way for you is probably to move the buttons()
declaration out of the userform, and declare it Public in a standard code
module. Then, in the class module, you can use code like this

Sub ButtonGroup_Click()
If ButtonGroup.Name = "cmdSubmit" Then
buttons(1).ButtonGroup.BackColor = vbRed
End If
End Sub


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
M

Myles

Hi Bob and Peter!

Yet again you've made my day. I can now access the controls from Class
Modules courtsey of the Public declaration idea. I am also particularly
enamoured of the Public Function idea for house keeping reasons.

Thank you so very much. I appreciate the help.

Myles.
 
T

TedN

Hi,
I came across this post and it seemed very similar to my problem. I tried to
use some of the solutions but got nowhere.

I create several checkboxes on a userform during runtime but I do not know
how to initiate an action when any one of them is checked or unchecked.

My code for generating the checkboxes is as follows:

Option Explicit
Dim cbx(36) As MSForms.CheckBox

Private Sub UserForm_Initialize()

Dim x As Integer

For x = 1 To 36
Set cbx(x) = Me.Controls.Add("Forms.Checkbox.1")

With cbx(x)
.Visible = True
.Enabled = True
.Top = 206 + x
.Left = 345
.Height = 12
.Width = 12
End With
Next x

End Sub

Would appreciate any help,

Thanks,
Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top