Option Buttons Added at Run Time

R

ruddin101

I have a form that adds and systematically names an unknown number of
option groups at run-time.

I simply don't know the syntax for retrieving info about these buttons
after they've been created.

Say I've created a two option groups (1 and 2) with three buttons each
(A1, B1, C1, A2, C2, B2).
If I was creating a button ahead of time, I could use:

debug.print OptionButton1.Value

so why does
debug.print A1.Value
or
debug.print OptionButtons("A1").value
give an error message and how should I be calling it?

Eventually I'd like to loop through each option group on the form, but
I'll be off and running with just a little hint about how to reference
these option buttons. Thank you so so much!
 
P

Peter T

The form compiles when it loads so you won't be able to refer to
subsequently added objects simply by object-name. But you can use
me.controls(string-Name)...., or

Private Sub UserForm_Click()
Dim ob As MSForms.OptionButton
Dim s As String
On Error Resume Next

Set ob = Me.Controls("A1")

If ob Is Nothing Then
Set ob = Me.Controls.Add("Forms.OptionButton.1", "A1", True)
ob.Caption = "Option A1"
s = ob.Name & " created"
Else
s = ob.Name & " already existed"
End If

MsgBox s & vbCr & ob.Value
End Sub

If you need to trap events of the newly added controls you will need a
'WithEvents' class module.

Regards,
Peter T
 

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