Solved

help please

Posted on 1998-11-18
7
149 Views
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
Thanx
0
Comment
Question by:dlray
  • 4
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
mithomas earned 50 total points
Comment Utility
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

    UpdateAnswerForSelection

End Sub

Private Sub cmbRegion_Click()
    UpdateAnswerForSelection
End Sub

Private Sub cmbSeason_Click()
    UpdateAnswerForSelection
End Sub

Private Sub cmbState_Click()
    UpdateAnswerForSelection
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
www.digitalanswers.com
----------------------

0
 

Author Comment

by:dlray
Comment Utility
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
d.ray@mindspring.com
0
 

Author Comment

by:dlray
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:mithomas
Comment Utility
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.

later...
0
 
LVL 1

Expert Comment

by:mithomas
Comment Utility
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.

later...
0
 

Author Comment

by:dlray
Comment Utility
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
0
 
LVL 1

Expert Comment

by:mithomas
Comment Utility
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 :-)

later...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now