在数据里记录里插进去了,不过出现提示错误,这是什么原因啊?高手进来看看!在线等

btw505 2004-11-20 08:48:52

一个按钮的代码:
Private Sub CmdOK_Click()
Dim Mycon As New ADODB.Connection
Dim Myrec As New ADODB.Recordset
Dim Mycom As New ADODB.Command

Dim strSQL As String, strConn As String


If txtName = "" Or txtID = "" Then
MsgBox "注意,班级名或所在第编号不能为空!", vbExclamation, " 提示"
txtName.SetFocus
Else
strSQL = "insert into Class values('" & txtName & "','" & txtID & "')"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Password=;Data Source="
strConn = strConn & App.Path & "\Student.mdb" & ";Persist Security Info=True"
Mycon.ConnectionString = strConn
Mycon.Open
Set Mycom.ActiveConnection = Mycon
Mycom.CommandText = strSQL
Set Myrec = Mycom.Execute
Myrec.update
Myrec.Close
Set Myrec = Nothing
Mycon.Close
Set Mycon = Nothing
End If

我的意思就是想把txtName,txtID的内容,按了此按钮之后,插入到数据库里的Class表里,不过我按了之后,会出现这样的提示:"对象关闭时,不允许操作",光标定位在Myrec.update这一行,我这个哪里有错,还有不行的话,大家写个简单的另外一种方法也可以,谢谢啊!
...全文
107 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
落伍者 2004-11-21
  • 打赏
  • 举报
回复
If txtName = "" Or txtID = "" Then
MsgBox "注意,班级名或所在第编号不能为空!", vbExclamation, " 提示"
txtName.SetFocus
Else
strSQL = "insert into Class values('" & txtName & "','" & txtID & "')"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Password=;Data Source="
strConn = strConn & App.Path & "\Student.mdb" & ";Persist Security Info=True"
Mycon.ConnectionString = strConn
Mycon.Open
Set Mycom.ActiveConnection = Mycon
Mycom.CommandText = strSQL
Set Myrec = Mycom.Execute
' Myrec.update '这句不要,你的sql语句执行时返回的记录集应该是只读的,而且如果有返回值的话也应该是影响的记录数,记得不太清楚的,查查帮助就知道了
Myrec.Close
Set Myrec = Nothing
Mycon.Close
Set Mycon = Nothing
End If

btw505 2004-11-20
  • 打赏
  • 举报
回复
上面这们老兄tztz520(午夜逛街),你的代码是对了,不过我那个为什么会有错,能说说吗?非常感谢!
tztz520 2004-11-20
  • 打赏
  • 举报
回复
'这样.

Private Sub Form_Load()
Dim Mycon As New ADODB.Connection
Dim Myrec As New ADODB.Recordset
Dim Mycom As New ADODB.Command

Dim strSQL As String, strConn As String


If txtName = "" Or txtID = "" Then
MsgBox "注意,班级名或所在第编号不能为空!", vbExclamation, " 提示"
txtName.SetFocus
Else
strSQL = "insert into Class values('" & txtName & "','" & txtID & "')"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Password=;Data Source="
strConn = strConn & App.Path & "\Student.mdb" & ";Persist Security Info=True"
Mycon.ConnectionString = strConn
Mycon.Open
Mycon.Execute strSQL
End If
End Sub
viena 2004-11-20
  • 打赏
  • 举报
回复
呵呵
btw505 2004-11-20
  • 打赏
  • 举报
回复
不要这几句,结果也一样的!怎么办?
tztz520 2004-11-20
  • 打赏
  • 举报
回复
Private Sub CmdOK_Click()
Dim Mycon As New ADODB.Connection
Dim Myrec As New ADODB.Recordset
Dim Mycom As New ADODB.Command

Dim strSQL As String, strConn As String


If txtName = "" Or txtID = "" Then
MsgBox "注意,班级名或所在第编号不能为空!", vbExclamation, " 提示"
txtName.SetFocus
Else
strSQL = "insert into Class values('" & txtName & "','" & txtID & "')"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Password=;Data Source="
strConn = strConn & App.Path & "\Student.mdb" & ";Persist Security Info=True"
Mycon.ConnectionString = strConn
Mycon.Open
Set Mycom.ActiveConnection = Mycon
Mycom.CommandText = strSQL
Set Myrec = Mycom.Execute
Myrec.update
'Myrec.Close不要这几句
'Set Myrec = Nothing
'Mycon.Close
'Set Mycon = Nothing
End If

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