每次运行就弹出"对象打开时,不允许操作"的提示!
添加数据的代码如下:
Private Sub Command3_Click()
Dim txtsql As String
Dim msgtxt As String
Dim conn As ADODB.Connection
Dim mrc As ADODB.Recordset
Dim sql As String
Dim i As Integer
i = 0
While Not List1.Selected(i)
i = i + 1
Wend
txtsql = "select * from PATIENT where Pno='" + Text2.Text + "'"
Set mrc = ExecuteSQL(txtsql, msgtxt)
If mrc.EOF Then
    Set conn = New ADODB.Connection
    Cs = "FileDSN=his.dsn;UID=sa;PWD=011248;DATABASE=master"
    conn.Open Cs
    sql = "select * from PATIENT"
    mrc.Open "sql", conn, 1, 3
    mrc.AddNew
    mrc("Pname") = Text1.Text
    mrc("Pno") = Text2.Text
    mrc("Psex") = List1.List(i)
    mrc("Page") = CInt(Text3.Text)
    mrc("Phistory1") = Text4.Text
    mrc("Phistory2") = Text5.Text
    mrc("Phistory3") = Text6.Text
    mrc("Phistory4") = Text7.Text
    mrc("Phistory5") = Text8.Text
    mrc("Pitem1") = Text9.Text
    mrc("Pitem2") = Text10.Text
    mrc("Pitem3") = Text11.Text
    mrc("Pitem4") = Text12.Text
    mrc("Pitem5") = Text13.Text
    mrc.Update
    MsgBox "添加档案记录成功", , "提示"
    Pno = Text2.Text
    frmchoseddep.Show
Else
    If MsgBox("您输入的病人编号已经存在,请去选择档案窗体", vbYesNo, "提示") = vbYes Then
    Me.Hide
    End If
End If
End Sub

解决方案 »

  1.   

    mrc.Open "sql", conn, 1, 3 
    这个应该是mrc.Open sql, conn, 1, 3 才对吧
      

  2.   

    mrc再打开之前先
    set mrc=nothing
    set mrc=new ADODB.Recordset 
    然后再mrc.open 
                
      

  3.   

    Private Sub Command3_Click() 
    Dim txtsql As String 
    Dim msgtxt As String 
    Dim conn As ADODB.Connection 
    Dim mrc As ADODB.Recordset 
    Dim sql As String 
    Dim i As Integer 
    i = 0 
    While Not List1.Selected(i) 
    i = i + 1 
    Wend 
    txtsql = "select * from PATIENT where Pno='" + Text2.Text + "'" 
    Set mrc = ExecuteSQL(txtsql, msgtxt)  '干什么用?
    If mrc.EOF Then '1
        Set conn = New ADODB.Connection 
        Cs = "FileDSN=his.dsn;UID=sa;PWD=011248;DATABASE=master" 
        conn.Open Cs 
        sql = "select * from PATIENT" 
        mrc.Open "sql", conn, 1, 3 '已经使用又打开,当然不可以
        mrc.AddNew 
        mrc("Pname") = Text1.Text 
        mrc("Pno") = Text2.Text 
        mrc("Psex") = List1.List(i) 
        mrc("Page") = CInt(Text3.Text) 
        mrc("Phistory1") = Text4.Text 
        mrc("Phistory2") = Text5.Text 
        mrc("Phistory3") = Text6.Text 
        mrc("Phistory4") = Text7.Text 
        mrc("Phistory5") = Text8.Text 
        mrc("Pitem1") = Text9.Text 
        mrc("Pitem2") = Text10.Text 
        mrc("Pitem3") = Text11.Text 
        mrc("Pitem4") = Text12.Text 
        mrc("Pitem5") = Text13.Text 
        mrc.Update 
        MsgBox "添加档案记录成功", , "提示" 
        Pno = Text2.Text 
        frmchoseddep.Show 
    Else 
        If MsgBox("您输入的病人编号已经存在,请去选择档案窗体", vbYesNo, "提示") = vbYes Then 
        Me.Hide 
        End If 
    End If 
    End Sub 
     
     
      

  4.   

    试一试这个:
    http://download.csdn.net/source/1498324
      

  5.   

    txtsql = "select * from PATIENT where Pno='" + Text2.Text + "'" 
    Set mrc = ExecuteSQL(txtsql, msgtxt) 
    If mrc.EOF Then 
        Set conn = New ADODB.Connection 
        Cs = "FileDSN=his.dsn;UID=sa;PWD=011248;DATABASE=master" 
        if conn.state=1 then conn.close'加上这一句
        conn.Open Cs 
        sql = "select * from PATIENT" 
        If mrc.State = 1 Then mrc.Close'加上这一句
        mrc.Open "sql", conn, 1, 3 
        mrc.AddNew 
        mrc("Pname") = Text1.Text 
        mrc("Pno") = Text2.Text 
        mrc("Psex") = List1.List(i) 
        mrc("Page") = CInt(Text3.Text) 
        mrc("Phistory1") = Text4.Text 
        mrc("Phistory2") = Text5.Text 
        mrc("Phistory3") = Text6.Text 
        mrc("Phistory4") = Text7.Text 
        mrc("Phistory5") = Text8.Text 
        mrc("Pitem1") = Text9.Text 
        mrc("Pitem2") = Text10.Text 
        mrc("Pitem3") = Text11.Text 
        mrc("Pitem4") = Text12.Text 
        mrc("Pitem5") = Text13.Text 
        mrc.Update 
        MsgBox "添加档案记录成功", , "提示" 
        Pno = Text2.Text 
        frmchoseddep.Show 
    Else 
        If MsgBox("您输入的病人编号已经存在,请去选择档案窗体", vbYesNo, "提示") = vbYes Then 
        Me.Hide 
        End If 
    End If 
      

  6.   

    记录集打开的时候要确认是否之前已被打开.如果之前打开了,要关闭;
    还有种方法,就是你再定义一个recordset,各是各的,这样就不会发生这样的问题了
      

  7.   

    1. LZ的程序第一次查询的数据库链接在哪里?很怀疑能否查到数据。
    2. else中应该有mrc的关闭操作。
    3. LZ只不过是要插入一条记录,需要做个动态记录集再更新么?直接一句update就可以了。
      

  8.   

    更正第二点,应该在if判断完成后进行mrc关闭操作。