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

标签 excel ms-access vba

我正在编写一个宏,它应该运行查询以将数据从 Excel 传输到 Access 数据库,并且一切正常,但是,如果我想在其中一个查询中使用字符串,我必须键入它们像这样:

'" & Lijn & "'

我知道通过使用问号和 setString,使用 Javascript 编写以下代码(用 VBA 编写)要容易得多:

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

有没有办法像 JavaScript 一样编写 VBA 代码?

最佳答案

据我所知,没有什么比 .NET string.Format() 方法 VBA 更好的了。但是您可以编写自己的此类函数版本,该函数使用代理并返回格式化字符串。

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

关于excel - 在 VBA 中的查询中使用声明的字符串的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41482818/

相关文章:

Excel-VBA : Unable to Insert Line Breaks

c# - Dapper 与 MS Access 更新和插入问题

sql - 如果连接ODBC,MS Access是否支持 "CASE WHEN"子句?

excel - 编译错误: Expected Separator or )

sql - ms-access:而不是行源,从查询运行

excel - 如何在Excel中将第一个字母小写

Excel表格: How do I use ONLY the first 30 or 60 rows

python - 在Python中重命名文件: WindowsError: [Error 2] The system cannot find the file specified

SQL:HAVING 子句

vba - 日期清理功能