vb中出现数据访问错误 20

OptionExplicitDimrs_bookAsNewADODB.RecordsetDimjiefouAsStringPrivateSubCommand1_Click... Option Explicit
Dim rs_book As New ADODB.Recordset
Dim jiefou As StringPrivate Sub Command1_Click()
Dim Cmd As New ADODB.Command
Dim rs_searchbook As New ADODB.Recordset
Const cntstring = "Provider=SQLOLEDB.1;password=l32;Persist Security Info=False;User ID=sa;Initial Catalog=图书;"
conn.Open cntstring
With Cmd
Dim sql As String

If Check1.Value = vbChecked Then
sql = "书名='" & Trim(Text1.Text & "") & "'"
End If
If Check2.Value = vbChecked Then
If Trim(sql) = "" Then
sql = "类别='" & Trim(Combo1.Text & "") & "'"
Else
sql = sql & "and 书名='" & Trim(Combo1.Text & "") & "'"
End If
End If
If Check3.Value = vbChecked Then
If Trim(sql) = "" Then
sql = "作者='" & Trim(Text2.Text & "") & "'"
Else
sql = sql & "and 作者='" & Trim(Text2.Text & "") & "'"
End If
End If
If Check4.Value = vbChecked Then
If Trim(sql) = "" Then
sql = "出版社='" & Trim(Text3.Text & "") & "'"
Else
sql = sql & "and 出版社='" & Trim(Text3.Text & "") & "'"
End If
End If
If Check5.Value = vbChecked Then
If Trim(sql) = "" Then
sql = "书籍编号='" & Trim(Text4.Text & "") & "'"
Else
sql = sql & "and 书籍编号='" & Trim(Text4.Text & "") & "'"
End If
End If
If Trim(sql) = "" Then
MsgBox "请选择查询方式!", 64, "提示"
Exit Sub
End If
sql = "select * from 图书信息 where " & sql
rs_searchbook.CursorLocation = adUseClient
rs_searchbook.Open sql, conn, adOpenKeyset, adLockPessimistic
DataGrid1.AllowAddNew = False
DataGrid1.AllowDelete = False
DataGrid1.AllowUpdate = False
Set DataGrid1.DataSource = rs_searchbook
DataGrid1.Refresh
Set conn = Nothing
End With
End Sub

Private Sub Command2_Click()
Unload Me
End Sub

Private Sub Command3_Click()
If Trim(book_num) = "" Then
MsgBox "请选择要借阅的图书!", 64, "提示"
Exit Sub
End If
If jiefou = "是" Then
MsgBox "此书已被借出!", 64, "提示"
Exit Sub
End If
frmborrowbook.Show

End Sub

Private Sub DataGrid1_Rowcolchange(LastRow As Variant, ByVal lastcol As Integer)
book_num = DataGrid1.Columns(0).CellValue(DataGrid1.Bookmark)
jiefou = DataGrid1.Columns(8).CellValue(DataGrid1.Bookmark)

End Sub

Private Sub Form_Load()
Dim rs_searchbookkind As New ADODB.Recordset
Dim sql As String
Const cntstring = "Provider=SQLOLEDB.1;password=l32;Persist Security Info=False;User ID=sa;Initial Catalog=图书;"
conn.Open cntstring
sql = "select * from 图书类别"
rs_searchbookkind.Open sql, conn, adOpenKeyset, adLockPessimistic
rs_searchbookkind.MoveFirst
If Not rs_searchbookkind.EOF Then
Do While Not rs_searchbookkind.EOF
Combo1.AddItem rs_searchbookkind.Fields(0)
rs_searchbookkind.MoveNext
Loop
Combo1.ListIndex = 0
End If
rs_searchbookkind.Close
Set conn = Nothing
End Sub
出错句

book_num = DataGrid1.Columns(0).CellValue(DataGrid1.Bookmark)
界面可以出来,但是datagrid1中没有出现要查找的数据
展开
 我来答
百度网友95cc616
2014-03-21 · TA获得超过251个赞
知道小有建树答主
回答量:448
采纳率:100%
帮助的人:273万
展开全部
datagrid1中关联的数据集对象是你在Command1_Click事件中定义的一个局部对象,当事件完成后,局部对象被关闭和释放,所以你是看不到数据的,数据库查询界面至少要有一个窗体级的数据集对象存在的,另外的数据连接对象可以到退出时再关闭

Option Explicit
Dim rs_book As New ADODB.Recordset
Dim rs_searchbook As New ADODB.Recordset

Dim jiefou As StringPrivate Sub Command1_Click()
Dim Cmd As New ADODB.Command
...
这样好点,另外,在rs_searchbook.Open sql, conn, adOpenKeyset, adLockPessimistic之前还因加上句if rs_searchbook.State<>adStateClosed then rs_searchbook.Close
还有
Private Sub DataGrid1_Rowcolchange(LastRow As Variant, ByVal lastcol As Integer)
if rs_searchbook.RecordCount>0 then
book_num = DataGrid1.Columns(0).CellValue(DataGrid1.Bookmark)
jiefou = DataGrid1.Columns(8).CellValue(DataGrid1.Bookmark)
end if
End Sub
试试吧
ZHMINGEN
2014-03-18 · TA获得超过1735个赞
知道大有可为答主
回答量:3149
采纳率:68%
帮助的人:1252万
展开全部
看了一下,有问题,能不能发界面图片,给你修改。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式