我在以下代码中遇到问题:
Set con = Application.CurrentProject.Connection
Set rs = CreateObject("ADODB.Recordset")
stSql = "SELECT * FROM [SB Items] "
stSql = stSql & "WHERE [SBID]=" & Me![SBID] & " AND [ItemNumber]=" & intBtn
rs.Open stSql, con, 1 ' 1 = adOpenKeyset
' If no item matches, report the error and exit the function.
If (rs.EOF) Then
MsgBox "There was an error reading the SB Items table."
rs.Close
Set rs = Nothing
Set con = Nothing
Exit Function
End If
因为我在 stSql 中执行上面的sql命令,导致只有一行
控件将转到错误块
和弹出消息框说“读取SB项目表时出错”
答案 0 :(得分:1)
使用此代码查看您获取的记录数。
if rs.Supports(adApproxPosition)=true then
i=rs.RecordCount
MsgBox "The number of records is: " & i
end if
'
' If no item matches, report the error and exit the function.
If (rs.EOF) Then
'and on as you have it
另外,如果有任何信息,请向err.desc报告以获取更多信息。
最后,检查Con是否附加到您认为正在使用的数据库中!
答案 1 :(得分:0)
2件事:
我注意到你设置了两次stSQL,我猜测是否存在换行原因。更简单的方法是使用“_”(后跟下划线的空格)。这使您可以将命令传送到下一行。
从以下位置更改Msgbox错误:
MsgBox“读取SB Items表时出错。”
于:
Msgbox“错误:”& err.desc
编辑:err.desc为您提供生成的确切错误代码。它并不总是有用,但它是你能得到的最好的。
答案 2 :(得分:0)
您没有错误阻止,您检查了rs.eof。
我倾向于选择使用
检查零行if (rs.eof) and (rs.bof)..
因为只有当记录集为空时才会出现这种情况,而不仅仅是当指针不在你预期的位置或者胖胖光标仍在填充时。
你确定代码没有完全按照它的设计去做,并在没有行时显示消息吗?
尝试使用debug.print stSQL来获取正在运行的实际查询并尝试直接针对数据库运行该查询。这将证实两件事: