0
votes

I have a recordset like this:

row1 = Type1, partid, partname

row2 = Type2, partid, partname

row3 = Type2, partid, partname

I want my output to be like this:

Type1: partid partname

Type2: partid partname, partid, partname

or

Type2: partid partname, partid, partname

Type1: partid partname

(depending on how the sort works out)

I have my results sorted by the Type value. I may or may not get same Types in a result, but if so the result is required to be printed as above.

I'm using .getRows() to get the recordset into an array.

I can't for the life of me figure out how to decide in the loop whether to print a break tag or a comma.

1
If you print the comma after the last item, you cannot know. One way out is to print the first item and iterate to print both commas and items at once, ex: "item1" --> ", item2" --> ", item3" --> ... - Frederik.L

1 Answers

0
votes

You need to check for a change in the type column. In that case, print <br /> and the type. In code:

Set r = CreateObject("ADODB.Recordset")
r.Fields.Append "t", 3
r.Fields.Append "i", 3
r.Fields.Append "n", 3
r.Open
r.AddNew : r.Fields("t") = 1 : r.Fields("i") = 10 : r.Fields("n") = 10
r.AddNew : r.Fields("t") = 2 : r.Fields("i") = 20 : r.Fields("n") = 21
r.AddNew : r.Fields("t") = 2 : r.Fields("i") = 22 : r.Fields("n") = 22
r.AddNew : r.Fields("t") = 3 : r.Fields("i") = 30 : r.Fields("n") = 31
r.MoveFirst
a = r.GetRows()
t = a(0, 0)
WScript.StdOut.Write a(0, 0)
For r = 0 To UBound(a, 2)
    If t <> a(0, r) Then
       WScript.StdOut.Write "<br />" & a(0, r)
       t = a(0, r)
    End If
    WScript.StdOut.Write Join(Array("", a(1, r), a(2,r)), ",")
Next

output:

cscript 24441584.vbs
1,10,10<br />2,20,21,22,22<br />3,30,31