Results 1 to 8 of 8

Thread: How To Test the field Whether Is Exist Or not

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    Malaysia
    Posts
    108

    Cool

    Hello...

    I want to know how to test the field whether is
    exist or not and if the field is not exist, i want
    to update my table with the code.

    Hope somebody show me how...

  2. #2
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    do you mean you want to see if a field exists, or a value for a certain field?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    Malaysia
    Posts
    108
    Hi Tom.

    yes I want to see whether a fields is exist or not.

  4. #4
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    No Problem

    are you using ADO or DAO, and which database (Access, SQL Server, etc...?)

    Tom

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    Malaysia
    Posts
    108
    Thanks Tom...

    I'm using DAO.
    But if you can give both example that's very nice to me.
    May be one day I could change to ADO.

  6. #6
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    DAO
    Code:
        Dim db As Database
        Dim rs As Recordset
        Dim fld As Field
        Dim blnFound As Boolean
        
        blnFound = False
        
        'open db/rs
        Set db = DBEngine.OpenDatabase("Nwind.mdb")
        Set rs = db.OpenRecordset("Customers")
        
        'search thru fields
        For Each fld In rs.Fields
            If ucase(trim(fld.Name)) = "ADDRESS" Then
                blnFound = True
                Exit For
            End If
        Next fld
        
        'do something if we found the field
        If blnFound = True Then
            MsgBox "Found It!"
        End If
        
        'cleanup
        rs.Close
        db.Close
        
        Set fld = Nothing
        Set rs = Nothing
        Set db = Nothing
    and now for ADO:
    Code:
        Dim cn As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim blnFound As Boolean
        Dim lngCounter As Long
           
        blnFound = False
        
        'open db connection
        Set cn = New Connection
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Nwind.mdb"
        
        'get rs
        Set rs = cn.Execute("Select * from Customers", , adCmdText)
        
        'loop thru fields
        For lngCounter = 0 To rs.Fields.Count - 1
            If UCase(Trim(rs.Fields(lngCounter).Name)) = "ADDRESS" Then
                blnFound = True
                Exit For
            End If
        Next lngCounter
        
        'did we find it?
        If blnFound = True Then
            MsgBox "Found It"
        End If
        
        'cleanup
        rs.Close
        cn.Close
        Set rs = Nothing
        Set cn = Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    Malaysia
    Posts
    108
    Wow...

    Thanks a lot Tom...


  8. #8
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844

    Thumbs up


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