【问题标题】:Easier way to use declared Strings in Query in VBA在 VBA 的查询中使用声明字符串的更简单方法
【发布时间】:2017-01-05 20:23:41
【问题描述】:

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

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

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

【问题讨论】:

    标签: excel ms-access vba


    【解决方案1】:

    据我所知,没有什么比 .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
    

    示例结果:

    【讨论】:

    • 哇非常感谢,这个比A.S.H.提供的答案还要好。因为它使代码更加清晰。谢谢!
    • 不客气。 :-) 请注意,出于安全原因,您可能不应该向用户显示带有查询文本的异常消息框。但它使调试变得容易得多,并且当您的代码正确时,MsgBox 根本不应该出现,因此这在现实世界中不太可能成为问题。
    • 好的,谢谢你告诉我。我的代码还有一个问题:当我运行它时,出现以下错误:找不到项目或库它与这段代码有关:Dim paramCount As Integer maxParamIndex = UBound(paramValues)
    • 糟糕,这是一个错字。上面我已经更正了,当然应该是Dim maxParamIndex As Integer
    • 我发现 C:\Windows\System32 中缺少 mscomct2.ocx。现在一切正常!感谢您的支持!
    【解决方案2】:

    如果我遇到这个问题,我会简单地自己解决它(尽管可能有其他“标准”解决方案),方法是定义我自己的简单全局函数(放入任何标准代码模块中)

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

    【讨论】:

    • 谢谢,我一定会试试的!
    猜你喜欢
    • 2021-12-06
    • 1970-01-01
    • 2017-08-16
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    相关资源
    最近更新 更多