在VBA中查询中使用声明的string更容易

我正在写一个macros,它应该运行查询将数据从excel传输到Access数据库,并且一切工作正常,但是,如果我想要在这些查询之一中使用string,我必须键入它们,如下所示:

'" & Lijn & "' 

我知道下面的代码(使用VBA编写)通过使用问号和setString更容易在Javascript中编写:

VBA:

 Dim ShiftsQ As String ShiftsQ = "INSERT INTO Shifts(Lijn, Operator, Ploeg, Teamleider) VALUES ('" & Lijn & "', '" & Operator & "', '" & Ploeg & "', '" & Teamleider & "');" 

使用Javascript:

 var ShiftsQ = SQL.prepareStatement(INSERT INTO Shifts(Lijn, Operator, Ploeg, Teamleider) VALUES (?, ?, ?, ?); ShiftsQ.setString(1, Lijn); ShiftsQ.setString(2, Operator); ShiftsQ.setString(3, Ploeg); ShiftsQ.setString(4, Teamleider); 

反正有写VBA代码像Javascript一样吗?

据我所知,没有什么像.NET的string.Format()方法的VBA。 但是你可以编写自己的版本,使用deputys并返回一个格式化的string。

 Private Sub Main() ' Your actual query ' The deputys are indexed in curley brackets (inspired from the .NET syntax of the equivalent function, making your code easy to read for .NET programmers) Dim qry As String qry = "SELECT {0}, {1} FROM {2} WHERE {3}" ' The values to set for the deputys in your query Dim parameters(3) As String parameters(0) = "firstname" parameters(1) = "lastname" parameters(2) = "users" parameters(3) = "userID = 'M463'" ' For demo purposes, this will display the query in a message box ' Instead of the MsgBox, you would use the formatted query to execute against the database MsgBox FormatString(qry, parameters) End Sub ' This is where the magic happens, the deputys in the given string will be replaced with the actual values from the provided array Private Function FormatString(strInput As String, paramValues() As String) ' This will be our return value Dim strOutput As String strOutput = strInput ' Verify that the given count of parameters matches the given count of deputys in the input string Dim maxParamIndex As Integer maxParamIndex = UBound(paramValues) Dim deputyCount As Integer For i = 1 To Len(strOutput) + 1 Step 1 If Mid(strOutput, i, 3) = "{" & deputyCount & "}" Then deputyCount = deputyCount + 1 End If Next ' If there is a mismatch between the count of parameters and the count of deputys, display exception message and exit the function ' Adding +1 to maxParamIndex is neccessary, as maxParamIndex refers to the maximum index (starting at 0, not 1) of the given array and deputyCount refers to the actual count of deputys (starting at 1) If maxParamIndex + 1 <> deputyCount Then MsgBox "Number of deputys has to match number of parameters for the given string:" & vbCrLf & strInput, vbCritical, "Exception in Function FormatString" FormatString = "" End If ' Iterate through the array and replace the deputys with the given values For i = 0 To maxParamIndex Step 1 strOutput = Replace(strOutput, "{" & i & "}", paramValues(i)) Next ' return the formatted string FormatString = strOutput End Function 

例子的结果:

在这里输入图像描述

如果我面对这个问题,我可以自己解决(也可能有其他“标准”解决scheme),通过定义我自己的简单的全局函数(放入任何标准的代码模块)

 Public Function S_(str as String) as String S_ = chr(39) & str & chr(39) End Function ShiftsQ = "INSERT INTO Shifts(Lijn, Operator, Ploeg, Teamleider) VALUES (" & S_(Lijn) & ", " & S_(Operator) & ", " & S_(Ploeg) & ", " & S_(Teamleider) & ");" 

这样,我将在我的所有项目中遵循一个简单而系统的规则,即在我的查询中对任何文本types的参数调用S_(param)