PDA

View Full Version : How do I add Certain items in a array



Jurid
08-18-2016, 02:58 PM
I got about 7 hours left to finish this code so any help is welcome.

I am using Microsoft Visual Basic 2015, and I need it to pull out everyone in an array with the same username, and tally their points up, than insert that information into another array with their First Name and Last Name and Gender and Agegroup. I'm pulling the first arrays info from a csv.
---------------------------------------------------------------------------------------------------------------------------------------------------

Module modArray
Public Structure Athletes
Public UserName As String
Public LastName As String
Public FirstName As String
Public Gender As String
Public Points As String
Public AgeGroup As String
End Structure
Public TopAthletes(20) As Athletes
Public Sub ReadAthletesArray()
'Find csv file
Dim FILE_NAME As String = "C:\Users\Jurid\Desktop\TopAthletes\LoadArray.csv"
Dim RecordPosition As Integer = 0
'How to read csv
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
Call LoadWaitingList(objReader.ReadLine(), RecordPosition)
RecordPosition = RecordPosition + 1
Loop
objReader.Close()
ReDim Preserve TopAthletes(RecordPosition)
Else
MsgBox("File Does Not Exist")
End If
End Sub

Public AthleteArray() As String
Public Sub LoadWaitingList(ByVal Newrecord As String, ByVal IndexPosition As Integer)
AthleteArray = Split(Newrecord, ",", 6)
With TopAthletes(IndexPosition)
.UserName = AthleteArray(0)
.Points = AthleteArray(1)
.LastName = AthleteArray(2)
.FirstName = AthleteArray(3)
.Gender = AthleteArray(4)
.AgeGroup = AthleteArray(5)
End With
End Sub

Public Function StringValue(ByVal IndexValue As Integer) As String
With TopAthletes(IndexValue)
StringValue = .UserName & "," & .Points & "," & .LastName & "," & .FirstName & "," & .Gender & "," & .AgeGroup
End With
End Function

Public Sub AthletePoints()
Dim UserName As String
Dim UserPoints() As Integer
For i = 0 To TopAthletes.GetUpperBound(0) - 1
UserName = TopAthletes(i).UserName
For o = 0 To TopAthletes.GetUpperBound(0) - 1
If UserName = TopAthletes(o).UserName Then
UserPoints(i) = TopAthletes(o).Points + UserPoints(i - 1)
End If
Next
Next
End Sub

End Module
----------------------------------------------------------------------------------------------------
That's the current code.
Ask if you need anymore

jonh
08-18-2016, 03:50 PM
Why are you using the VB.net library?

In Access it should be a simple job to link the CSV file as a table and write SQL to return a recordset of the info you need.

Jurid
08-18-2016, 06:00 PM
Why are you using the VB.net library?

In Access it should be a simple job to link the CSV file as a table and write SQL to return a recordset of the info you need.

Hmmm how does one do that? How do I do the whole, add certain values together?

jonh
08-18-2016, 11:32 PM
To sum rows you use grouping and sum().
To sum columns you just add them together.


SELECT ID, Sum(Value1), Sum(Value2), Sum([Value1]+[Value2]) AS Total
FROM Table1
GROUP BY ID