Solved

"Grouping" recordset

Posted on 2004-09-21
4
196 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now