Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA userform - add label to frame

I have six small frame inside a big frame, and I had create a button to add new small frame inside the big frame with these code:

For Each cCont In Me.Controls
    If TypeName(cCont) = "Frame" Then
        lCount = lCount + 1
    End If
Next cCont

If lCount = 6 Then
    '384 is the top property of No.6 frame 
    '72 is every small frame distance
    top = 384 + 72
ElseIf lCount > 6 Then
    top = 384 + (72 * (lCount - 5))
End If

Set addBtn = bigFrame.Controls.Add("Forms.Frame.1")
With addBtn
    .Height = 66
    .Left = 6
    .top = top
    .Width = 312
    .name = "frameName" & lCount + 1
    .Caption = "frameName - " & lCount + 1
    .Font.Size = 12
End With

So here is my question, how can I add a new label in the new small frame? I know the code should be something like this:

Set label = bigFrame.frameName.Controls.Add("Forms.Label.1")

After add a new small frame, the name of new small frame it should be frameName7. What should I write in the code so that when press the button will add a small frame with label. Thanks.

like image 406
Mrk030 Avatar asked Dec 06 '25 07:12

Mrk030


1 Answers

You already have a reference to the newly inserted frame so you can just:

Dim myLabel As MSForms.Label
Set myLabel = addBtn.Controls.Add("Forms.Label.1", "MyLabel", True)

or directly from the userform with:

Set myLabel = Me.Controls("FrameName").Controls.Add("Forms.Label.1", "MyLabel", True)
like image 173
Cristian Buse Avatar answered Dec 09 '25 00:12

Cristian Buse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!