lundi 23 mai 2016

How to dynamically increase Checkbox-Buttons 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?




Aucun commentaire:

Enregistrer un commentaire