Recordset.Find Error: Rowset does not support scrolling backward with Stored Procedure

Recordset.Find Error: Rowset does not support scrolling backward with Stored Procedure

我正在尝试使用 Find 在我的记录集中查找值,因为 Seek 似乎不受支持,但我无法克服错误

Rowset does not support scrolling backward

根据this SO thread,我必须指定adOpenDynamic,但这并没有改变错误代码。

我正在使用一个存储过程 (sp_fkeys),其中 returns 所有 table 的所有键,在本例中为我指定的一个 table table 名字。

Private Sub maintablebox_Change()

Dim cnn As ADODB.Connection
Dim keys As ADODB.Recordset
Set cnn = New ADODB.Connection
connstring = "omitted"
cnn.Open connstring

Set keys = New ADODB.Recordset
keys.CursorLocation = adUseServer
query = "EXEC sp_fkeys @fktable_name = 'astAssets'"
keys.Open query, connstring, adOpenDynamic, adLockReadOnly

' >>>>>Error on the line below
keys.Find "PKTABLE_NAME = 'astAssetTypes'"

Debug.Print keys.Fields("FKCOLUMN_NAME")
End Sub

我测试了你的代码,可以重现错误。对我来说,通过添加 moveFirst 解决了这个问题。

Private Sub maintablebox_Change()

Dim cnn As ADODB.Connection
Dim keys As ADODB.Recordset
Set cnn = New ADODB.Connection
Set keys = New ADODB.Recordset

cnn.Open DBPORT

Set keys.ActiveConnection = cnn
keys.CursorType = adOpenStatic

keys.Open "Select city_name, afas_rel_number, city_code from pkn_cities where has_month_report = true order by city_name ASC;"

keys.MoveFirst 'this did the trick for me
keys.Find "city_code = '1366'"
Debug.Print keys.Fields("city_name")

End Sub

问题不在keys.CursorType语句,而在keys.CursorLocation语句

我将 keys.CursorLocation = adUseServer 替换为 keys.CursorLocation = adUseClient,这立即解决了问题。

工作代码变成了

Private Sub maintablebox_Change()

Dim cnn As ADODB.Connection
Dim keys As ADODB.Recordset
Set cnn = New ADODB.Connection
connstring = "omitted"
cnn.Open connstring

Set keys = New ADODB.Recordset
keys.CursorLocation = adUseClient
query = "EXEC sp_fkeys @fktable_name = 'astAssets'"
keys.Open query, connstring, adOpenDynamic, adLockReadOnly

keys.Find "PKTABLE_NAME = 'astAssetTypes'"

Debug.Print keys.Fields("FKCOLUMN_NAME")
End Sub

我在 this vbforums thread 上找到了解决方案。我注意到 OP 像我一样使用存储过程,所以我看到的错误代码可能是由于这个因素造成的。

我对 ADODB 记录集的了解有限,所以如果有人愿意解释为什么将 cursorlocation 设置为 adUseClient 可以解决我的问题,请做客。