help please

Posted on 1998-11-18
Last Modified: 2010-05-03
I am creating an application and on one of the forms
I have created 3 combo boxes with various items inside one is all of the states ca, alabama,etc and another has seasons  winter,summer,spring,etc and another has regions north south ,east etc
I have various outcomes for each selected (example if california and north and east are selected then lblanswer.caption = "whatever"
But I cant figure out how to code this easily as there are so many different outcomes.
my question is: Is there an easy way ? and how do I code it
Question by:dlray
  • 4
  • 3

Accepted Solution

mithomas earned 50 total points
ID: 1445536
There's no way to get away from the fact that there are so many outcomes.  However, there is a fairly easy way to map the selections to text "answers":

Assuming that each combination results in different answers (or that this is the usual case), you'll need to use a multi-dimensional array.

The array must have as many dimensions as the number of item types, so in your case, that's 3 dimensions (state, season, region).

Now the hard part.  You've got to fill the array somehow.  There's no escaping this work.  It just has to be done.

Once you've got the array filled, the rest is easy.  Just use the .listindex property of the combos to figure out what member of the array to use.  I've pasted all the code you need for this below.  Start a new project and add three combos and a label to Form1.  Name the combos cmbState, cmbRegion, and cmbSeason.  Name the label lblAnswer.  Then paste the code, below into the form.
Option Explicit

    Const StateMax = 50
    Const RegionMax = 6
    Const SeasonMax = 4
    Dim Answers(1 To StateMax, _
                1 To RegionMax, _
                1 To SeasonMax _
                ) As String

Private Sub Form_Load()

    Dim i, j, k As Long
    'Fill Answers Array
    For i = 1 To StateMax
        For j = 1 To RegionMax
            For k = 1 To SeasonMax
                Answers(i, j, k) = "Answer for (" + CStr(i) + "," + CStr(j) + "," + CStr(k) + ")"
            Next k
        Next j
    Next i

    'Fill combo boxes and set to default selection
    For i = 1 To StateMax
        cmbState.AddItem "State" + CStr(i)
    Next i
    cmbState.ListIndex = 0
    For i = 1 To RegionMax
        cmbRegion.AddItem "Region" + CStr(i)
    Next i
    cmbRegion.ListIndex = 0
    For i = 1 To SeasonMax
        cmbSeason.AddItem "Season" + CStr(i)
    Next i
    cmbSeason.ListIndex = 0


End Sub

Private Sub cmbRegion_Click()
End Sub

Private Sub cmbSeason_Click()
End Sub

Private Sub cmbState_Click()
End Sub

Private Sub UpdateAnswerForSelection()
    'Handle indexing errors gracefully, since
    'at start, some listindexes will be invalid.
    On Error Resume Next
    'ListIndex is zero-based!  Could have let arrays default
    'to zero-based.  Whatever you prefer!
    lblAnswer.Caption = Answers(cmbState.ListIndex + 1, _
                               cmbRegion.ListIndex + 1, _
                               cmbSeason.ListIndex + 1)
End Sub


Good luck!

Mike Thomas
Digital Answers, LLC


Author Comment

ID: 1445537
thank you for the responce.I am new to vb im in my 6th month at the local college
ive done 2 other arrays and Im a little lost still
is there any way you could give me step by step as I cannot look at code and completely
decipher it yet.
or make a sample prog
thank you

Author Comment

ID: 1445538
Ive made the program but dont know where to put the answers in the code could you make an example with a few answers "answer1","2",etc
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.


Expert Comment

ID: 1445539
The place where it says "Fill Answers Array" is where you'd put code to put the answers in the array.  It would be better to put this into a Sub or Function, since it will become LARGE.

The bummer is that you have x*y*z entries in the array to fill, where x is states, y is regions, and z is seasons.  Depending upon how many regions you actually have (let's assume 6), this turns out to be 1200 entries in the array.  A real pain.  Anyway, the idea is you have to fill the array values "by hand".

For example, assume that the first state is Alabama:


Private Sub FillArray()
  'I don't know what your answers are supposed to be, so I'm
  'faking it here.
  Answers(1, 1, 1) = "Alabama Southeast Spring Answer"
  Answers(1, 1, 2) = "Alabama Southeast Summer Answer"
  Answers(1, 1, 3) = "Alabama Southeast Fall Answer"
  Answers(1, 1, 4) = "Alabama Southeast Winter Answer"
  Answers(1, 2, 1) = "Alabama Northeast Spring Answer"
  Answers(1, 2, 2) = "Alabama Northeast Summer Answer"
  Answers(50, 6, 4) = "Wyoming NorthWest Winter Answer"

End Sub


It occurs to me that your states must be related to the regions in some way???

It seems weird to have 3 interdependent dropdown lists where two of them sort of overlap in definition.  It seems as though if you select "Southeast", for example, that you should only see states such as Alabama, Louisiana, Florida, Georgia, etc.  Is there supposed to be a relationship like this?  If so, there's more work to do in some ways and less in others.  The reason I ask this question is why would someone select something like Florida, Northwest, and Winter?  What purpose does the selection of Florida and Northwest serve?  I guess I'm saying that I need to know more about your problem to solve it effectively at this point.


Expert Comment

ID: 1445540
When thinking about arrays, think of something physical to help.  For example, a one dimensional array is like a row of boxes, each with a number.  You can put something in each box and then give someone instructions on how to get a particular item by telling them what number to go to.  So you could hand them a piece of paper saying boxes(6), and they'd go to the 6th box and take out whatever's there.

Multidimensional arrays just add another dimension (obviously).  So you'd have a bunch of boxes laid out in a square shape on the floor and number them as to rows and columns.  With this arrangement you'd say boxes(1, 3), which would tell the person to get something out of row one, column three.

A three dimensional array would maybe be like a row of veritcal storage units full of boxes (take the square of boxes from above and turn it up on its side to be vertical).  Then you'd say boxes(3,1,2) to instruct a person to go to the third storage unit, 1st shelf, third column (or box).

It'd be easier to draw a picture, but this is the idea.

For your particular example, let's say the storage units each represent a State, the shelves represent Regions, and the boxes, numbered left to right, represent Seasons.  Can you picture this?  This is how the array works.

I hope this helps, rather than confuses.


Author Comment

ID: 1445541
It is for a fly fishing application.
select state
select region (north ,south ,east,west)
select season winter,summer,spring

and shows you the bug hatchings from your selection at season times and regions
So you will know which flies to use at proper regions and times
Also I added the other code and nothing?
still cant figure it out, how to code the answers

Expert Comment

ID: 1445542
That code was just a demonstration of something you might do!! :-)  YOU need to do all the coding for populating the array YOURSELF.  I mean, that's where the usefulness of the program comes in, it's kind of like the program's "intelligence".  I can't do that for you.

So is "region" the region of the state?  Or is it a region of the country?  If it is region of the country, then it's reasonable to expect the user to pick that FIRST, since then you can limit the state box to only states in the selected region.  If it's region of the state, then I've given all the logic you need.  You just have to do the hard work of filling in the array.

I'll try to explain it again.  Give each state a number.  This number is the number you'll use for the index of information for the state.  Give each region a number.  This is used for region indexes.  Give each season a number.  This is used for season indexes.  If you give Alabama a "1", South a "1", and Winter a "3", then the way to SET the answer for "South Alabama in Winter" is:
 Answer(1, 1, 3) = "whatever you think the answer should be"

I've given you all of the code you need and have explained arrays at length.  I don't know what else to give you at this point.  If you have specific questions, go ahead and ask them, and I'll try to answer.

Sorry if I seem grumpy, but it's late and I'm tired :-)


Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question