views:

15

answers:

1

Hey,

I've been trying to make a form with 68 items while positioning all the items on a form via a loop, but this loop isn't working :( Can someone please help me get this to work? I've tried looking everywhere but can't see what to do :/

  Dim Items(67) As String
  For x = 0 To 67
    Items(x) = "Ctl" & x
  Next

  #It goes from 1 here as I manually set up 1 otherwise (x - 1) wont work.
  For x = 1 To 67
    Form_Home.Items(x).Top = 0
    Form_Home.Items(x).Left = (Form_MainScreen.Items(x - 1).Left) + (Form_MainScreen.Items(x - 1).Width)
    Form_Home.Items(x).Height = 225
    Form_Home.Items(x).Width = 500
  Next

Thank you everyone :)

+1  A: 

What do you mean with Form_Home.Items? if you already have controls added to your forms, probably there is a Controls collection, so you can iterate trough it and set its properties, assuming each control has a name of the form Ctl0, Ctl1, Ctl2... then you can proceed as follow:

Dim ct_name as String
Dim ct_name_before as String
For x = 1 To 67
   ct_name_before = "Ctl" & CStr(Cint(x-1))
   ct_name = "Ctl" & CStr(x)
   Form_Home.Controls(ct_name).Top = 0
   Form_Home.Controls(ct_name).Left = (Form_MainScreen.Items(ct_name_before).Left) + _
                                      (Form_MainScreen.Items(ct_name_before).Width)
   Form_Home.Controls(ct_name).Height = 225
Next

Some version of VBA allow you to create array of controls sharing all the same name and having different index, then you can iterate through the array to get each control.

ArceBrito