Solved

"Grouping" recordset

Posted on 2004-09-21
4
200 Views
Last Modified: 2009-12-16
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
Comment
Question by:sata
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 4

Expert Comment

by:ChenChen
ID: 12110662
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
 
LVL 1

Author Comment

by:sata
ID: 12110674
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
 
LVL 5

Accepted Solution

by:
KarcOrigin earned 150 total points
ID: 12110698
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
 
LVL 18

Expert Comment

by:JR2003
ID: 12111027
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question