I need to create the code for CommandButton that will be created during run-time. This command button is dynamic because it based on the user data.
User_From code
Private Sub UserForm_Activate()
Dim ctlTXT As Control
For RevNo = 1 To RevCounter
Set ctlTXT = Me.Controls.Add("Forms.CommandButton.1")
ctlTXT.name = RevNo
ctlTXT.Caption = Sheet4.Range("D" & RevNo + 4).value
ctlTXT.Left = 18
ctlTXT.Height = 18: ctlTXT.Width = 72
ctlTXT.Top = 15 + ((RevNo - 1) * 25)
Next
Me.Height = (RevNo * 17) + 50
ReDim Preserve cmdArray(1 To RevNo)
Set cmdArray(RevNo).CmdEvents = ctlTXT
Set ctlTXT = Nothing
End Sub
Class module Code
Private Sub CmdEvents_Click()
Dim i As Integer
i = CmdEvents.name
RevisionFormPrevious.LblResponsible.Caption = Sheet4.Range("C" & i +4).value
RevisionFormPrevious.LblEdition.Caption = Sheet4.Range("D" & i + 4).value
RevisionFormPrevious.LblTelNo.Caption = Sheet4.Range("E" & i + 4).value
RevisionFormPrevious.LblFeatures.Caption = Sheet4.Range("D" & i + 4).value
RevisionFormPrevious.Features.value = Sheet4.Range("F" & i + 4).value
Load RevisionFormPrevious
RevisionFormPrevious.Show
End Sub
The problem is, if there are more than one button created, the code only works for the last button created. When the first and second button clicked, nothing happened.
RevCounterdeclared, populated and what is its' value when called?Classcode you need to add at the topPublic WithEvents CmdEvents As MSForms.CommandButton, Then, in yourSubyou need to define it usingDim ctlTXT As As MSForms.CommandButton