在VBA中使用查询中声明的字符串的更简单方法

时间:2017-01-05 10:39:21

标签: excel vba excel-vba ms-access access-vba

我正在编写一个应该运行查询以将数据从excel传输到Access数据库的宏,并且一切正常,但是,如果我想在其中一个查询中使用字符串,我就是这样做的。我可以这样打字:

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

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

VBA:

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);

的Javascript:

Deny from all

有没有像Javascript那样编写VBA代码?

2 个答案:

答案 0 :(得分:2)

据我所知,没有像.NET string.Format()方法VBA那样的东西。但是你可以编写自己的这种函数版本,使用deputys并返回一个格式化的字符串。

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

示例结果:

enter image description here

答案 1 :(得分:1)

如果我遇到这个问题,我会自己解决它(可能还有其他的#34;标准&#34;解决方案),通过定义我自己的简单全局函数(放入任何标准代码模块)

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) & ");"

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