Solved

"Grouping" recordset

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

920 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

17 Experts available now in Live!

Get 1:1 Help Now