Results 1 to 4 of 4

Thread: Check if field exists

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    86

    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

  2. #2
    Hyperactive Member
    Join Date
    Feb 2000
    Posts
    284
    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

  3. #3
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378
    hmmm...i suppose you could do:

    VB Code:
    1. On Error Resume Next
    2.   myvalue = ""
    3.   myvalue = mytable!myfield
    4.   '* In either way your value will be set to the field value or else remains blank.

    but I guess its not very elegant....
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    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
  •  



Click Here to Expand Forum to Full Width