简体   繁体   中英

How to dynamically add Checkbox-Buttons to userform in VBA

I have a userform with 5 checkbox buttons for 5 pdf versions. Well, when the user calls the userform, then the userform initializes 5 checkbox buttons to select one of them. At the moment, the code is very static and not so good.

Here the example:

If rs.EOF = False Then
  Do Until rs.EOF Or i = 5
     Select Case i
       Case Is = 0
         frmOne.Version5.Visible = True
         frmOne.Version5.Caption = rs!versNo & "#" & rs!versFrom
         frmOne.Version5.tag = rs!versNo & "_" & rs!FiD & ".pdf"
       Case Is = 1
         frmOne.Version4.Visible = True
         frmOne.Version4.Caption = rs!versNo & "#" & rs!versFrom
         frmOne.Version4.tag = rs!versNo & "_" & rs!FiD & ".pdf"
       Case Is = 2
         frmOne.Version3.Visible = True
         frmOne.Version3.Caption = rs!versNo & "#" & rs!versFrom
         frmOne.Version3.tag = rs!versNo & "_" & rs!FiD & ".pdf"
       Case Is = 3
         frmOne.Version2.Visible = True
         frmOne.Version2.Caption = rs!versNo & "#" & rs!versFrom
         frmOne.Version2.tag = rs!versNo & "_" & rs!FiD & ".pdf"
       Case Is = 4
         frmOne.Version1.Visible = True
         frmOne.Version1.Caption = rs!versNo & "#" & rs!versFrom
         frmOne.Version1.tag = rs!versNo & "_" & rs!FiD & ".pdf"
    End Select
    i = i + 1
    rs.MoveNext
  Loop
End If

To much code I think. So my intention was to define it like the example below, but this doesn't work:

If rs.EOF = False Then
    For i = 1 To 5
      With frmOne
         .Version & i &.Visible = True
         .Version & i &.Caption = rs!versNo & "#" & rs!versFrom
         .Version & i &.tag = rs!versNo & "_" & rs!FiD & ".pdf"
      End With
      rs.MoveNext
    Next i
End If

Do have anyone an idea how could I fix that?

go like follows:

If rs.EOF = False Then
    For i = 1 To 5
        With frmOne.Controls("Version" & i) '<~~ use Controls collection of Userform object
           .Visible = True
           .Caption = rs!versNo & "#" & rs!versFrom
           .Tag = rs!versNo & "_" & rs!FiD & ".pdf"
        End With
        rs.MoveNext
    Next i
End If

You can refer to the Controls collection using the name:

If rs.EOF = False Then
    For i = 1 To 5
      With frmOne.Controls("Version" & i)
         .Visible = True
         .Caption = rs!versNo & "#" & rs!versFrom
         .tag = rs!versNo & "_" & rs!FiD & ".pdf"
      End With
      rs.MoveNext
    Next i
End If

To actually add the controls at runtime too:

Do While not rs.EOF
    i = i + 1
      With frmOne.Controls.Add("Forms.CheckBox.1", "Version" & i, True)
         .Caption = rs!versNo & "#" & rs!versFrom
         .tag = rs!versNo & "_" & rs!FiD & ".pdf"
      End With
      rs.MoveNext
Loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM