简体   繁体   English

我可以从Excel VBA中的.sql文件运行T-SQL更新查询并包含变量吗?

[英]Can I run T-SQL update query from .sql file in Excel VBA and include variables?

I am new to SQL Express 2012. 我是SQL Express 2012的新手。
I have an SQL statement running directly from Excel 2010 VBA which updates the database, similar to below. 我有一条直接从Excel 2010 VBA运行的SQL语句,该语句更新了数据库,如下所示。 I am using ADO and the command .Execute(stSQL) from within Excel. 我在Excel中使用ADO和命令.Execute(stSQL)。

stSQL = "UPDATE Organisation " _
   & "SET Org_Ref = '" & usrformtextbox1.Value & "', " _
        & "Org_Name = '" & usrformtextbox2.Value & "', " _
        & "Contact_Lastname = '" & usrformtextbox3.Value & "', " _
        & "Contact_Firstname = '" & usrformtextbox4.Value & "', " _
        & "Org_Contact_Email = '" & usrformtextbox5.Value & "', " _
        & "Org_Contact_Tel = '" & usrformtextbox6.Value & "', " _
    & "WHERE Org_ID = " & prikey

I want to take this 'hard coded' query out of Excel, save it, and execute it from an .sql file. 我想从Excel中取出此“硬编码”查询,将其保存,然后从.sql文件执行它。
I have saved this into an .sql file. 我已经将其保存到一个.sql文件中。 I can read the .sql file into the variable stSQL, the textboxes are populated,but it will not now run with .Execute(stSQL). 我可以将.sql文件读入变量stSQL中,将填充文本框,但现在将无法使用.Execute(stSQL)运行它。
I get an "Incorrect syntax near '&'" error. 我收到“&'附近的语法不正确”错误。
Clearly I want the variables to be populated from my userfrom textboxes, automatically. 显然,我希望从文本框的用户中自动填充变量。
Can anyone shed any light on what I am doing wrong and advise on my approach, syntax etc. Thank you. 任何人都可以对我做错的事情有所了解,并就我的方法,语法等提出建议。谢谢。

try this: 尝试这个:

stSQL = "UPDATE Organisation " _
   & "SET Org_Ref = '" & usrformtextbox1.Value & "', " _
        & "Org_Name = '" & usrformtextbox2.Value & "', " _
        & "Contact_Lastname = '" & usrformtextbox3.Value & "', " _
        & "Contact_Firstname = '" & usrformtextbox4.Value & "', " _
        & "Org_Contact_Email = '" & usrformtextbox5.Value & "', " _
        & "Org_Contact_Tel = '" & usrformtextbox6.Value & "' " _
    & "WHERE Org_ID = " & prikey

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

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