Verify Value Of All Controls Added Dynamically Upon Exit Using With Events Method

angsuman

New Member
Joined
Aug 19, 2015
Messages
30
Hello All,

I have created a blank user form. Dynamically I have added Text Box, Label, Check Box and command Button to it. I have been able to handle the even generated while command button is clicked.Upon clicking the command button, user form will be unloaded. However I am trying to validate the value entered into Text Box and verify check box is selected and this is where I cannot access them using following code. Please help:

Code:
=========================
Code In Unload
=========================


Dim clsCommand () As New clsCommandClass


CcCount = 2
k = 0


Public Sub UserForm_Initialize()
   For i = 1 To CcCount
      Set CcB = CcDataCollect.Controls.Add("Forms.TextBox.1", "CcBox" & i)
      k = k + 1
      ReDim Preserve clsCommand(1 To k)
      Set clsCommand(k).clsCmdGroup = CcB
      
      Set CcB = CcDataCollect.Controls.Add("Forms.TextBox.1", "CcdBox" & i)
      k = k + 1
      ReDim Preserve clsCommand(1 To k)
      Set clsCommand(k).clsCmdGroup = CcB
      
      Set CcBt = CcDataCollect.Controls.Add("Forms.CheckBox.1", "CcCheckBoxA" & i, True)   
      k = k + 1
      ReDim Preserve clsCommand(1 To k)
      Set clsCommand(k).clsCmdGroup = CcBt
      
      
      Set CcBt = CcDataCollect.Controls.Add("Forms.CheckBox.1", "CcCheckBoxB" & i, True)
      k = k + 1
      ReDim Preserve clsCommand(1 To k)
      Set clsCommand(k).clsCmdGroup = CcBt
   Next i	
   
   Set clcb = CcDataCollect.Controls.Add("Forms.CommandButton.1", "CloseComButton")
   k = k + 1
   ReDim Preserve clsCommand(1 To k)
   Set clsCommand(k).clsCmdGroup = clcb  
End Sub


=========================
Code In Class clsCommand
=========================
Option Explicit
Public WithEvents clsCmdGroup As MSForms.CommandButton


Private Sub clsCmdGroup_Click()
                                  <----- This is where I want to validate the value and if text box value is incorrect or is 
                                            empty the focus  
                                            should go back to Text Box
   Unload CcDataCollect
End Sub

Please help

Thanks
Angsuman
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Check the following

In userform1 put the code:

Code:
Dim colTbxs As Collection 'Collection Of Custom
Dim alto
'
Private Sub UserForm_Initialize()
    Dim ctlLoop As MSForms.Control
    Dim clsObject As Clase1
    Set colTbxs = New Collection
    alto = 10
    For i = 1 To 5
        Set CcB = UserForm1.Controls.Add("Forms.TextBox.1", "CcBox" & i)
        With CcB
            .Height = 18: .Width = 48: .Left = 10: .Top = alto
            alto = alto + 25
        End With
        '
        Set clsObject = New Clase1
        Set clsObject.tbxCustom1 = CcB
        colTbxs.Add clsObject
    Next
    Set clcb = UserForm1.Controls.Add("Forms.CommandButton.1", "CloseComButton")
    With clcb
        .Caption = "close": .Height = 18: .Width = 48: .Left = 100: .Top = 10
    End With
    Set clsObject = New Clase1
    Set clsObject.tbxCustom2 = clcb
    colTbxs.Add clsObject
End Sub

In Class1 the code:
Code:
Option Explicit
Public WithEvents tbxCustom1 As MSForms.TextBox         'Custom Textbox
Public WithEvents tbxCustom2 As MSForms.CommandButton   'Custom Command
'
Private Sub tbxCustom2_Click()
    Dim ctr
    For Each ctr In UserForm1.Controls
        If TypeOf ctr Is MSForms.TextBox Then
            If ctr.Value = "" Then
                MsgBox "Textbox is empty: " & ctr.Name
                ctr.SetFocus
                Exit Sub

            End If
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top