Khrys
Khrys

Reputation: 2774

RecordSet, Checking the Last Record

I am using the google chart framework based on a data in database...

I need to generate an output like:

([
  ['Year',  'ON',   'OFF',  'X1',   'X2'],
  ['Jan/12',    2,  5,      10,         9],
  ['Fev/12',    5,  10,     54,         10],
  ['Mar/12',    10, 36,     15,         10]
]);

Note that the last line do not have a comma. My code looks like:

([
  ['Year',  'ON',   'OF',   'X1',   'X1'],
    <%
        WHILE NOT DB.EOF
        ON= DB("ON")
        OFF= DB("OFF")
        X1= DB("X1")
        X2= DB("X2")
    %>
        ['<% Response.Write Month %>/<% Response.Write Year %>',    <% Response.Write ON %>, <% Response.Write OFF %>, <% Response.Write X1 %>, <% Response.Write X2 %>,
    <%
        DB.MOVENEXT
        WEND
    %>
]);

My question: How can I check when the loop come to the last line, instead of put the comma, put just nothing?

Thanks!

Upvotes: 2

Views: 4380

Answers (2)

vegetable
vegetable

Reputation: 333

AnthonyWJones's answer seems ok for me, but you could also ask inside loop if after move next you're not at EOF then you write ","

([ 
    ['Year',  'ON',   'OFF',   'X1',   'X2']
    <% Do While Not DB.EOF %>
        [
         '<%= Month %>/<%= Year %>', 
         <%= DB("ON") %>, 
         <%= DB("OFF") %>, 
         <%= DB("X1") %>, 
         <%= DB("X2") %>
        ]
        <%
        DB.MoveNext
        %>
        <% If Not DB.EOF Then %>
            ,
        <% End If %>
    <% Loop %>
]); 

;-)

Upvotes: 3

AnthonyWJones
AnthonyWJones

Reputation: 189457

You get RecordCount = -1 because a forward only recordset does not know how many records are yet to come.

The approach to take here is to insert some content at the top of the loop.

([ 
  ['Year',  'ON',   'OFF',   'X1',   'X2']
    <% 
        Do While Not DB.EOF
            Response.Write ", " & vbCrLf

            Response.Write "  ['" & Month & "/" & Year & "', " & DB("ON") & ", " & DB("OFF") & ", " & DB("X1") & ", " & DB("X2") & "]"  

            DB.MoveNext 
        Loop 
    %> 
]); 

Upvotes: 1

Related Questions