Results 1 to 7 of 7

Thread: array question

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4

    array question

    Does anyone have an example on adding a column in an array with say, 3 columns. My array is 14 rows with 3 columns and I cannot get it to work. Here is my code. The column I want to add is the 2nd column, or #1 since I'm using 0 to 2.
    thanks!


    Private Sub mnuReportsHighIncomeReport_Click()

    Dim curRowTotal As Currency
    Dim intRowIndex As Integer
    Dim curIncomeTotal As Currency

    For intRowIndex = 0 To 13

    curIncomeTotal = curIncomeTotal + mintDataArray(intRowIndex, 1)

    Next intRowIndex
    Print mintDataArray(mintCurrentRow, 1)

    End Sub

  2. #2
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Private Sub cmdTotalIncome_Click()

    Dim curRowTotal As Currency
    Dim intRowIndex As Integer
    Dim curIncomeTotal As Currency

    For intRowIndex = 0 To 13
    curIncomeTotal = curIncomeTotal + mintDataArray(intRowIndex, 1)
    Next

    Print curIncomeTotal

    End Sub

    Ring a bell? I grabbed this from another forum (that shall remain nameless )
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4
    UDAMAN! thanks a million!

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4
    ah chit. I forgot one thing. Once I find the average I need to display those listings that exceed the average. I need to display all three columns when doing this. I need another loop don't I? I have this but for some reason its not working. lol

    For intRowIndex = 0 To 13

    If mintDataArray(intRowIndex, 1) > mcurIncomeTotal Then
    Print mintDataArray(intRowIndex, 1)

    End If
    Next

  5. #5
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    since this is a school assignement, you would have to provide more precise details regarding the requirements - I find your reply unclear. But I should warn you, I will not provide code next time - but rather food for thought (so you'd have to engage your brainpower )....as a former student I know where you're at and I do commend your interest in learning by posting here. So in the spirit of learning, feel free to reply. regards

    DA
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    4

    Talking

    you gotta admit, I was aweful close on that one! Brainpower? pfft! after 12 hours on this program I have no brain left! lol

    1st column is Id number, 2nd is annual income, 3rd is number of persons in household. I need to display (or print) the id number and income for each household that exceeds the average income. I know we first need to figure the average, then loop back thru a second time (with an IF statement?) to test each row to see it the income exceeds the average. I hope I'm being clear enough. There really isn't more to it other than that.

    Thanks,

    Troy

    btw, I don't expect you to hand feed me, rather lead me to the water.

  7. #7
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    well alright then...to the water we go..

    Indeed you are correct that the first step in all of this mess which has cost already atleast 12 hours of intense and painful barinpower (been there - in fact come to think of it - I despised arrays initially) is to calculate the average. And a loop through the array shall accomplish that by rolling up the total each time we iterate and retrieve a value from the array. Average is then of course the total divided by number of pieces of data....and you're probably gonna want to store this value in a variable. Personally, that would be my first step - I would declare such a variable. Then, as you said, iterate through the array, roll up the total, and after the loop, perform the calculation - assign it to the variable. And yes, I agree (now that I understand the problem better) that a second iteration through the array will be required. You already wrote the piece of code to check :

    Code:
    If mintDataArray(intRowIndex, 1) > mcurIncomeTotal Then
    Its what to do after the "Then" part is the question - what do we know ? we know that we need to display three columns:

    the values are obviously coming from the array and you have elements 0, 1, and 2 and I know you know how to reference them and pull them out. To display all on one line, the most common way is through concatenation. VB concatenates with the ampersand character so why not start by thinking about how to create a line....then its just a question of where and how to display it:

    OPTIONS:

    you already know the Print command - it prints to the form and about as exciting as "Hello World" (no offense) - you could do this and issue a command each time you run across a value in the array that meets the condition

    you could use some sort of container:

    - listbox
    - combobox
    - textbox

    OR if you really wanna impress the pants off your teacher,
    - Listview

    I will start you off with the combobox and listbox - they both use a method call .AddItem to add strings to their lists. So checkout either on the web, or hopefully you have access to a version of MSDN (I have the full version on my machine - if I highlight the word "ComboBox" and press F1, MSDN opens right to that topic with all the methods, properties, and there is sample code you can paste right into your project)....

    other hints:

    what are ways to manipulate strings ? how can we space things so that they line up to a specific margin, or column? checkout

    Format function
    Spc function
    Chr function

    constants:

    vbCrlf
    vbTab
    ...others

    In summary, two loops, first one gets average, second one checks against calculated average and displays each field from the array in some sort of container - and that's it - its off to get an "A"!!

    I OFFER THE FOLLOWING DEAL:

    If you can figure out how to use a Listview with code that populates it, I will provide some code that allows you to sort the columns at the click of a mouse.

    Bonne Chance mon ami
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

Posting Permissions

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



Click Here to Expand Forum to Full Width