Is there any way of checking if a field exists in a recordset before trying to retrieve the value of the field.
For Example:
If Exists(mytable!myfield) Then
myvalue = mytable!myfield
Else
myvalue = ""
End If
Printable View
Is there any way of checking if a field exists in a recordset before trying to retrieve the value of the field.
For Example:
If Exists(mytable!myfield) Then
myvalue = mytable!myfield
Else
myvalue = ""
End If
A function like the following works
Public Function FieldExists(rs As ADODB.Recordset, ByVal strField As String) As Integer
Dim f As Integer
f = -1 ' Not found
If Not (rs Is Nothing Or strField = "") Then
For f = rs.Fields.Count - 1 To 0 Step -1
If UCase(rs.Fields(f).Name) = UCase(strField) Then
Exit For
End If
Next
End If
FieldExists = f
End Function
hmmm...i suppose you could do:
VB Code:
On Error Resume Next myvalue = "" myvalue = mytable!myfield '* In either way your value will be set to the field value or else remains blank.
but I guess its not very elegant....
If you're using SQL server:
IF EXISTS(SELECT * FROM SysColumns WHERE ID = (SELECT ID FROM SysObjects WHERE NAME = '[Table Name]') AND NAME = '[Column Name]')
PRINT 'Found
ELSE
PRINT 'Not found'