Consulting

Results 1 to 5 of 5

Thread: How do I access the Click Event of controls added at run time?

  1. #1

    How do I access the Click Event of controls added at run time?

    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

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Chris,
    Welcome to VBAX,
    Can you post a copy of your code so we can see your control names etc.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3

    Cool The Code

    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":

    [VBA] 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[/VBA]

    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?

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Chris

    Where are you doing this?

    Are you sure ! is the correct qualifier?

  5. #5

    Your have several options. Here is one of them...

    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...

    [vba]
    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
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •