从文本文件中取出string,评估一次返回的variablesVBA / SQL Server
我使用以下代码从vba文件中获取SQL代码(本节中没有问题):
Function ReturnSql(filepath As String) As String Dim TextFile As Integer Dim FileContent As String 'Determine the next file number available for use by the FileOpen function TextFile = FreeFile 'Open the text file Open filepath For Input As TextFile 'Store file content inside a variable FileContent = Input(LOF(TextFile), TextFile) ReturnSql = FileContent 'Close Text File Close TextFile End Function
然后,我将代码放入VBA中的adodb连接。 一切顺利。 不过,我想添加一个variables。 如果这一切都在vba它看起来像这样:
sub RunSqlWithVariable dim strSQL as string strSQL = "SELECT ClientTerm FROM ClientTable WHERE ClientID = '" & strClient & "';" 'Put this into an adodb connection etc end sub
我已经将SQL过程存储在文本文件中。 这些就像这样:
SELECT ClientTerm FROM ClientTable WHERE ClientID = '" & strClient & ";'"
但是我不能得到variablesstrClient评估(不知道我的措辞在这里)。 有这个function吗? 或者我应该只使用查找和replace?
编辑:为了清楚起见,我想带回上面的string,并获取variables来显示一个值,我把它传递给代码的VBA部分,所以从SQL.txtinput将如上所述,string我想请参阅下面的vba:
SELECT ClientTerm FROM ClientTable WHERE ClientID = 'Opera Winfrey';
这是你正在尝试?
Sub Sample() Dim sql As String Dim sClient As String sClient = "Sid" strClient = ReturnSql("C:\Users\Siddharth Rout\Desktop\Sid.txt") strClient = Replace(strClient, "strClient", Chr(34) & sClient & Chr(34)) Debug.Print strClient End Sub
产量
SELECT ClientTerm FROM ClientTable WHERE ClientID = '" & "Sid" & ";'"