Software development EricSavage - 04 Sep 2009 12:40 pm
Checking Field Exists in VB6 RecordSet
If you try to access a record in an ADODB record set where the record does not exist, you’re likely to get an exception. The first question to ask of course is why you’re writing code for a field that might not exist, but certainly in my example, I was developing a class that was extensible and ready to be scaled depending on the requirements. To be more specific, it was populating the properties of a class once a record set had been filled by a fetch from the database. I had no guarantee that the SQL statement would meet specs, so I needed some error catching, just populating those properties that had corresponding record set fields.
Here then is the code written to check if the field existed. It is written in VB6:
Private Function RecordSetHasField(strField As String, rs As ADODB.Recordset) As Boolean
Dim objField As Field
For Each objField In rs.Fields
If UCase(objField.Name) = UCase(strField) Then
RecordSetHasField = True
Exit Function
End If
Next
RecordSetHasField = False
End Function
A typical use could be something like the following:
If RecordSetHasField("OrderedGauge", rs) Then
If Not IsNull(rs!OrderedGauge) Then
MetalGaugeOrig = rs!OrderedGauge
End If
End If

on 02 Mar 2010 at 12:42 pm 1.michi said …
accessing objfield.name leads to a:
“methode or datamember not found”