if Rs.Fields("字段") then
   .....
end if
我这个字段有时候有,有时候没有的,现在就是判断它有没有,然后处理,但是怎么判断呢?

解决方案 »

  1.   

    haveField=false
    for each i in rs.fields
        if i.name="字段" then
            haveField=true
            exit for
        end if
    nextif haveField then msgbox "字段存在!"
      

  2.   

    循环一下Fields判断Rs.Fields.Item(index).Name
      

  3.   

    顶楼上的.
    另外
    如果sql数据库可以查数据库表
    select * from syscolumns a,sysobjects b where a.id=b.id and a.name='字段' and b.name='表'
      

  4.   

    dim fld as Field
    for each fld in rs.fields
        if fld.name="字段" then
            msgbox "字段存在!"
            exit for
        end if
    next
      

  5.   

    Dim cn As ADODB.Connection, rs As ADODB.Recordset
    Dim i As Integer
    Dim TheFieldExisting As Boolean
    Dim strField As StringSet cn = New ADODB.Connection
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB;Persist Security Info=False"
    cn.OpenSet rs = New ADODB.Recordset
    rs.Open ("SELECT * FROM titles"), cn'方法一
    For i = 0 To rs.Fields.Count - 1
        If UCase(rs.Fields(i).Name) = "TITLE" Then
            TheFieldExisting = True
            Exit For
        End If
    Next i
    MsgBox "Field [Title] " & IIf(TheFieldExisting, "", "Not ") & "in Place"'方法二
    On Error Resume Next
    strField = rs!UserName
    If Err.Number = 3265 Then TheFieldExisting = False
    MsgBox "Field [UserName] " & IIf(TheFieldExisting, "", "Not ") & "in Place"
      

  6.   

    一个表最多255个字段,你认为一个255次的循环对电脑来说会是一个负担么?只需要0.000000001ms而已!:-P
      

  7.   

    呵呵,我现在有7个字段,怎么样在产生了3265之后还原err.number啊,现在好象一个出错后面的err.number就不变了