我用vb连接sql
现在有一个模块是查找给定的会员号是否存在
txtSQL = "select * from 工会会员基本信息表 where 会员号 = '" & Trim(Text1.Text) & "'"
    Set mrc = ExecuteSQL(txtSQL, MsgText)
    
    If mrc.Fields(0) = Text1.Text Then当不存在时,由于mrc中没有存值,所以上面的语句就会出错了????怎么办?有什么现成的函数实现此功能吗?谢谢

解决方案 »

  1.   

        Dim CN   As New ADODB.Connection                '定义数据库的连接
        Dim mrc As New ADODB.Recordset
        CN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\NWIND.MDB;Persist Security Info=False"
        CN.Open 
    txtSQL = "select * from 工会会员基本信息表 where 会员号 = '" & Trim(Text1.Text) & "'"   mrc .CursorLocation = adUseClient   mrc.Open txtSQL, CN, adOpenDynamic, adLockBatchOptimistic
        if mrc.RecordCount>0 then    '这样判断一下           ................
              If mrc.Fields(0) = Text1.Text Then
               ...    end if
      

  2.   

    if mrc.Eof then    
       '没有记录
       ...............
       .............     
    end if
      

  3.   

    在执行SQL语句前加上客户端游标.即mrc .CursorLocation = adUseClient
      

  4.   

    txtSQL = "select * from 工会会员基本信息表 where 会员号 = '" & Trim(Text1.Text) & "'"
        Set mrc = ExecuteSQL(txtSQL, MsgText)
        
        If mrc.Fields(0) = Text1.Text Then
    用 mrc.eof判断
        If mrc.eof then
             msgbox "无数据"
        else
             Fields(0) = Text1.Text Then
        endif
      

  5.   

    txtSQL = "select * from 工会会员基本信息表 where 会员号 = '" & Trim(Text1.Text) & "'"
        Set mrc = ExecuteSQL(txtSQL, MsgText)
            I
        If mrc.Fields(0) = Text1.Text Then
      

  6.   

    用mrc.RecordCount来判断比较好。
      

  7.   

    但是在记录非常多的情况下,RecordCount是非常消耗资源的,因为Recordset要把所有的记录都过一遍才能得到Recordcount,用EOF比较快。另外要注意,如果数据库记录中允许Null,最好先用 
     If Not IsNull(mrc.Fields(0)) Then ... 判断一下,否则会出错,除非确定mrc.Fields(0)中不会出现NULL
      

  8.   

    rs.open
    not (rs.bof and rs.eof)为真表示找到相关记录.
      

  9.   

    mrc.recordcount=0 then
      msgbox "没有记录!"
    end if
      

  10.   

    txtSQL = "select * from 工会会员基本信息表 where 会员号 = '" & Trim(Text1.Text) & "'"
        Set mrc = ExecuteSQL(txtSQL, MsgText)
        
        If mrc.eof Then
          msgbox("非法会员")
        end if
      

  11.   

    if mrc.Eof then    
       '没有记录
       ...............
       .............     
    end if
      

  12.   

    if not rs.eof or rs.bof then-----end if