Re: student question/ array

From: Jim Carlock (anonymous_at_127.0.0.1)
Date: 04/10/04


Date: Sat, 10 Apr 2004 12:30:59 -0400

Okay, that's a lot better.

Troy Scott explained the following:
********************************
Create a project to analyze an income survey. The statistics for
each home include an indentification code, the number of
members in the household, and the yearly income.
********************************

A menu will contain File, Reports, and Help.
The File menu will contain Enter Data, and Exit.
Data is entered through text boxes on a form.
As each item is entered, the user will commit the information
into a table.

The printable reports needed are as follows:
(a) A three-column report displaying the id, homecount, and totalincome.
(b) A report of identification number and income for each household
that exceeds the average income. (This is the same as (a) but it compares
the totalincome against an averageincome figure.)
(c) The percentage of households having incomes below the poverty
level.

Poverty Level: 8000 for a family of one or two, plus 2000 for each
additional member.

********************************
Okay, we need to define several things. Being that all the income levels
are going to be positive integers, we will use a Long variable to
represent these.

You can include this information in your report if you'd like, but some
of it won't be needed. We are using Long instead of Integer, because
all operating systems these days work quicker with Long variables
than with Integer variables. Long also means that the corresponding
incomes we can record will be able to cover a wider ranger and
these facts make us want to use Long's over Integer's.

Private Const BASE_POVERTY_INCOME As Long = 8000&
Private Const ADD_POVERTY_PER_KID As Long = 2000&
Private miHomeCount As Long 'holds current record info
Private miTotalIncome As Long 'holds current record info
Private miID As Long 'holds current record info
Private miDataCount As Long 'number of records in store
Public Type typData
  id As Long
  iHomeCount As Long
  iTotalIncome As Long
End Type
Public arrData(0) As typData

We will use a combobox for the ID. Not for the number of folks
in the house, as the number of folks can well exceed 20, esp. in
oriental, and muslim families with multiple wives. ;-)

It looks like you'll need to provide a good reason for using a
textbox instead of a combobox for the number, and here it is:

The combobox will be used to select a record. This is a much
more effective useage for a combo box. The number that people
enter for the number of people is a variable number and a
textbox would be better suited for this application. ;-)

We are setting up the form to do several things.
(1) We want to be able to browse the data.
(2) We want to be able to view reports.
(3) We want to be able to edit and add data.

The reports will be displayed in RichTextBoxes, and using a
separate form.

The data entry and data browsing form will be the same form
with a button that can save data/edit data, a button that can be
used to cancel an edit or close the form, and a four navigation
buttons that are hidden when the form goes into an editing
mode.

So create a form with three (2) text boxes and one combobox.

Name the items:
frmData
  cbxID
  cmdClose
  cmdPrev
  cmdNext
  cmdFirst
  cmdLast
  cmdEdit
  cmdAdd

frmReport
  rtbReport

When the cmdEdit button is clicked, the caption will change to &Save
and the cmdClose caption will change to &Cancel. cmdAdd will
disappear.

When we click on the cmdAdd button the buttons will behave in the
same manner that the cmdEdit buttons behaved. Thus we can use
cmdAdd.Visible to determine if we are in a browsing or editing
state.

When the cmdClose button is clicked, we exit the program if we're in
a browsing mode (as the caption will state "&Close").

When browsing, cbxID will be enabled, when editing (or adding) it will
be disabled.

When you add an item to the data list, you'll need to use code such as
the following:

ReDim arrData((miDataCount + 1))

You'll need to open the .dat file for append and add the record in some
format, comma separated, spaced, it doesn't matter.

When in browsing mode, the cbxID will be enabled, and all the text
boxes will be disabled, and while in editing mode (this includes append),
the text boxes will be all editable except the id text box. The ID field
should be maintained by code.

You can place code in the Click event to display the appropriate array
item.

We are using a User Defined Type (UDT) to hold the information. It
is created as an array and we will create a procedure to fill that array
in... There should be the following procedures.

subReadData
subAddRecord
subEditRecord

All subEditRecord will do, is the following:

Private Sub subEditRecord()
txtID.Enabled = False
txtTotInc.Enabled = True
txtHomeCount.Enabled = True
cbxID.Enabled = False
cmdEdit.Caption = "&Save"
cmdClose.Caption = "&Cancel"
cmdAdd.Visible = False
cmdPrev.Visible = False
cmdNext.Visible = False
cmdFirst.Visible = False
cmdLast.Visible = False
End Sub

Code the cmdEdit button as follows:
Private Sub cmdEdit_Click(...)
  If cmdAdd.Visible Then
    'we are in browse mode
    subEditRecord
  Else
    'we are in some form of edit mode and the button reads as Save
    subSaveRecord
  End If
