Enumerating Search Results

Topic Last Modified: 2006-06-11

This example shows how to process a returned recordset, accommodating for multivalued and null properties, and how to render the results.

Example

VBScript

Example

'writes property values from a recordset (Rs)
'returned from a SQL query

'The DoResults function writes an HTML table
'for each record (item) in the recordset. In that table
'each property and value are written to a table cell,
'accommodating for null and multivalued properties.
On Error GoTo

Function DoResults(Rs)
    'If empty recordset, return error
    If Rs.EOF = True Then
     On Error Resume Next
     Err.Raise adErrNoCurrentRecord
     Response.Write "<br><FONT FACE=Arial SIZE=2>No items found, run another query.<p>"
     Else
          Rs.MoveFirst
          Do Until Rs.EOF
         'Create a HTML table for each record, make table headings stand out
         Response.Write "<FONT FACE=Arial SIZE=2><TABLE BORDER=1 cellpadding=5 cellspacing=5>"
         Response.Write "<TR><TH bgcolor=#0000ff><FONT color=#ffffff>Property</TH>"
         Response.Write "<TH bgcolor=#0000ff><FONT color=#ffffff>Value</TH></TR>"

         For Each f In Rs.Fields
            'for every field in the record
            Response.Write "<TR><TD><FONT SIZE=1>"
            Response.Write f.Name
            Response.Write "</TD><TD><FONT SIZE=1>"

            'accommodate null and multivalued properties
            If Not IsNull(f.Value) Then
                If IsArray(f.Value) Then
                   'a multivalued  property
                  Dim V
                  For Each V In f.Value
                     On Error Resume Next
                     Response.Write(V & "<br>")
               Else
                  On Error Resume Next
                  Response.Write f.Value
               End If
            Else
               'Field is null
               Response.Write "Null"
            End If
            Response.Write "</TD></TR>"
           Response.Write "</TABLE><br><hr><br>"
            Rs.MoveNext
        Loop
   End If
End Function

GoTo Ending

' Implement custom error handling here.
ErrHandler:
   MsbBox Err.Number + " " + Err.Description
   Err.Clear

Ending: