+ Reply to Thread
Results 1 to 3 of 3

Record the checkboxes value which were created programmatically

  1. #1
    Registered User
    Join Date
    01-24-2020
    Location
    Toronto
    MS-Off Ver
    Professional Plus 2016
    Posts
    4

    Record the checkboxes value which were created programmatically

    I have a userform, in initial instance I have 7 checkboxes with a command button the user can create a new row with same numer of checkboxes (i.e., 7) and another button to delete the same row. Till here it works fine, my next stage is when the user checks one or all checkboxes from the newly created row, I am have difficulty in recording which checkbox was clicked and visa-versa.

    I have used class module to trigger the events, it works perfectly when there are no predefined checkboxes in the form, but once I add the initial 7 checkboxes the msgbox displays the checkox23, checkbox99, checkbox278 as their names.

    In userform I have this code -

    Dim chkBoxEvent As clsBoxEvent
    Dim chkBoxColl As Collection

    Private Sub btnAddClass_Click()
    Dim ctrl As Control, newCtrl As Control, offsetTop As Integer
    Set chkBoxColl = New Collection

    offsetTop = 36

    For Each ctrl In Me.Controls
    If TypeName(ctrl) <> "CommandButton" Then
    If ctrl.Top = btnAddClass.Top - offsetTop Then
    If TypeName(ctrl) = "ComboBox" Then
    Set newCtrl = Me.Controls.Add("Forms.ComboBox.1")
    ElseIf TypeName(ctrl) = "TextBox" Then
    Set newCtrl = Me.Controls.Add("Forms.TextBox.1")
    End If
    nchk = 7
    If TypeName(ctrl) = "CheckBox" Then
    For i = 1 To nchk
    Set newCtrl = Me.Controls.Add("Forms.Checkbox.1")

    With newCtrl
    .Height = ctrl.Height
    .Width = ctrl.Width
    .Top = ctrl.Top + offsetTop
    .Left = ctrl.Left
    .Tag = nchk * 10
    End With
    Set chkBoxEvent = New clsBoxEvent
    Set chkBoxEvent.cBox = newCtrl
    chkBoxColl.Add chkBoxEvent
    Next
    End If
    If TypeName(newCtrl) <> "CheckBox" Then
    With newCtrl
    .Height = ctrl.Height
    .Width = ctrl.Width
    .Top = ctrl.Top + offsetTop
    .Left = ctrl.Left
    End With
    End If
    End If
    End If
    Next ctrl

    btnAddClass.Top = btnAddClass.Top + offsetTop
    btnRemoveClass.Top = btnRemoveClass.Top + offsetTop
    Me.Height = Me.Height + offsetTop
    End Sub

    Private Sub btnRemoveClass_Click()
    Dim ctrl As Control, offsetTop As Integer
    offsetTop = 36

    For Each ctrl In Me.Controls
    If TypeName(ctrl) <> "CommandButton" Then
    If ctrl.Top = btnAddClass.Top - offsetTop Then
    Me.Controls.Remove (ctrl.Name)
    End If
    End If
    Next ctrl
    btnAddClass.Top = btnAddClass.Top - offsetTop
    btnRemoveClass.Top = btnRemoveClass.Top - offsetTop
    Me.Height = Me.Height - offsetTop
    End Sub

    And in Class Module -
    Public WithEvents cBox As MSForms.CheckBox

    Private Sub cbox_click()
    msgbox cBox.Name & " clicked. Value is now " & IIf(cBox.Value, "Checked", "Unchecked")
    End Sub
    Attached Files Attached Files
    Last edited by Praveen_k_g; 04-29-2020 at 09:36 AM.

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2019
    Posts
    6,551

    Re: Record the checkboxes value which were created programmatically

    Please heed the forum rules when posting code.
    Read the yellow banner
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    01-24-2020
    Location
    Toronto
    MS-Off Ver
    Professional Plus 2016
    Posts
    4

    Re: Record the checkboxes value which were created programmatically

    So sorry about that.

+ 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. [SOLVED] When record is created/deleted in Table1, then add/delete record to Table2
    By michellepace in forum Excel General
    Replies: 1
    Last Post: 11-26-2015, 04:45 AM
  2. Programmatically created form with name conflict
    By Jacafio in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-23-2015, 12:53 PM
  3. Replies: 1
    Last Post: 01-17-2012, 10:47 AM
  4. Replies: 0
    Last Post: 01-17-2011, 07:13 AM
  5. [SOLVED] Help:Programmatically Adding Checkboxes with Captions
    By josh@shift4.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-19-2006, 03:15 PM
  6. Detect change in programmatically created checkboxes
    By TomJones322 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2005, 07:21 PM
  7. [SOLVED] Adding checkboxes programmatically to a worksheet
    By Stef in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-01-2005, 12:06 PM

Tags for this Thread

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