Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox Arrays

Status
Not open for further replies.

simoncpage

Programmer
Apr 4, 2002
256
GB
Is there anyway in VBA to create Arrays of checkboxes, I have no problem in VB6 but how do I do this in VBA. Im stuck and any help would be appreciated thanks

Simon
 
Unfortunately, VBA does not support control arrays.
 
' paste this in the code window of a blank form
' I tried this in Word 97 and Excel 97


Code:
Option Explicit

Private maChkBoxes(1 To 3) As Object
Private WithEvents MyCommandButton As CommandButton

Private Sub MyCommandButton_Click()
  Dim i As Long
  Dim sMsg As String
  
  For i = 1 To 3
    sMsg = sMsg & maChkBoxes(i).Caption & " is " & _
           IIf(maChkBoxes(i).Value, "", "not ") & _
           "checked" & vbCrLf
  Next i
  
  MsgBox sMsg
End Sub

Private Sub UserForm_Initialize()
  Dim i As Long
  
  Set MyCommandButton = Controls.Add("Forms.CommandButton.1", "MyCommandButton")
  MyCommandButton.Top = (Me.InsideHeight - MyCommandButton.Height) / 2
  MyCommandButton.Left = (Me.InsideWidth - MyCommandButton.Width) / 2
  MyCommandButton.Caption = "Click me!"
  MyCommandButton.Visible = True
  
  
  For i = 1 To 3
    Set maChkBoxes(i) = Controls.Add("Forms.CheckBox.1", "MyChkBox" & i + 1)
    maChkBoxes(i).Top = (i - 1) * maChkBoxes(i).Height
    maChkBoxes(i).Caption = "Box " & i
    maChkBoxes(i).Visible = True
  Next i
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top