Question

I hope I can explain this as I mean it!

I have an SQL query which has been formed as a View to make it easier to pull the data.

However, I have a week number column which gets the date and then calculates the week number. I have everything outputted into an FPDF document, but I need to split the cells after the week number changes, then display a total for that week number.

How would I go about checking when the week number column changes? I just can't think of a solution to this.

This is the code I have currently which doesn't work, there is a very high probability that this isn't right whatsoever.

if iCounter > 1 Then      
    NewSum = rs("W")     
    rs.moveprevious
    if StrComp(NewSum,rs("W")) = 1 Then
        pdf.Cell 15,5,"EOM",1,1,"C",1 
    else    
        rs.movenext     
        pdf.Cell 15,5,"",1,1,"C",1     
    End if
end if
Was it helpful?

Solution

In this scenario there are different ways to approach this, personally I've always found using Arrays a lot easier more flexible and more efficient than using the ADODB.Recordset.

Once you have a ADODB.Recordset object regardless of the approach (be it ADODB.Command, Recordset.Open() or Connection.Execute()) you can convert it to a two dimensional array using Recordset.GetRows().

Using your example I would structure your code like this;

Please take into consideration this is untested and coded from memory, I just wanted to give you the general gist of how to do this kind of computation in a Classic ASP environment.

Dim rs, data, row, rows
Dim weekno
Dim total_weekno, current_weekno

'Assuming you have instantiated your rs object
'...

'Convert to an Array variable (data)
If Not rs.EOF Then data = rs.GetRows()
'Close and release Recordset from memory
Call rs.Close()
Set rs = Nothing

If IsArray(data) Then
  rows = UBound(data, 2)

  'Iterate through the array
  For row = 0 To rows
    'Assuming weekno column is the first in your resultant columns.
    weekno = data(0, row)
    If weekno = current_weekno Then
      'Increment our total for the current weekno by 1.
      total_weekno = total_weekno + 1
    Else
      'Place logic for adding new row to PDF here.
      'Use total_weekno to display the incremented total.

      'Afterward reset total_weekno for the new current weekno.
      total_weekno = 0
      'Our weekno has changed so set current weekno.
      current_weekno = weekno
    End If
  Next
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top