vba recordset.movefirst "无法重新启动行集位置"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22356278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
recordset.movefirst "Rowset position cannot be restarted"
提问by PointXIV
I have a function that runs a stored procedure that returns only a single row and column (so one result).
我有一个运行存储过程的函数,该过程仅返回单行和单列(所以一个结果)。
I'm trying to get that one result into a variable so I can return it. I'm trying to use recordset.MoveFirst
but I get the "Rowset position cannot be restarted." error. I tried just removing it, since I only have one result, but I then get an overflow. My statement looks like this:
我试图将那个结果放入一个变量中,以便我可以返回它。我正在尝试使用,recordset.MoveFirst
但出现“无法重新启动行集位置”。错误。我尝试将其删除,因为我只有一个结果,但随后出现溢出。我的声明是这样的:
If recordset.EOF = False Then
recordset.MoveFirst
temp = rs!ID
End IF
temp
is an integer. I've checked the stored procedure to make sure it only returns the single result, and it does. Am I doing something wrong? Is there a better way to pass the result into a variable? It's possible the recordset is forward only (which means it's read only?) but I can't seem to find an answer as to how to fix that.
temp
是一个整数。我检查了存储过程以确保它只返回单个结果,并且确实如此。难道我做错了什么?有没有更好的方法将结果传递给变量?记录集可能是前向的(这意味着它是只读的?)但我似乎无法找到如何解决这个问题的答案。
采纳答案by Alex K.
There is usually no reason to MoveFirst
if you have not previously navigated the record set.
MoveFirst
如果您以前没有浏览过记录集,通常没有理由这样做。
The overflow
is unrelated to the database code and is caused by rs!ID
not fitting in a VBA integer
(16 bit) so make temp
a Long
instead (32 bit) and remove MoveFirst
.
的overflow
是无关的数据库代码,并且通过引起 rs!ID
在VBA不配合integer
(16位),从而使temp
一个Long
替代(32位)和删除MoveFirst
。
回答by Keith
Make sure you are not using a forward-only recordset. Recordsets are this way by default. Instead use a dynamic (adOpenDynamic) or static (adOpenStatic) cursor type.
确保您没有使用只进记录集。默认情况下,记录集是这种方式。而是使用动态 (adOpenDynamic) 或静态 (adOpenStatic) 游标类型。
You may also need to set CursorLocation = adUseClient
.
您可能还需要设置CursorLocation = adUseClient
.
Finally, check for BOF
before calling MoveFirst
.
最后,BOF
在调用之前检查MoveFirst
。
Example:
例子:
...
rs.CursorType = adOpenStatic
rs.CursorLocation = adUseClient
rs.Open "SELECT * FROM MyTable"
...
If (Not rs.BOF) Then
rs.MoveFirst
End If
回答by Tomek
You can use MoveFirst for forward only recordset ONLY when you are at EOF. Tricky, undocumented, but WORKS!!!!
仅当您处于 EOF 时,您才可以将 MoveFirst 用于前向记录集。棘手,无证,但有效!!!!