• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

"Grouping" recordset

Hi,

I have a recordset which contains a list of data.

Below is a sample of them:
Company     Amt         item
A133           1.2          Item A
A133           13.4        Item B
B444           44.2        Item C
A133           15.3        Item D
C335           22.5        Item E
B444           44.2        Item C


I need to write them into a file in this format in a text file, which to me, looks like a "grouping".

A133
Item A   1.2
Item B   13.4
Item D   15.3

B444
Item C   44.2
Item C   44.2

C335
Item E   22.5


Can someone advise me how can I use VB to do this? Thanks!
0
sata
Asked:
sata
1 Solution
 
ChenChenCommented:
this code should do the job:

open YOUR_FILE_PATH  for output as #1

'rs would be your recordset
do while not rs.eof
print #1, rs.fields("Company")
print #1,rs.fields("item") & " " &  rs.fields("Amt")
print #1,""
rs.movenext
loop

close #1
0
 
sataAuthor Commented:
Hi,

I don't understand how can this help:

using the data that I have provided:
Company     Amt         item
A133           1.2          Item A
A133           13.4        Item B
B444           44.2        Item C
A133           15.3        Item D
C335           22.5        Item E
B444           44.2        Item C

Your code will print it into:

A133
Item A 1.2

A133
Item B 13.4

B444
Item C 44.2

:
:

The above will not be the ideal ouput that I'm looking for. Or am I missing something?
0
 
KarcOriginCommented:
Hi,
I don't know how you are writing you SQL but the best thing is to get the recordset with a group by clause. Like -
Select Company, Amt, Item From yourTableName Group By Company
After this your record will be in this format -
Company    Amt    Item
A133         1.2      Item A
A133         13.4    Item B
A133         15.3    Item D
B444          44.2   Item C
B444         44.2    Item C
C335         22.5    Item E

Now populate your recordset as -

Dim strTemp as String
Open yourFile For Output As #1

Do While Not rs.EOF()
  if (strTemp <> rst.Fields("Company")) Then
    Print #1, rs.Fields("Company")
  End If
  Print #1,rs.fields("item") & " " &  rs.Fields("Amt")
  Print #1,""
  strTemp = rs.Fields("Company")
  rs.MoveNext
Loop

Thats all.
Note: I have not tested this code but this idea should work in your case.
GoodLuCk


0
 
JR2003Commented:
Your select statement should look like this
select Company, Amt, item
from table1
order by Company, item , Amt

when you are bringing the records back you should store the previous value of Company and if it is different from the current value then output a line with the company name.

JR

e.g.
Dim sCompany as String
With rs
    While not .EOF
        if sPrevCompany <> !Company & "" Then
            sPrevCompany = !Company & ""
            WriteLine !Company
        End If
        WriteLine !item & "  " & !Amt
        .MoveNext
    Wend
    .Close
End With
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now