End Sub

The navigation buttons will require some coding, but the above should
be enough to get you started. If you have any questions, feel free to
ask.

I'm going to tell you that I'm going to go against your professor's wishes
in using a combobox to grab a number for user data entry. It does two
things to the data in the survey. It is imperfect and creates problems,
and it does NOT give a correct number for families under 4.

This is no where complete, I know, but it should help you get started.

-- 
Jim Carlock
http://www.microcosmotalk.com/
Post replies to the newsgroup.
"Troy Scott" wrote:
Jim, maybe I didn't make myself clear.  This is a problem in the
current chapter we are dealing with.  The problem originally stated
it wanted the student to create a project to analyze an income
survey. Here is the problem in full.
Test Data
This where the test data is, too much to list.
Then the professor hands out a *** with some changes he wants:
Modify the menu structure.
File
> Enter Data - Enter new survey data. Be certain to initialize the array.
> Open Survey File - Retrieve data from the data file and place in the
array.
> Save Survey File - Write the data from teh array to the survey data file.
Reports
> Survey Data - All survey data
> High Income - household totalincome exceeding the average income
> Poverty - The percentage of households with Poverty incomes.
Help
> About
Combo Box Data Entry.  When allowing for data entry, use a combo box to
permit selection of "Number of Persons".  The list components of the combo
box should allow for family sizes, or "Number of Perons" up to 4.  However,
larger family sizes should be permitted by entry into the text component of
the combo box. Describe in the comments of the program abstract which type
of combo box you have selected and why you made this choice.
Report Generation.  Produce reasonably formatted reports for all reports.
Describe the contents of each report in the comments of your program
adbstract.
Processing a Data File.  When saving the survey data in a sequential data
file, name the dat file SURVDATA.DAT and be certain that the data file is
stored in the project directory for this project.  If necessary you may need
to sue the APP.PATH techniques described in the text.  You may need to
review the first portion of chapter 10. The use of APP.PATH is described in
this chapter in the section "Locating a File".
********************************
So, now you know as much as I do.  Like I said, he hasn't been much help at
all on this, much less this semester.  Not once has he sat down and use his
computer to display sample code or even used it for teaching.  He lectures
the whole time.  95% of the class time is lecture.  Go figure.
Troy
"Jim Carlock" <anonymous@127.0.0.1> wrote in message
news:OX#l6#vHEHA.4092@TK2MSFTNGP11.phx.gbl...
> It sounds like the typical student thing where they leave alot up to
> the student to determine the best way to do this. I've seen alot of
> instructors work in this manner, and I think they're looking for
> creativity.
>
> The way I'd think of it is they want a database. It would help if we
> knew why a number would be stored in a combo box and what
> that number really represents. Is it the number of persons in a
> particular city, state, country? You might want to start here.
>
> Restating things means you understand what is being asked,
> and if it's done in front of the class, will show leadership and that
> you are willing to step up to the plate and it will help the rest of
> the class as well. Don't worry about what they want to teach,
> worry about what the problem is. Find out the requirements
> that are needed. Alot of times the professor will indicate that
> it is up to YOU. But I really don't think that is appropriate,
> because things in real life are more defined and the requirements
> are more specific, and the end result will be that you will have
> accept their abstract problem (and this where your creativity
> will come into play, because this is where you redefine what
> the problem really is and you create something that goes
> beyond what is required, and you'll have an out if the project
> you've created doesn't represent what was asked). If needed
> restate the problem, put it in writing and get the professor to
> sign it.
>
> I'd present the professor with something as follows:
>
> "I understand that you want a database to hold the following
> information:
>
> ID, AVGANNUALINCOME, and CITYPOPULATION
>
> and that you will use this information to determine the average
> shoe size, which in turn will be used to determine digit size,
> which in turn will be used to determine the size of the seat of the
> commode, in to prevent kids from falling into commercial
> commodes and to prevent these from overflowing into
> classrooms."
>
> I've done this in this past with professors that gave quite abstract
> problems and it works. Sometimes professors give you a broken
> problem and look for you to fix what they've broken. It can be
> used in a good manner to help students understand that they need
> to ask questions when a problem is presented to them. The
> student (or person working on a project) must get a clear
> understanding of the project and then take the project from that
> point and work a solution.
>
> There really is no need to put a number that represents the number
> of persons, in a combobox, and that doesn't make any sense at all.
> So ask the professor about what kind of purpose there is for a
> number being put into a combobox. It is usually the id that goes
> into a combobox, or a limited list of words that are put there,
> as in the case of country names, state names, city names, etc.
>
> If you really want to impress the guy, make sure you understand,
> the problem. If he is looking for creativity, be creative.
>
> As for the problem, I'd present a form with three fields. Make the
> ID field the combobox. Add a fourth field and call it CityName.
>
> You can get to this goal, in several different ways. I'd use an
> Access database if you have availability to one. Do you have an
> Access Database?
>
> --
> Jim Carlock
> http://www.microcosmotalk.com/
> Post replies to the newsgroup.
>
>
> "Troy Scott mindspringcom>" <tigerweld<removebrakets> wrote:
> no Jim, I need for it to be 14 rows with 3 columns.  The first column will
> hold an ID Number, second column is Annual Income, and third column is
> Number of Persons.  To be honest we have a terrible teacher for a
professor.
> He didn't go over arrays well and the whole class is lost.
>
> The user must be able to use a text box for Annual Income and ID Number,
the
> Number of Persons must be a combo box.  User must be able to enter all
this
> data for 14 rows, then save it as a file, be able to load the file back
into
> the array and print it.  I believe I can figure out the rest if I can just
> get started on entering the data into an array.  Like I said the professor
> didn't go over this part at all, nor does the text book even mentions it!
>
> Thanks,
>
> Troy
>
> "Jim Carlock" <anonymous@127.0.0.1> wrote in message
> news:uX35TpeHEHA.2164@TK2MSFTNGP12.phx.gbl...
> > You will need to set up a loop to loop through the items.
> >
> > Dim i As Integer
> > For i = 1 To 13
> >   mintIncomeSurvey(i, 1)  = Val(Text1.Text)
> >   '...whatever other code you need to set the other stuff up
> > Next i
> >
> > That would put the same value into the first column of the array
> > you have set up.
> >
> > Was there something that we might have overlooked?
> >
> > Did you want 13 text boxes? What exactly did you want
> > to accomplish. Feel free to describe it in real world terms,
> > ie, you'd like to store name and address information, you'd
> > like to set up a mortgage payment schedule, etc.
> >
> > Alot of times combo boxes are used to select items and
> > then information is put into other places, ie textboxes, see
> > real world example of javascript at:
> >
> > http://www.microcosmotalk.com/tech/scripts/
> >
> > Select a coutry out of the country combobox there. It's
> > not VB but the same thing can be done in VB.
> >
> > Let us know if that helps.
> >
> > --
> > Jim Carlock
> > http://www.microcosmotalk.com/
> > Post replies to the newsgroup.
> >
> >
> > "Troy Scott" wrote:
> > How is this going to work for 14 rows?
> >
> > Troy
> >
> > "Randy Birch" <rgb_removethis@mvps.org> wrote:
> > > mintIncomeSurvey(1, 1)  = Val(Text1.Text)
> > > mintIncomeSurvey(1, 2)  = Val(Text2.Text)
> > > mintIncomeSurvey(1, 3)  = Val(Text3.Text)
> > >
> > > ... or ...
> > >
> > > If Combo1.ListIndex > -1 then
> > >    mintIncomeSurvey(1, 1)  = Val(Combo1.List(Combo1.ListIndex))
> > > end if
> > >
> > > ... or, if you're storing the numeric value in the combo item's
ItemData
> > > property ...
> > >
> > > If Combo1.ListIndex > -1 then
> > >    mintIncomeSurvey(1, 1)  = Combo1.ItemData(Combo1.ListIndex)
> > > end if
> > >
> > > ... then ...
> > >
> > > debug.print mintIncomeSurvey(1, 1)
> > > debug.print mintIncomeSurvey(1, 2)
> > > debug.print mintIncomeSurvey(1, 3)
> > >
> > > to see the values.
> > > --
> > >
> > > Randy Birch
> > > MVP Visual Basic
> > > http://vbnet.mvps.org/
> > > Please respond only to the newsgroups so all can benefit.
> > >
> > >
> > > "Troy Scott mindspring.com>" <tigerweld<removebrakets> wrote in
message
> > > news:eTW5gJeHEHA.3584@TK2MSFTNGP09.phx.gbl...
> > > : Hello, I'm a VB newbie and I am stumped!  I'm trying to use two text
> > boxes
> > > : and a drop down list to enter data into an array and I don't know
how
> to
> > > : begin.
> > > :
> > > :  This is all I have so far.
> > > :
> > > : Dim mintIncomeSurvey(1 To 14, 1 To 3) As Integer
> > > : Dim mintRowIndex As Integer
> > > : Dim mintColIndex As Integer
> > > :
> > > :
> > > : HELP!
> > > :
> > > : Troy
> > > :
> > > :
> > >
> >
> >
> >
>
>
>