?
Solved

"Grouping" recordset

Posted on 2004-09-21
4
Medium Priority
?
202 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 600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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