Private Sub Command1_Click()
Dim objcon   As New ADODB.Connection
Dim objrs   As New ADODB.Recordset
Dim strConn As String
Dim str1 As String
strConn = "Provider=SQLOLEDB.1;User ID=sa;password=19890425;Initial Catalog=gui;Data Source=jujumao"
objcon.Open strConn
str1 = "INSERT INTO student(sno,sname,ssex,sage,sdept) VALUES( ' " & Text1 & " ', ' " & Text2 & " ',' " & Text3 & " ', ' " & Text4 & " ' , ' " & Text5 & " ' ) "
objcon.Execute str1
objrs.Open "select * from student", objcon, 3, 1
'在部件中添加DataGrid控件
Set DataGrid1.DataSource = objrs
End Sub
Private Sub Command2_Click()
Dim objcon   As New ADODB.Connection
Dim objrs   As New ADODB.Recordset
Dim strConn As String
Dim str1 As String
strConn = "Provider=SQLOLEDB.1;User ID=sa;password=19890425;Initial Catalog=gui;Data Source=jujumao"
objcon.Open strConn
If Not (objrs.EOF) Then
Dim i As Integer
i = MsgBox("真的要删除 : " & obj.Fields(0).Value & " 的记录? ", vbYesNo, "删除记录 ")
If i = vbYes Then
objrs.Delete adAffectCurrent '关键是这句,删除当前记录
End If
End If插入的时候一点事情都没有,删除的时候就出错了。提示对象关闭时不允许操作  ,顺便问句,adaffectcurrent是不是删除鼠标指向行的记录啊????'在部件中添加DataGrid控件
Set DataGrid1.DataSource = objrs
End Sub

解决方案 »

  1.   

    什么是当前纪录
    objcon.Open strConn 
    objcon.excute"delete from student where sno=123" 你objrs在Private Sub Command2_Click() 声明了但是还没有用啊如果你想把objrs声明全局的话,就要在form里面声明而不是每个sub下面
      

  2.   

    Private Sub Command2_Click() 
    Dim objcon  As New ADODB.Connection 
    Dim objrs  As New ADODB.Recordset 
    Dim strConn As String 
    Dim str1 As String 
    strConn = "Provider=SQLOLEDB.1;User ID=sa;password=19890425;Initial Catalog=gui;Data Source=jujumao" 
    objcon.Open strConn 
    If Not (objrs.EOF) Then 
    Dim i As Integer 
    i = MsgBox("真的要删除 : " & obj.Fields(0).Value & " 的记录? ", vbYesNo, "删除记录 ") 
    If i = vbYes Then 
    objrs.Delete adAffectCurrent '关键是这句,删除当前记录 
    End If 
    End If 插入的时候一点事情都没有,删除的时候就出错了。提示对象关闭时不允许操作  ,顺便问句,adaffectcurrent是不是删除鼠标指向行的记录啊???? 
    '在部件中添加DataGrid控件 
    Set DataGrid1.DataSource = objrs 
    End Sub 
    ------------------你根本就没有打开过objrs,当然会有这个提示咯
    你的变量是在按钮点击过程中定义的,这个过程结束,变量也就销毁了.
    所以按钮2中发生的事和按钮1中发生的事根本没有关系,除非你把数据库对象都定义到窗体变量那里去,才可以在窗口打开的期间一直保存
      

  3.   

    怎么没有用,你再看看objrs.Delete adAffectCurrent 不是已经使用了objrs了嘛?
      

  4.   

    objrs.Open "select * from student", objcon, 3, 1
    加在if语句前
    If Not (objrs.EOF) Then 
    Dim i As Integer 
    i = MsgBox("真的要删除 : " & obj.Fields(0).Value & " 的记录? ", vbYesNo, "删除记录 ")