Consulting

Results 1 to 5 of 5

Thread: Solved: VB6 - set form item value using variable names?

  1. #1
    VBAX Regular andrew93's Avatar
    Joined
    Aug 2005
    Location
    Auckland, New Zealand
    Posts
    68
    Location

    Solved: VB6 - set form item value using variable names?

    Hi

    I'm working on a VB6 project and am seeking an elegant solution to some messy coding, if possible. My form is displaying 36 pairs of outcomes based on various things, including 2 dice results. I am showing the results in a 6x6 grid made up of 36 text boxes labelled "Outcome1" through to "Outcome36". The 36 pairs of results are stored in arrays, ie. Dice1(35) & Dice2(35). Is it possible to use a loop to select each 'Outcome' text box? At the moment my code looks like this :

    Outcome1.Caption = Dice1(0) & ", " & Dice2(0)
    Outcome2.Caption = Dice1(1) & ", " & Dice2(1)
    Outcome3.Caption = Dice1(2) & ", " & Dice2(2)
    '....etc etc etc
    Is there a smarter way of doing this with a loop? If so, how do I get the code to select the correct outcome box based on a loop counter?

    Thanks in advance
    Andrew

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Just as a suggestion, I would make a control array of labels/textboxes. then loop,

    [vba]
    Dim Cntr as Long
    For Cntr=0 to 35
    Outcome(Cntr).Caption = Dice1(Cntr) & ", " & Dice2(Cntr)
    Next
    [/vba]
    HTH

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    ... or maybe
    [vba]
    k = 0
    For i = 1 To 6
    For j = 1 To 6
    k = k + 1
    Me.Controls("Outcome" & k).Caption = i & ", " & j
    Next
    Next

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    AS you are using VB6, have you setup the textboxes as a control array?

  5. #5
    VBAX Regular andrew93's Avatar
    Joined
    Aug 2005
    Location
    Auckland, New Zealand
    Posts
    68
    Location
    Thanks everyone. The control array did the trick!
    Cheers
    Andrew

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •