VB中使用ADODB对象添加数据时为什么老是弹出不允许操作的提示
每次运行就弹出"对象打开时,不允许操作"的提示!添加数据的代码如下:
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("P易做图") = 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 --------------------编程问答-------------------- mrc.Open "sql", conn, 1, 3
这个应该是mrc.Open sql, conn, 1, 3 才对吧
--------------------编程问答-------------------- mrc再打开之前先
set mrc=nothing
set mrc=new ADODB.Recordset
然后再mrc.open
--------------------编程问答-------------------- 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("P易做图") = 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
--------------------编程问答-------------------- 试一试这个:
http://download.csdn.net/source/1498324 --------------------编程问答-------------------- 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("P易做图") = 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
--------------------编程问答-------------------- 记录集打开的时候要确认是否之前已被打开.如果之前打开了,要关闭;
还有种方法,就是你再定义一个recordset,各是各的,这样就不会发生这样的问题了 --------------------编程问答-------------------- 学会使用F8! --------------------编程问答-------------------- 对象已经打开了,再次打开就重复了..... --------------------编程问答--------------------
为什么不每次更新完成数据之后就关闭呢? --------------------编程问答-------------------- 1. LZ的程序第一次查询的数据库链接在哪里?很怀疑能否查到数据。
2. else中应该有mrc的关闭操作。
3. LZ只不过是要插入一条记录,需要做个动态记录集再更新么?直接一句update就可以了。 --------------------编程问答-------------------- 更正第二点,应该在if判断完成后进行mrc关闭操作。 --------------------编程问答-------------------- 这么简单的问题,这么多回答应该解决了吧 为什么还不结贴
补充:VB , 数据库(包含打印,安装,报表)