Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help with reports w/arrays

hi all , Im trying to make a report with total sales per salesperson in a given date . The problem Im haveing is writing the reports. I need to open the data file( its .txt) and look at all the records and list the salespeople and how many sales they had. eg:

Steve 4
john 2
Mike 3

Now the file can have hundreds of records in any order, here is a few records;

Mike,10:08:20 AM,10:08:49 AM ,Repeat Customer,Bedroom,Financing TD,No,,5/25/2006
Steve,10:08:58 AM,10:09:03 AM ,Customer Refferal,Upholstery,Buying House,No,,5/25/2006
Steve,10:09:13 AM,10:09:20 AM ,Sign/Drive By,Recliner,Financing TD,No,,5/25/2006
John,10:09:14 AM,10:09:53 AM ,Sign/Drive By,Bedroom,Didn't Have Style,No,,5/25/2006
Steve,10:10:06 AM,10:10:49 AM ,TV Ad,Bedroom,Didn't Have Style,No,,5/25/2006
Steve,10:10:04 AM,10:10:52 AM ,TV Ad,Bedroom,Price,No,,5/25/2006
Mike,10:08:20 AM,10:08:49 AM ,Repeat Customer,Bedroom,Financing TD,No,,5/25/2006
Steve,10:08:58 AM,10:09:03 AM ,Customer Refferal,Upholstery,Buying House,No,,5/25/2006

Here is the code that Im working with:

Private Sub NameCount(sFIle As String)
Dim tmp() As String
Dim Lines() As String
Dim tName As String
Dim Found As Boolean


Lines = Split(sFIle, vbCrLf)
For x = 0 To UBound(Lines)

If Lines(x) <> "" Then
tmp = Split(Lines(x), ",")
tName = tmp(0)
Found = False
'----------------------
' Writing to listbox This part is looking to see if name is already
' on list if so add 1 to it
'----------------------
If List1.ListCount <> 0 Then
For i = 0 To List1.ListCount - 1
tmp = Split(List1.List(i), " ")
If tmp(0) = tName Then
List1.List(i) = tName & " " & CInt(tmp(1)) + 1
Found = True
Exit For
End If
Next
End If
If Not Found Then
List1.AddItem tName & " 1"
End If

End If
Next

'----------------------
End Sub
Private Sub Form_Load()
Dim sF As String
Open "C:\Program Files\Microsoft Visual Studio\VB98\data2.txt" For Input As #1
sF = Input(LOF(1), 1)
Close #1
NameCount sF
End Sub



Right now the code works perfect but the out is a listbox, I need it to the same thing but right to a file
Any suggestions would be helpfull Thanks
[2765 byte] By [stevelach] at [2007-11-11 8:50:41]
# 1 Re: Help with reports w/arrays
Assuming from your post that you are comfortable with 'retrieving' your data from the listview, the answer sort of lies in the opposite of your 'input' :-

dim lFile as long

lFile=FreeFile (better than using a specific number)
Open "myfilename" For Output As lFile

then check out help (position cursor on 'Print', and press F1). Look at Print # statement, and the Write # statement.
gupex at 2007-11-11 17:25:02 >
# 2 Re: Help with reports w/arrays
I don't have a problem writing to a file , its the whole function thats in the lisbox routine that im having a problem with. I need it to check to see if the name is already there if so add 1 to it. :confused:
stevelach at 2007-11-11 17:25:53 >
# 3 Re: Help with reports w/arrays
2 suggestions - (1) do exactly what you are doing (counting in listbox) then cycle through listbox when finished, writing 1 line to output file for each item, or (2) set up 2 arrays instead of a listbox (1 for names, the other for counting) and either increment counting one if already in names, or redim preserve one more to each array if new name found. Then create output file when finished.
gupex at 2007-11-11 17:27:03 >
# 4 Re: Help with reports w/arrays
another solution is to open the file in Access, like a database, and read a recordset using "Select Count", specifing the first parameter (the name) The recordset will compute the total of rows with the same name. This is what a DB guy will do.

Without using a database, you can also use a Collection, using the name as a key. That is, find the element of the collection which key is equal to the current name. If the elemente exists, increment it by one. If it does not, create a new element and add it to the Collection using the name as key.
Something like

on error resume next
dim c as MyElement
set c = myCollection.item(name)
if c is nothing then
set c = new MyElement
c.Count= 1
myCollection.add a, name
else
c.Count=c.Count+1
endif

where MyElement is a simple class with a Long property Count

Marco
mstraf at 2007-11-11 17:27:56 >