请看下面这段程序,为啥不能删除记录啊。
Set ds = OpenDatabase("abc.mdb")
Set rs = ds.OpenRecordset("a")
rs.Index = "PrimaryKey"
rs.Seek "=", Text1.Text
If rs.NoMatch = False Then
rd.Delete
End If
因为在表a中,主键是自动编号,不是Text1中所显示内容的字段,是不是和这个有关?
再有,我试过findfirst方法,也是不能删除,是不是非用SQL不可啊?

解决方案 »

  1.   

    find会移动记录指针的,如果找不到,会到eof,这时根本没有记录,当然不能删除了。
    所以,应该在查找之前记录当前位置,找不到时再回到这个位置删除。
      

  2.   


    Do Until rs.NoMatch=true
       rs.Delete
       rs.Seek "=", Text1.Text
    loop
      

  3.   

    to 中海,
    find会记录指针没错,可是我是在判断nomatch=false的情况下才删除的,不是随便就删除啊!
      

  4.   

    Set ds = OpenDatabase("abc.mdb")
    Set rs = ds.OpenRecordset("a")
    rs.Index = "PrimaryKey"
    rs.Seek "=", Text1.Text
    If rs.NoMatch = False Then
    rd.Delete
    End If
    ---------------------
    试在rd.Delete后加:rd.movefirst
    或refrash.
    还有给你提两点意见:
    1.不要用If rs.NoMatch = False ,要规范,用 If not rs.NoMatch  then...
    2.Text1.Text最好写成 trim(Text1.Text)
    3.也是最重要的一点:尽早放弃DAO,用ADO做跳板,最终学习ADO.NET,这才是你应该走的路!
    ---------------------
    结帖的时候别忘给我加点分啊,呵呵!!
      

  5.   

    to 水止如心,
    谢谢您的意见,我一定规范自己的语法,也会尽快学习ado。
    但是经过您的点拨,问题还是没有解决。
    首先,Text1.Text写成 trim(Text1.Text)后,数据类型转换错误。
    其次,对您前面的程序调试后,在rs.Seek "=", Text1.Text位置,也出现了数据类型转换错误。
    希望您能再帮助我修改一下!
      

  6.   

    你的PrimaryKey字段应该不是字符型吧,你可以把text1.text转成长整型或整型。
      

  7.   

    to 大石头,
    我的text1.text是文本,PrimaryKey是自动编号,之间不能转换啊!
      

  8.   

    Set ds = OpenDatabase("abc.mdb")
    Set rs = ds.OpenRecordset("a")
    rs.Index = "PrimaryKey"
    rs.Seek "=", val(trim(Text1.Text))
    If rs.bof and rs.eof Then
       msgbox "没有找到!!!"
       exit sub
    else
       rs.Delete
    End If
      

  9.   

    Dim Rs As New ADODB.Recordset
    Dim StrSql As StringStrSql="select * from 表名 where 条件"
    If Rs.state = adStateOpen Then Rs.Close
    Rs.CursorLocation = adUseClient
    Rs.Open StrSql, Conn, adOpenKeyset, adLockPessimistic
    If Not (Rs.BOF And Rs.EOF) Then
    End If
      

  10.   

    看一下你的記錄集打開方式!!
    Recordset.CursorLocation = adUseClient
    Recordset.CursorType = adOpenKeyset
    Recordset.LockType = adLockOptimistic
      

  11.   

    你会不会是笔误呀!那该是rs.delete