0

how can you add multiple checkboxes to a userForm Frame? This seems like a trivial thing to do, but my code is only generating a checkbox for last item in array.

Private Sub btnGenerate_Click()
Dim i As Long
Dim lic As licence
Dim temp As Variant
Dim desc As String
Dim chkbox As MSForms.CheckBox
Dim str As String

For Each lic In licenceCollection
    temp = lic.getClause
Next lic

For i = LBound(temp) To UBound(temp)
    'Debug.Print temp(i)
    desc = "Future-Sampling " & i
    'Utility.createCheckBoxes temp(i), desc
    Set chkbox = licenceForm.resultFrame.Controls.Add("Forms.Checkbox.1", desc)
    chkbox.Caption = temp(i)
    chkbox.Value = desc
    chkbox.Width = "450"
    chkbox.Height = "50"
    chkbox.WordWrap = True
    chkbox.Value = False
    chkbox.GroupName = "Future Sampling"
Next
End Sub  

Any suggestions here much appreciated. Thanks in advance.

6
  • You need an i on your last Next... Commented Dec 6, 2016 at 17:29
  • @Rdster "Next " + variable serves just for retaining value of i outside the scope of loop. Commented Dec 6, 2016 at 17:34
  • 1
    @holmicz - It has nothing to do with scope. There's no function difference between Next and Next i at all. It's purely a matter of preference and coding style. Commented Dec 6, 2016 at 17:43
  • @Rdster You are right, I don't know where I got it from. :-) Commented Dec 6, 2016 at 17:47
  • 1
    Except help noobs keep their code easier to follow and read. Commented Dec 6, 2016 at 17:58

3 Answers 3

1

They're all being created just fine, but the reason you can only see the last one is that they're all stacking on top of each other in the default position when they're added. You need to position them with the .Top and .Left properties:

Dim xPos As Long
For i = LBound(temp) To UBound(temp)
    desc = "Future-Sampling " & i
    Set chkbox = Me.Controls.Add("Forms.Checkbox.1", desc)
    With chkbox
        .Top = xPos
        .Caption = temp(i)
        .Value = desc
        .Width = 450
        .Height = 24
        .WordWrap = True
        .Value = False
        .GroupName = "Future Sampling"
        xPos = xPos + 24
    End With
Next
Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem is, that you are adding components on the same position, therefore it is overlapping and you are seeing only last one, isn't it? You need to adjust left and top coordinate, if I remember correctly.

Comments

0

Thanks all, that seemed to do the trick! I never figured this cont

Revised code snippet below,

      For i = LBound(temp) To UBound(temp)
                desc = "Future-Sampling " & i
                Set chkbox = Me.resultFrame.Controls.Add("Forms.Checkbox.1", desc)
                With chkbox
                    .Top = xPos
                    .Caption = temp(i)
                    .Value = desc
                    .Width = 450
                    .Height = 24
                    .WordWrap = True
                    .Value = False
                    .GroupName = "Future Sampling"
                    xPos = xPos + 24
                End With


        Next i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.