简体   繁体   English

在用户选择的特定表上运行一个MS Access SQL脚本

[英]Run one MS Access SQL script on a particular Table chosen by user

I have a MS Access 2016 database (*.accdb) with 20+ Tables. 我有一个包含20多个表的MS Access 2016数据库(* .accdb)。 Fields in each of them vary slightly from Table to Table. 每个表中的字段在表之间略有不同。 I've no VBA experience, so I'm sticking only to the SQL query below (redacted). 我没有VBA经验,所以我只坚持下面的SQL查询(已编辑)。

SQL script SQL脚本

myvar below is the parameter I'd like to be prompted when the script is run so that I enter the Table I want the changes applied to. 下面的myvar是我希望在运行脚本时提示的参数,以便我输入要应用更改的表。

PARAMETERS 
[myvar] TableID;

UPDATE 
[myvar]

INNER JOIN  
Excel_Data ON [myvar].[Part Number] = Excel_Data.[Part Number] 

SET 
[myvar].[Value] = '?', 
[myvar].Description = Excel_Data.Description, 
[myvar].[Ref] = '?'
.
.
.

WHERE 
[myvar].Description Is Null;

Output 产量

Error message: 错误信息:

Too few parameters. Expected 0.

What I need 我需要的

I prefer a solution for above in a SQL script form as above, not involving VBA, preferably. 我更喜欢以上述SQL脚本形式提供上述解决方案,最好不涉及VBA。 I'd like to enter the Table name when prompted so the script knows which table to UPDATE. 我想在出现提示时输入表名称,以便脚本知道要更新的表。 FYI: The PARAMETERS work when it is not a Table as I've shown in my script above. 仅供参考:如果参数不是如我上面的脚本中所示的表格,则它起作用。

Help/advise is highly appreciated. 帮助/建议受到高度赞赏。

EDIT 1 编辑1

Since it seems not possible to use parameters as Table names, could you suggest a VBA solution? 由于似乎无法使用参数作为表名,因此您可以建议使用VBA解决方案吗? A sample code, perhaps? 可能是示例代码?

As said in the comments, you can't really solve this without VBA. 如评论中所述,没有VBA,您将无法真正解决此问题。

You can store your SQL query in a string, and use a placeholder to indicate the tablename. 您可以将SQL查询存储在字符串中,并使用占位符指示表名。 Then get the tablename using an inputbox and replace the placeholder with the tablename. 然后使用输入框获取表名,并将占位符替换为表名。

Dim sqlString As String
sqlString = "UPDATE [%Placeholder%] "  & vbCrLf & _
"INNER JOIN Excel_Data ON [%Placeholder%].[Part Number] = Excel_Data.[Part Number]  "  & vbCrLf & _
"SET [%Placeholder%].[Value] = '?', " & vbCrLf & _
...
"WHERE [%Placeholder%].Description Is Null;"
sqlString = Replace(sqlString, "%PlaceHolder%", InputBox("Enter a tablename"))
CurrentDb.Execute sqlString

In a more mature solution, I'd create a form with a combobox containing all available table names, and add a function to sanitize tablenames (replace "]" with "]]") 在一个更成熟的解决方案中,我将创建一个带有包含所有可用表名的组合框的表单,并添加一个清理表名的函数(将“]”替换为“]]”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM