Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Enterprise
Expertise: All
Jul 21, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Bind Columns to Field Objects When Looping Through Records

Here is a simple way to improve performance with respect to recordset traversal of your MDAC (Microsoft Data Access Components) application. If you want to traverse the recordset then you should obtain references to the field objects at the beginning and then simply use those references when looping through the records. If you don't take the reference and use fields then it incurs the overhead of looking up the fields in the Recordset::Fields collection for each record in the recordset. For a large recordset, this task can get quite expensive. This sample VB code shows both approaches:

Sub TraverseRecordset()

Dim objRecordset As New Recordset
Dim objField0, objField1, objField2

objRecordset.ActiveConnection = "DSN=MyDSN;User Id=sa;Password=;"
objRecordset.Source = "Select Column1, Column2, Column3 From MyTable"
' Take the references to the field objects
Set objField0 = objRecordset.Fields(0)
Set objField1 = objRecordset.Fields(1)
Set objField2 = objRecordset.Fields(2)

Do While Not objRecordset.EOF
' Good approach - Columns are bound to field objects in the following line
Debug.Print objField0.Value, objField1.Value, objField2.Value 

' Bad approach - Columns are not bound to field objects in the following line
Debug.Print objRecordset("Column1"), objRecordset("Column2"), objRecordset("Column3") 
' Move to the next record in the recordset

End Sub
Deepak Pant
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date