使用 SQL 语句在 VBA 中创建记录集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/192707/
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
Creating Recordset in VBA wit SQL statement
提问by FirebirdVII1963
I am trying to create a recordset in Access VBA that will show me all records in a table related to the current record of a form. My current code looks like this:
我正在尝试在 Access VBA 中创建一个记录集,它将向我显示与表单当前记录相关的表中的所有记录。我当前的代码如下所示:
Private Sub Form_Load()
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("Select [ID], [Ln] From [Order Detail] Where ((([Order Detail].[ID]) = [Forms]![Order Data Entry Header]![ID]))")
rst.MoveLast
Forms![Order Data Entry Header].LineNum = rst![Ln]
End Sub
I am doing this so that when adding new records they can be numbered sequentially after the highest number. When I run the form it get "Run-time Error: '3061' Too few parameters. Expected 1." on the Set rst line.
我这样做是为了在添加新记录时,它们可以在最高数字之后按顺序编号。当我运行表单时,它得到“运行时错误:'3061'参数太少。预期为 1。” 在设置第一行。
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Pittsburgh DBA
The issue is the fact that the string you see there is exactly what is getting passed to the driver.
问题在于您在那里看到的字符串正是传递给驱动程序的内容。
You need to "build up" the string, like so:
您需要“建立”字符串,如下所示:
Set rst = CurrentDb.OpenRecordset("Select [ID], [Ln] From [Order Detail] Where ((([Order Detail].[ID]) = " & [Forms]![Order Data Entry Header]![ID] & "))")
Watch to make sure that [Forms]![Order Data Entry Header]![ID] is safe content, since you are building up an SQL statement.
注意确保 [Forms]![Order Data Entry Header]![ID] 是安全内容,因为您正在构建 SQL 语句。