0

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

7 Comments

And what is, when the checkboxes not predefined in the userform? How can I add the checkboxes to the userform?
Use the Add method of the controls collection.
You mean just to define With frmOne.Controls.Add("Version" & i)...
When I do this, I'm getting the runtime error: '-2147221005 (800401f3)' invalid class string
Use the Left, Top, Height and Width properties.
|
1

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

1 Comment

look to the comment below!

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.