Public Sub RecordSetExample() ' Recordset properties and methods '******************************************************** '*** Use of the ICommand interface is not safe. *** '*** Use of SQL to change data bypasses all *** '*** logic and error checking. Use of Select *** '*** queries can impact performance. *** '*** Use ICommand only if there is no good *** '*** alternative. *** '******************************************************** Dim com As Command Dim RecSet As Recordset, CloneSet As Recordset Dim i As Integer, RecCnt%, ColCnt% Dim OriginalSummary As String, CloneSummary$ Dim TypeCode As Integer, TypeString As String On Error GoTo RecordSetExampleErr ' Set and execute the SQL statement to select defects ' that have tests associated with them and sort them ' by the defects' summaries. 'tdc is the global TDConnection object. Set com = tdc.Command com.CommandText = _ "select bg_summary, ts_name, ts_exec_status, bg_responsible " _ + "from bug, test where bg_test_reference = ts_test_id and " _ + "bg_responsible IS NOT Null" _ + " order by bg_summary" 'Execute the query and get the recordset. Set RecSet = com.Execute ColCnt = RecSet.ColCount RecCnt = RecSet.RecordCount Debug.Print "Record Count = " & RecCnt _ & ", Column Count = " _ & ColCnt ' Got to the first record. RecSet.First Debug.Print "Position is " & RecSet.Position ' Reports 0 ' Save the summary field of the first record. OriginalSummary = RecSet.FieldValue("BG_SUMMARY") ' Make a copy of the recordset. Set CloneSet = RecSet.Clone CloneSet.First CloneSummary = CloneSet.FieldValue("BG_SUMMARY") 'Are they the same? Debug.Print "Are they the same ? " _ & CStr(CloneSummary = OriginalSummary) Debug.Print CloneSummary ' Destroy the clone. Set CloneSet = Nothing ' Go to the next in the original. RecSet.Next Debug.Print "Before Recordset? " & RecSet.BOR 'Reports False Debug.Print "Record number is " & RecSet.Position ' Reports 1 ' Describe the columns. For i = 0 To ColCnt - 1 TypeCode = RecSet.ColType(i) 'For code of DataTypeString, ' see example 'Convert data types to string' TypeString = DataTypeString(TypeCode) Debug.Print "Column name " & RecSet.ColName(i) _ & ", Type: " & TypeString & ", Size: " _ & RecSet.ColSize(i) Next i ' Output the column names and values. For i = 0 To ColCnt - 1 Debug.Print "Column name and value: " _ & RecSet.ColName(i) & ", " & RecSet.FieldValue(i) Next i ' Output the column index by name. Debug.Print "The index of the summary column is " _ & RecSet.ColIndex("BG_SUMMARY") Debug.Print "The index of the responsible column is " _ & RecSet.ColIndex("BG_RESPONSIBLE") ' Go to the last record and output the column names and values. RecSet.last Debug.Print "Are we past the record set? " _ & RecSet.EOR 'Reports False Debug.Print "Record number is " _ & RecSet.Position ' Reports RecordCount - 1 For i = 0 To ColCnt - 1 Debug.Print "Column name and value: " _ & RecSet.ColName(i) & ", " & RecSet.FieldValue(i) Next i ' Try to go past the last record. RecSet.Next Debug.Print "Are we past the record set? " _ & RecSet.EOR 'Reports True Debug.Print "Record number is " _ & RecSet.Position ' Reports RecordCount exactly Debug.Print "The number of records is " & RecCnt On Error GoTo BadLoop ' This loop fails, as expected, since EOR is true. For i = 0 To ColCnt - 1 Debug.Print RecSet.ColName(i) & ", " & RecSet.FieldValue(i) Next i BadLoop: On Error GoTo RecordSetExampleErr Debug.Print err.Description ' Now step back to the last record. RecSet.Prev Debug.Print "Are we past the record set? " _ & RecSet.EOR 'Reports True Debug.Print "Record number is " _ & RecSet.Position ' Reports RecordCount - 1 ' Go to the last record directly. RecSet.last Debug.Print "Are we past the record set? " _ & RecSet.EOR 'Reports True Debug.Print "Record number is " _ & RecSet.Position ' Reports RecordCount - 1 Exit Sub RecordSetExampleErr: ErrorHandler err End Sub