如何使用管道作为VBA访问中的SQL查询的一部分

时间:2013-01-30 07:25:22

标签: sql access-vba pipe

我的访问数据库中有一个记录集,其中有一个名为“OperationDesc”的字段,用于存储包含管道(“|”)字符的字符串。每当我使用VBA SQL方法搜索我的记录集来识别其中包含管道字符的特定字符串时 - 我收到错误3077“表达式中垂直条的使用无效。

导致错误的部分是:

Set dbs = CurrentDb
qr = "SELECT * FROM [A500Constants] ORDER BY [A500Constants].[ID];"
Set rs = dbs.OpenRecordset(qr)
qr = "[A500Constants].[ID] = " & Me.Controls("AddRoutDesc" & spot).Value
**rs.FindFirst qr**

If rs.Fields("ID") = 1 And Not rs.Fields("OperationDesc") = Me.Controls("AddRoutDesc" & CStr(spot)).Value Then
    'If true at start of rs when not suppose to be - signifies new entry time
    'if Me.Controls("AddRoutDesc" & CStr(Spot)).Value returned a description rather than ID the If would evaluate properly for the 1st item on the routing too
    'beginning process of validating new operation data

当Me.Controls(“AddRoutDesc”& spot)时,程序会在“rs.FindFirst qr”上发出错误.Value是一个包含|的字符串。管道人物。

这是尝试做什么:有n个组合框,其命名约定为AddRoutDesc0,AddRoutDesc1,....从那里我允许用户输入任何文本s /他希望。离开组合框后,我通过标题为“OperationDesc”的列搜索表/记录集,以查看之前是否使用过该描述。 (我知道我正在搜索ID - 我想在OperationDesc字段上搜索,因为这是允许用户同时输入新描述的唯一方法b / c多列组合框之间的冲突列表属性的链接。)组合框中填充了可供选择的先前描述,其中一些具有管道|字符。当我选择了这样的描述并离开运行上述内容的组合框时,会出现此错误。如果有一些更简单的方法 - 我很满意或者如果有人知道如何让SQL将管道视为字符串的另一部分,那就完美了。

我目前准备好以下代码,它可以搜索| (或任何真正的东西)并用一些额外的分隔符和管道替换它,使它不在SQL中标记w / e。

Private Function SQLStringFixer(InputString As String, ByVal FindString As String, ByVal ReplacementString As String, ByVal CompType As Integer) As String
   If Not IsNull(InputString) Then
        Dim WorkingStr As String
        Dim Pntr As Integer

        WorkingStr = InputString
        Pntr = InStr(1, WorkingStr, FindString, CompType)

        Do While Pntr > 0
            WorkingStr = Left(CStr(WorkingStr), CLng(Pntr - 1)) & ReplacementString & Mid(WorkingStr, Pntr + Len(FindString))
            Pntr = InStr(Pntr + Len(ReplacementString), WorkingStr, FindString, CompType)
        Loop
        SQLStringFixer = WorkingStr
    Else
        SQLStringFixer = ""
    End If
End Function

Private Function FixStr4JetSQL(InputString As String) As String
     Dim Temp As String
     Temp = SQLStringFixer(InputString, "'", "''", vbBinaryCompare)
     FixStr4JetSQL = SQLStringFixer(Temp, "|", "' & chr(124) & '", vbBinaryCompare)
End Function

我最终只需知道哪些字符或一组字符会让SQL中的查询看到|管道作为管道,而不是一些关键符号做w / e。如果有人知道这是什么 - 这将是一个很大的帮助 - 这样一个微不足道的事情花了我这么多时间。

1 个答案:

答案 0 :(得分:2)

  

rs.FindFirst qr是包含Me.Controls("AddRoutDesc" & spot).Value竖线字符的字符串时,程序会在|处出现错误。

我不确定它是否适用于包含除数字之外的任何字符串或NULL之类的SQL关键字。

考虑Me.Controls("AddRouteDesc" & spot).Value是否包含iamastringwithoutapipe。这一行:

qr = "[A500Constants].[ID] = " & Me.Controls("AddRoutDesc" & spot).Value

相当于:

qr = "[A500Constants].[ID] = " & "iamastringwithoutapipe"

相当于:

qr = "[A500Constants].[ID] = iamastringwithoutapipe"

我认为正确的陈述是:

qr = "[A500Constants].[ID] = '" & Me.Controls("AddRoutDesc" & spot).Value & "'"

相当于:

qr = "[A500Constants].[ID] = 'iamastringwithoutapipe'"

总体而言,使用FindFirst这样is quite fragile。你可能最好写一些parameterised queries和/或不使用Access。