VB中出现的错误
Private Sub Command1_Click()
Dim dbrec As New ADODB.Recordset
Set dbrec.ActiveConnection = dbconnection
dbrec.Source = "select* from book where bookid='" & Trim(txtbookid) & " ' "
dbrec.CursorType = adOpenDynamic
dbrec.LockType = adLockOptimistic
dbrec.Open
If dbrec.EOF = ture And dbrec.BOF = True Then '判断是否已有馆藏书
dbrec.AddNew
dbrec("bookid") = txtbookid
dbrec("booktype") = cbobooktype
dbrec("isbn") = txtisbn
dbrec("bookname") = txtbookname
dbrec("publisher") = txtpublisher
dbrec("author") = txtauthor
dbrec("sizes") = booksize
dbrec("price") = CCur(txtprice)
dbrec("pages") = CInt(txtstocks)
dbrec("spare") = CInt(txtstocks)
dbrec("abstract") = txtabstract
dbrec("location") = txtlocation
dbrec("pubdate") = CDate(txtpubdate)
dbrec.Update
dbrec.Close
Set dbrec = Nothing
MsgBox "登记完毕"
Unload Me
Else
MsgBox "书号重复,请修复! "
txtbookid.SetFocus
dbrec.Close
Set dbrec = Nothing
End If
End Sub
执行时出现错误,上面写着:实时错误424,要求对象
这个很急,希望高手帮一下忙!
我试了 shipizheng的方法,解决了原先那个问题,但是我按确定后出来一个“书号重复,请修复”的对话框,按确定后出来一个“实时错误424,要求对象”按调试后,指示错误在“txtbookid.SetFocus”这行上,我发现shipizheng对vb好像还蛮在行的,我能加你QQ吗? 展开
dbconnection对象你定义没有?
如果没有,请按以下步骤:
①工程→引用→选中图中加蓝的地方(如下图)
②加入Dim dbconnection As New ADODB.Connection
如果还是出错,你把代码出错的地方图片给上传上来,让大家看看。
补充如下:
Dim dbrec As New ADODB.Recordset
Set dbrec.ActiveConnection = dbconnection
dbrec.Source = "select* from book where bookid='" & Trim(txtbookid) & " ' "
dbrec.CursorType = adOpenDynamic
dbrec.LockType = adLockOptimistic
dbrec.Open
把以上的代码换成以下的试试
Dim dbconnection As New ADODB.Connection
Dim dbrec As New ADODB.Recordset
dbconnection.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Uigur_Medicine" '注意把Uigur_Medicine这个数据库改成你的数据库名称
dbrec.open "select* from book where bookid='" & Trim(txtbookid) & " ' ",dbconnection,adOpenKeyset, adLockPessimistic
注意:用完后注意关闭 dbconnection 和dbrec两个实例化后的对象。还有我的数据库是用的sql2005,access数据库写的代码略有不同。
补充二:
①是不是没有名为txtbookid的文本框控件或者是名字写错了,你检查一下。②
MsgBox "书号重复,请修复! "
txtbookid.SetFocus
dbrec.Close
Set dbrec = Nothing
把以上的代码改成
MsgBox "书号重复,请修复! "
txtbookid.SetFocus
dbrec.Close
Set dbrec = Nothing
dbconnection.close