+ Reply to Thread
Results 1 to 5 of 5

Which Checkbox was clicked?

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2008
    Posts
    74

    Which Checkbox was clicked?

    I have a frame in a form that has 12 check boxes in it. I would like to respond to a click within the frame in one place (DataSelectFrame_Click()). But I need to know which check box was clicked. If I have to just use the check box name then I am repeating the same code 12 times. I can't retrieve the name of the checkbox that was clicked. If I use Me.Activecontrol.Name I get the name of the frame. How do I get the name of the control within the frame?

    I was working from this;

    Private Sub DataSelectFrame_Click()
    
        AddToArray (Right(Me.ActiveControl.Name, Len(Me.ActiveControl.Name) - 3))
    
    End Sub
    Thanks.
    Scott
    The harder it gets the happier I am.
    Finally got my signature set up!

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Which Checkbox was clicked?

    use a simple bit of code to run a subroutine.

    
    Private Sub CheckBox1_Click()
    CBManager (1)
    End Sub
    
    Private Sub CheckBox2_Click()
    CBManager (2)
    End Sub
    
    Private Sub CheckBox3_Click()
    CBManager (3)
    End Sub
    
    Private Sub CBManager(CB As Integer)
    MsgBox ("You clicked Checkbox No: " & CB & " Value: " & Me.Controls("Checkbox" & CB).Value)
    End Sub
    Last edited by mehmetcik; 02-14-2016 at 11:29 AM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Which Checkbox was clicked?

    Hi mehmetcik,

    Good work!

    I would however change your last Sub to:

    Private Sub CBManager(CB As Integer)
    MsgBox ("You clicked Checkbox No: " & CB)
    End Sub
    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Registered User
    Join Date
    05-22-2008
    Posts
    74

    Re: Which Checkbox was clicked?

    Thank you both for your responses. I didn't think it through very well before I asked and find that I have to do something like the above example, which is what I wanted to avoid. The data select frame has a list f data points the user can choose from in any order he/she wants. The result of the selection is a chart with the series displayed in the order chosen from the form. So when the user clicks on Outdoor Temperature then that item should be in the array at the next available position. But instead of having a Sub for each selection I was wanting to do something like
    Sub FrameDataSelect_Click()
    
    ' Which item was just selected
    'Here I tried;
    ' Me.Controls("FrameDataSelect").Selection 
    'to identify the object within the frame that was selected. 
    'But I am not "seeing" the object correctly.
    'Add that to the array
    'increment the array counter
    
    End Sub
    Suppose I use labels in the frame so that when the user clicks on a label I can change the forecolor to green or gold or whatever, add a - to the Caption and the position value, i.e. "Indoor Humidity - 3" meaning that this will be the third line in the chart.

    I also have a "Reset All" button that would just reset everything, but again I am having trouble accessing the objects in the way I expect.

    Private Sub cmdClear_Click()
    Dim ctrl As MSForms.Control
    Dim thisControl As Object
    
    
        For Each ctrl In Me.Controls.Item("DataSelectFrame")
            If TypeName(ctrl) = "Label" Then
                
                Me.Font.ForeColor = vbBlack
                Me.Caption = Left(Me.Caption, Len(Me.Caption) - 4)
            End If
        Next
    End Sub
    I have been away for a long time. I guess I just have to get back up to speed. The object browser shows the MSForms Controls as having font etc accessible, but my code is telling me that the object doesn't support this property on the for each line. This is related to the original question in that I am trying to grab and object and manipulate its internal objects.

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Which Checkbox was clicked?

    Sorry for the delay


    In the attached worbook.

    Paste your macro code in B1 eg:

    
    Private Sub TextBox1_Change()
    TBCh (1)
    End Sub
    Private Sub TextBox1_Enter()
    TBEn (1)
    End Sub
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TBEx (1)
    End Sub
    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    TBMd (1)
    End Sub
    Click on the button and it will create the simple Textbox, Checkbox, code for you.
    Attached Files Attached Files
    Last edited by mehmetcik; 02-14-2016 at 02:53 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 9
    Last Post: 05-23-2014, 03:50 PM
  2. How to check if Checkbox has been right clicked
    By siobeh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2014, 12:38 PM
  3. [SOLVED] How to make a non-macro button/checkbox that when clicked will copy value out of that cell
    By ladydubs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-20-2013, 12:44 PM
  4. Override the data in the cell when checkbox is clicked
    By gsweta in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-04-2009, 10:57 AM
  5. Checkbox runs same macro when clicked or unclicked
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2007, 02:23 AM
  6. name of checkbox that was clicked in a userform
    By borg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-13-2006, 04:10 PM
  7. [SOLVED] Determine when checkbox is clicked
    By wAyne in forum Excel General
    Replies: 1
    Last Post: 02-21-2006, 04:40 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1