MS Access VBA - 使用 SELECT INTO 创建新表并添加记录

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

MS Access VBA - Using SELECT INTO to create a new table and add a record

sqlvbams-access

提问by Adam44

I'm trying to take a record from another table [Process Order] and put it into a new table with the date on it (NewTableName), but I keep getting 'Too few parameters. Expected 1'. I got it to work and pass one field over based on the [Process No] = txtProc or something earlier but can't get it to pass all the fields I need.

我正在尝试从另一个表 [Process Order] 中获取记录并将其放入带有日期的新表中 (NewTableName),但我一直收到“参数太少”的消息。预计 1'。我让它工作并根据 [Process No] = txtProc 或更早的东西传递了一个字段,但无法让它传递我需要的所有字段。

 Dim strSQL As String
 Dim NewTableName As String

 NewTableName = "[Process Order-Oven " & Format(Date, "DD-MM-YYYY") & "]"

 strSQL = "SELECT [Run No], [Product ID], [Process No], [Product Description], " & _
          "[Good Product Produced] INTO " & NewTableName & _
          " FROM 'Process Order' WHERE [Process No] = txtProc.Value"

 CurrentDb.Execute strSQL, dbFailOnError

采纳答案by PaulFrancis

[You need to concatenate the txt value. I hope it is a Number. If not enclose it between single quotes.

[您需要连接txt值。我希望它是一个数字。如果没有将其括在单引号之间。

 Dim strSQL As String
 Dim NewTableName As String

 NewTableName = "[Process Order-Oven " & Format(Date, "DD-MM-YYYY") & "]"

 strSQL = "SELECT [Run No], [Product ID], [Process No], [Product Description], " & _
          "[Good Product Produced] INTO " & NewTableName & _
          " FROM [Process Order] WHERE [Process No] = " & txtProc

 CurrentDb.Execute strSQL, dbFailOnError

回答by ron tornambe

I removed the txtProc.Value from the string so that the value would be supplied - I assume it is a text field, so it is enclosed with quotes. This should solve the "too few parameters" error. I also changed the 'Process Order' to use bracket delimiters.

我从字符串中删除了 txtProc.Value 以便提供该值 - 我假设它是一个文本字段,所以它用引号引起来。这应该可以解决“参数太少”错误。我还更改了“流程顺序”以使用括号分隔符。

strSQL = "SELECT [Run No], [Product ID], [Process No], [Product Description], " & _
         "[Good Product Produced] INTO " & NewTableName & _
         " FROM [Process Order] WHERE [Process No] = '" & txtProc.Value & "'"