PDA

View Full Version : How do I access the Click Event of controls added at run time?



munhasen
12-11-2006, 10:02 AM
I have added some option button controls in a custom user form using the Add Method. This was no problem. But when a user clicks on the one of these option buttons, I want some new controls added to that same form, based on which option button he/she has clicked.

Does anyone know how I can access the Click event of these controls?

Thanks!

Chris

mdmackillop
12-11-2006, 01:38 PM
Hi Chris,
Welcome to VBAX,
Can you post a copy of your code so we can see your control names etc.
Regards
MD

munhasen
12-14-2006, 07:28 AM
Here's the setup:

I have a label box that asks "Are you starting a new product or updating an existing product?" Below that are two option buttons that were created by manually adding controls like normal.

- The first option button ("optnNewProduct") reads " New Product".

- The second option button ("optnExisitingProduct") reads " Updating Exisiting Product".

When they click either of the options two more options appear below them, which I added using the "Add Method". Here is the code when the user clicks "Updating Exisiting Product":

Private Sub optnExisitingProduct_Click()
Set lblGT = Controls.Add("Forms.Label.1", lblGrapicsOrText, Visible)
lblGT.Height = 12
lblGT.Left = 60
lblGT.Top = 120
lblGT.Width = 240
lblGT.Caption = "Are you updating the status of the graphics or the text?"

Set optnG = Controls.Add("Forms.OptionButton.1", optnGraphics, Visible)
optnG.Height = 18
optnG.Left = 84
optnG.Top = 144
optnG.Width = 48.75
optnG.Caption = "Graphics"

Set optnT = Controls.Add("Forms.OptionButton.1", optnText, Visible)
optnT.Height = 18
optnT.Left = 156
optnT.Top = 144
optnT.Width = 36.75
optnT.Caption = "Text"
End Sub

This creates the following:

- A label with the caption "Are you updating the status of the graphics or the text?"
- An option button with the caption "Graphics"
- An option button with the caption "Text"

So far so good. What I am having a problem with is having more option buttons appear when the user clicks either "Graphics" or "Text". To refer to an added control (i.e. to set it properties) you refer to the userform name and place an exclamation point after it then the name of the added control...like:

-userform1!optnGraphics

However, I tried creating a "Click Event" using with that format, but I get a "Compile Error: Expected end of statement" error when doing this.

Any thoughts?

Norie
01-07-2007, 12:59 PM
Chris

Where are you doing this?

Are you sure ! is the correct qualifier?

tstom
01-07-2007, 03:23 PM
There are other methods such as consolidating events sourced from a custom object into a callback. The following method is the easiest to implement and may do well enough for you.

Note that there may be no good reason to name the controls if you retain a module level reference. Set all of your properties using these references. Also, you should provide a groupname for each group of optionbuttons...

See the attached file to watch a working example...


Option Explicit

Private lblGT As MSForms.Label
Private WithEvents optnG As MSForms.OptionButton
Private WithEvents optnT As MSForms.OptionButton

Private Sub optnExisitingProduct_Click()
If (lblGT Is Nothing) And (optnG Is Nothing) And (optnT Is Nothing) Then
Set lblGT = Controls.Add("Forms.Label.1", , Visible)
lblGT.Height = 12
lblGT.Left = 60
lblGT.Top = 120
lblGT.Width = 240
lblGT.Caption = "Are you updating the status of the graphics or the text?"

Set optnG = Controls.Add("Forms.OptionButton.1", , Visible)
optnG.Height = 18
optnG.Left = 84
optnG.Top = 144
optnG.Width = 48.75
optnG.Caption = "Graphics"
'note that you should group your options
optnG.GroupName = "GraphicsOrText"

Set optnT = Controls.Add("Forms.OptionButton.1", , Visible)
optnT.Height = 18
optnT.Left = 156
optnT.Top = 144
optnT.Width = 36.75
optnT.Caption = "Text"
'note that you should group your options
optnT.GroupName = "GraphicsOrText"
End If
End Sub

Private Sub optnG_Click()
MsgBox "You clicked the ""Graphics"" option."
End Sub

Private Sub optnT_Click()
MsgBox "You clicked the ""Text"" option."
End Sub