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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:18:18  来源:igfitidea点击:

recordset.movefirst "Rowset position cannot be restarted"

vbaexcel-vbaadoexcel

提问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.MoveFirstbut 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

tempis 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 MoveFirstif you have not previously navigated the record set.

MoveFirst如果您以前没有浏览过记录集,通常没有理由这样做。

The overflowis unrelated to the database code and is caused by rs!IDnot fitting in a VBA integer(16 bit) so make tempa Longinstead (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 BOFbefore 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 用于前向记录集。棘手,无证,但有效!!!!