vba 运行时错误 3021 - 没有当前记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17336316/
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-08 15:59:44  来源:igfitidea点击:

Run time error 3021- no current record

vbams-accessms-access-2010

提问by Kaja

I want to link the result of a query to a Textbox but I get this error: here is my code:

我想将查询结果链接到文本框,但出现此错误:这是我的代码:

Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT XValue, YValue,Wert FROM tb_DCM_Daten WHERE (FzgID=" & Forms!frm_fahrzeug!ID & " AND Name='" & List2.Value & "')")
Text10.Text = rst!XValue //error in this line

It should be return c.a 20 record

它应该是 return ca 20 记录

Why do I get this error and how can I solve it?

为什么会出现此错误,我该如何解决?

回答by Gord Thompson

One possible reason for the error is that Nameis a reserved word in Access, so you should use

错误的一个可能原因是它Name是 Access 中的保留字,因此您应该使用

... & " AND [Name]='" & ...

You could also test for rst.EOFbefore trying to use rst!XValue. That is, to verify whether or not your query is returning at least one row you can add the code

您还可以rst.EOF在尝试使用rst!XValue. 也就是说,要验证您的查询是否返回至少一行,您可以添加代码

If rst.EOF Then
    MsgBox "The Recordset is empty."
End If

immediately after the .OpenRecordsetcall. If the Recordset isempty, then you'll need to verify your SQL statement as described by @GregHNZ in his comment above.

.OpenRecordset通话后立即。如果 Recordset空,那么您需要按照@GregHNZ 在上面的评论中的描述验证您的 SQL 语句。

回答by Nexus

Usually, I would do this. Create a new query in Access , switch to SQL View , Paste my code there and go to Design >> Run.

通常,我会这样做。在 Access 中创建一个新查询,切换到 SQL View ,在那里粘贴我的代码并转到 Design >> Run。

SELECT XValue, YValue,Wert FROM [tb_DCM_Daten] WHERE [FzgID]=12 AND [Name]='ABC';

if your query syntax is correct you should see the result otherwise error mssg will tell where you are wrong. I used to debug a much more complicated query than yours and this is the way that I've done. If there is still error, maybe you should try

如果您的查询语法正确,您应该会看到结果,否则错误 msg 会告诉您哪里错了。我曾经调试过比您的查询复杂得多的查询,这就是我所做的。如果仍然有错误,也许你应该尝试

Dim sql as String
sql = "SELECT...."
Set rst = CurrentDb.OpenRecordset(sql)

Another possible reason might be your table name. I just wonder what is your table name exactly ? if your table contains white space you should make it like this [DCM Daten].

另一个可能的原因可能是您的表名。我只是想知道你的表名到底是什么?如果你的表格包含空白,你应该像这样[DCM Daten]。

回答by jovenb

One more thing I like to add that may cause this, is your returning a sets of resultset that has "Reserved word" fields, for example:

我想补充的另一件事可能会导致这种情况,是您返回一组具有“保留字”字段的结果集,例如:

Your "Customers" table has field name like the following:

您的“客户”表具有如下所示的字段名称:

Custnum  | Date | Custname

we know that Datefield is a reserved word for most database

我们知道日期字段是大多数数据库的保留字

so when you get the records using

所以当你使用

SELECT * FROM Customers

this will possible return "No Current Record", so instead selecting all fields for that table, just minimize your field selection like this:

这可能会返回“无当前记录”,因此请选择该表的所有字段,只需最小化您的字段选择,如下所示:

SELECT custnum, custname FROM Customers

回答by wadlooper

After trying the solutions above to no avail, I found another solution: Yes/No fields in Access tables cannot be Null (See allenbrowne.com/bug-14)

在尝试上述解决方案无济于事后,我找到了另一个解决方案:Access 表中的 Yes/No 字段不能为 Null(请参阅allenbrowne.com/bug-14

Although my situation was slightly different in that I only got the "No current record." error when running my query using GROUPBY, my query worked after temporary eliminating the Yes/No field.

虽然我的情况略有不同,我只得到了“没有当前记录”。使用 GROUPBY 运行我的查询时出错,我的查询在临时消除是/否字段后工作。

However, my Yes/No field surprisingly did not contain any Nulls. But, troubleshooting led me to find an associated error that was indeed populating my query result with Null Yes/No values. Fixing that associated error eliminated the Null Yes/No values in my results, thus eliminating this error.

但是,我的 Yes/No 字段出人意料地不包含任何空值。但是,故障排除让我找到了一个相关的错误,该错误确实用 Null Yes/No 值填充了我的查询结果。修复该相关错误消除了结果中的 Null Yes/No 值,从而消除了此错误。