Author Topic: Looping through controls with an array  (Read 4499 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
Looping through controls with an array
« on: November 12, 2007, 02:54:00 PM »

Hey guys,

I am stomped on this one:
I am sure there must be a way to do this;
With this project I will have app. 140 of each control type.
I am trying to do some sort of loop using an array as opposed to having to list each line out individually.

I know the below code will not work but hopefully it will show what I am trying to do in code
I am trying to perform the below action on "several" labels and images

Userform1.labelname.Caption = Userform2.Textbox.Value
Userform1.Imagename.Picture = Userform2.CMBItem.Picture


Code: [Select]
Dim Ctrl as Control
Dim Labls(20) As Integer
Dim Tboxs(20) As Integer
Dim Imgs(20) As Integer
Dim Cmbs(20) As Integer

For Each Ctrl In Userform1.Controls
 If TypeOf Ctrl Is Label Then
  If TypeOf Ctrl Is Textbox Then
   If TypeOf Ctrl Is ImageThen
    If TypeOf Ctrl Is ComboboxThen
 For Labls = 1 To 20
  For Tboxs = 1 To 20
   For Imgs = 1 To 20
    For Cmbs = 1 To 20
     Userform1.LL(labelname & Labls)L.Caption = Userform2.TBItem(Textbox & Tboxs)L.Value
     Userform1.IML(Imagename & Imgs).Picture = Userform2.CMBItem(Comboboxs & Cmbs)L.Picture
    Next
   Next
  Next
 Next
End If
Next Ctrl

End Sub

Thanks
Mark

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through controls with an array
« Reply #1 on: November 12, 2007, 03:48:10 PM »
Couple of things I see right away.  The IFs dont have any else's, so if the first is true, it crashes on second if.  I would use Select Case as it might be faster.

Second, I dont think nesting the For loops is going to work, as I think the way it stands, you have 160,000 possible iterations
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ML

  • Guest
Re: Looping through controls with an array
« Reply #2 on: November 12, 2007, 03:54:13 PM »

Yes CM

Thank you

I just tossed that code up to get the general idea across.

I agree, it would definetely be better to handle this with a Case Statement and you are right, I did miss the end if statements.

However, do you kind of get where I am going with this? And, do you think it can be done?

Otherwise, I am looking at that line of code about 800 times  :-(

Thanks

Mark

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through controls with an array
« Reply #3 on: November 12, 2007, 04:05:05 PM »
It can totally be done.  Just a matter of getting the syntax right
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through controls with an array
« Reply #4 on: November 12, 2007, 04:07:56 PM »
Do you have a pic of the intended result to show what all thats doing?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ML

  • Guest
Re: Looping through controls with an array
« Reply #5 on: November 12, 2007, 04:09:45 PM »

Yes

Check your e-mail

THank you so much!

Mark

ML

  • Guest
Re: Looping through controls with an array
« Reply #6 on: November 12, 2007, 05:40:05 PM »

CM

Here is a much better representation of what I am trying to do

Mark

Code: [Select]
Sub PopLine()

Dim Ctrl As Control
Dim Labls(20) As Integer
Dim Tboxs(20) As Integer
Dim Imgs(20) As Integer
Dim Cmbs(20) As Integer

For Each Ctrl In Form1.Controls
 If TypeOf Ctrl Is Label Then
  If TypeOf Ctrl Is TextBox Then
   For Labls = 1 To 20
    For Tboxs = 1 To 20
     Form1.LL1L(Labls).Caption = Form2.TB01(Tboxs).Value
    Next Labls
   Next Tboxs
  End If
 End If
Next Ctrl

     
For Each Ctrl In Form1.Controls
 If TypeOf Ctrl Is Image Then
  If TypeOf Ctrl Is CommandButton Then
   For Imgs = 1 To 20
    For Cmbs = 1 To 20
     Form1.IML1_1(Imgs).Picture = Form2.CMBItem01L(Cmbs).Picture
    Next Imgs
   Next Cmbs
  End If
 End If
Next Ctrl
   
End Sub

I believe for this to work; I will need to keep each group of controls together as I have above

Thanks again

Mark

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through controls with an array
« Reply #7 on: November 12, 2007, 09:30:02 PM »
I still don't think it will work b/c if you look at this
Code: [Select]
For Each Ctrl In Form1.Controls
 If TypeOf Ctrl Is Label Then
  If TypeOf Ctrl Is TextBox Then
   For Labls = 1 To 20
    For Tboxs = 1 To 20
     Form1.LL1L(Labls).Caption = Form2.TB01(Tboxs).Value
    Next Labls
   Next Tboxs
  End If
 End If
Next Ctrl
lets say your first control is in fact a Label
It passes the first IF statement and evals to TRUE, so the Then piece is If Textbox.  Which it is not, so FALSE, end if, end if, next control.  I don't see how this could possibly work because the control being eval'd can't be a label and a textbox at the same time.  Does that make sense?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Bob Wahr

  • Guest
Re: Looping through controls with an array
« Reply #8 on: November 12, 2007, 10:23:34 PM »
Mark, I agree with Duh on what he's saying.  I'm not trying to be a dick but trying to understand what you are actually trying to do is really making my brain hurt.  I think I understand what you are going for more or less but I'm probably wrong.

Code: [Select]
Sub PopLine()

Dim Ctrl As Control
Dim intCnt As Integer

For Each Ctrl In Form1.Controls
  Select Case typeof ctrl
    Case textbox
      for intcnt = 1 to 20 ' seems like you should be doing 0 to 19 to me as your index should be starting at 0 but I digress
         Form1.LL1L(IntCnt).Caption = Form2.TB01(intCnt).Value
       Next intCnt
    Case Label
       for intCnt 1 to 20
          'do your thing
        next intcnt
    Case thenextone
      'etc. ad infinitum
  End Select
next ctrl
the loop inside a loop is still is a bad thing but I can't think of what to do with it right now.  I'll try to do better tomorrow after some sleepage.

FWIW, doing this
Code: [Select]
   For Labls = 1 To 20
    For Tboxs = 1 To 20
     Form1.LL1L(Labls).Caption = Form2.TB01(Tboxs).Value
    Next Labls
   Next Tboxs
is going to write the value of Form1.LL1L(1) over Form2.TB01(1-20) the (2) will overwrite them all with that value and on and on until when it is finished, Form2.TB01(1-20) will all have the same value as Form1.LL1L(20)

To loop that "correctly" it would be
Code: [Select]
   For Labls = 1 To 20
    For Tboxs = 1 To 20
     Form1.LL1L(Labls).Caption = Form2.TB01(Tboxs).Value
    Next Tboxs
   Next Labls
but since they will have the same value there's no reason to run both loops.

SomeCallMeDave

  • Guest
Re: Looping through controls with an array
« Reply #9 on: November 12, 2007, 11:36:40 PM »
I'm not sure that I really understand what ML is trying to accomplish either.  But that has never stopped me before.

I don't think the controls can be accessed by Form1.LL1L(some integer value)  because the  name is LL1L2  as a string and the controls aren't indexed as an array when called by name.  (does that make sense?)  The controls can be accessed by name via the .Item property of the Form.Controls collection  (controls(name as string) ).  You would have to build the name using the indexing integer such as 
Code: [Select]
  BaseName & indexInteger
I think you will have to grab the Controls array for both forms and then assume that the indices of that array match from on form to the next. IOW  the first control (be it textbox or command button or what have you) on one form matches with the first control on the other form.  I'm thinking that is a BIG assumption.

If that assumption is valid,  this code will transfer the values from one form to the other.

Code: [Select]
Public Sub ChangeStuff()
    Dim crtl1 As Control
    Dim crtl2 As Control
    Dim i As Integer
    Dim currTextBox As TextBox
    Dim currLabel As Label
   
    Dim cControl1 As Controls
    Dim cControl2 As Controls
   
   
    Set cControl1 = UserForm1.Controls
    Set cControl2 = UserForm2.Controls
   
    For i = 0 To cControl2.Count - 1
        'assuming the index of the Form1 controls match the index of the Form2 controls
        '(but i don't see any way to match them other than the above assumption
        If TypeOf cControl1(i) Is Label And TypeOf cControl2(i) Is TextBox Then
            Set currLabel = cControl1(i)
            Set currTextBox = cControl2(i)
            currLabel.Caption = currTextBox.Value
        End If
    Next i

    UserForm1.Show
   
    'UserForm2.Show

End Sub


If that assumption is not valid,  there will have be some checking on the form names to insure a match.  That would probably mean grabbing a control from Form1, making sure it is the one that is desired,  then looping through all the controls on Form2 until we find the corresponding name then inserting the correct data.  The continue to the next control on Form1, loop thru Controls-2  looking for its match  etc.

These are the drunken ramblings of a sleepy man,  so use them at your own risk.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through controls with an array
« Reply #10 on: November 13, 2007, 09:27:22 AM »
These are the drunken ramblings of a sleepy man,  so use them at your own risk.
I know that feeling.  I apparently passed out from sheer exhaustion last night at the computer, and the kids thought I was dead.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

SomeCallMeDave

  • Guest
Re: Looping through controls with an array
« Reply #11 on: November 13, 2007, 09:35:31 AM »
Now that is what I call a good (if not memorable) evening   :-D

Except for scaring the kids

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Looping through controls with an array
« Reply #12 on: November 13, 2007, 09:40:47 AM »
the kids were afraid to hit me b/c they thought I would fall out of the computer chair.  When my wife got home, she saw I was breathing, so she punched me to wake me up, and sent me to bed.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ML

  • Guest
Re: Looping through controls with an array
« Reply #13 on: November 13, 2007, 10:35:56 AM »

Quote
Mark, I agree with Duh on what he's saying.  I'm not trying to be a dick but trying to understand what you are actually trying to do is really making my brain hurt.  I think I understand what you are going for more or less but I'm probably wrong.

Hey Bob, you are not being a d**k LOL LOL  :-D
I completely understand; this is insane :)

Listen guys; I said from "the get-go" that I know the code is not written correctly, I was merely trying to get a basic idea of what I was trying to do across. I try to articulate what I am asking the best I can but it is hard when I am not sure I know what the hell I am asking LOL

Anyhow, I have been told certain things can not be done in the past and in the end; I did get them.
The question is; is it worth the time and energy to do it? hummmm  :-(

Bob, you and CM are both correct; a Select Case Statement would have been much better and it is where I would have ended up.
I just tossed up what I had in a somewhat pseudo fashion.

The below code is pretty close Bob, I think
And yes, you are correct, we need to start the index at 0.
Man, my brain hurts too guys LOL

In any case, I am sorry if I have permanently damaged anyone's brains LOL
I just have a philosophy; if you can conceive it, then you can achieve it.
So, with this project, I may just take the long road as the results are the same but I always "try" to be a minimalist with code.
So, just think, if anyone (including myself) can figure this one out, it could benefit us all later :)

Thanks guys!

Mark

Code: [Select]
Dim Ctrl As Control
Dim intCnt As Integer

For Each Ctrl In Form1.Controls
  Select Case typeof ctrl
    Case textbox
      for intcnt = 1 to 20 ' seems like you should be doing 0 to 19 to me as your index should be starting at 0 but I digress
         Form1.LL1L(IntCnt).Caption = Form2.TB01(intCnt).Value
       Next intCnt
    Case Label
       for intCnt 1 to 20
          'do your thing
        next intcnt
    Case thenextone
      'etc. ad infinitum
  End Select
next ctrl




ML

  • Guest
Re: Looping through controls with an array
« Reply #14 on: November 13, 2007, 10:37:21 AM »

I must admit, you guys are troopers and I really do appreicate this forum A LOT!

Thanks guys!