|
-
Nov 11th, 2002, 06:53 AM
#1
Thread Starter
Lively Member
Check if field exists
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
-
Nov 11th, 2002, 06:57 AM
#2
Hyperactive Member
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
-
Nov 11th, 2002, 06:58 AM
#3
Hyperactive Member
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....
-
Nov 11th, 2002, 08:06 AM
#4
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'
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|