Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VBA access中将管道作为SQL查询的一部分使用_Sql_Ms Access_Vba_Pipe - Fatal编程技术网

如何在VBA access中将管道作为SQL查询的一部分使用

如何在VBA access中将管道作为SQL查询的一部分使用,sql,ms-access,vba,pipe,Sql,Ms Access,Vba,Pipe,我的access数据库中有一个记录集,它有一个名为“OperationDesc”的字段,用于存储包含管道(“|”)字符的字符串。每当我使用VBA SQL方法在我的记录集中搜索以识别包含管道字符的特定字符串时,我会收到一个错误3077“表达式中垂直条的使用无效” 导致错误的部分如下所示: Set dbs = CurrentDb qr = "SELECT * FROM [A500Constants] ORDER BY [A500Constants].[ID];" Set rs = dbs.OpenR

我的access数据库中有一个记录集,它有一个名为“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.Control(“AddRoutDesc”&spot)值是一个包含|管道字符的字符串时,程序会在“rs.FindFirst qr”处出错

这是要做什么:有n个组合框,命名约定为AddRoutDesc0,AddRoutDesc1,…。从那里,我允许用户输入他/她想要的任何文本。离开组合框后,我通过名为“OperationDesc”的列搜索表/记录集“查看以前是否使用过此说明。(我知道我目前正在ID上搜索-我想在OperationDesc字段上搜索,因为这是允许用户同时输入新描述的唯一方法,因为多列组合框和“链接到列表”属性之间存在冲突。)组合框中填充了可供选择的先前描述,其中一些具有管道特性。当我选择了这样一个描述,并且离开了运行上述描述的组合框时,就会出现这个错误。如果有更简单的方法-我洗耳恭听,或者如果有人知道如何让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的关键符号。如果有人知道这是什么-这将是一个很大的帮助-这样一件小事花费了我这么多时间

Me.Controls(“AddRoutDesc”&spot)时,程序在
rs.FindFirst qr
处出错。Value
是一个包含管道字符的字符串

我不确定它是否适用于除数字或SQL关键字以外的任何字符串,如
NULL

考虑
Me.Controls(“AddRouteDesc”和spot).Value
是否包含
iamastringwithoutappe
。该行:

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

qr = "[A500Constants].[ID] = " & "iamastringwithoutapipe"
qr = "[A500Constants].[ID] = iamastringwithoutapipe"
qr = "[A500Constants].[ID] = 'iamastringwithoutapipe'"
相当于:

qr = "[A500Constants].[ID] = " & "iamastringwithoutapipe"
qr = "[A500Constants].[ID] = iamastringwithoutapipe"
qr = "[A500Constants].[ID] = 'iamastringwithoutapipe'"
我认为正确的说法应该是:

qr = "[A500Constants].[ID] = '" & Me.Controls("AddRoutDesc" & spot).Value & "'"
这相当于:

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

总的来说,像这样使用
FindFirst
。你最好写一些和/或不使用Access。

qr=“[a500常量].[ID]=”&Me.Controls(“AddRoutDesc”&spot.Value”)
你不应该用管道在表达式周围加引号吗?你是说你有一个字段,里面有多个描述吗?这看起来很像一个设计错误。如果有一个描述表,每个描述都有一行(),那么就可以省去很多麻烦。为什么要使用FindFirst而不是将WHERE语句附加到SQL中?当您还没有使用LIKE时,我对它的工作原理感到困惑。谁有时间建立一个合适的关系模型?我在用无线电工具!我们以后总是能够快速更改它,对吗?@ta.speot.is我建议使用比编写代码更快的设计方法。Access之所以成为RAD工具,主要是因为您可以避免编写代码。关键的一点是“省去你自己的很多麻烦”。好的,我刚刚将行改为:qr=“[a500常量]。[OperationDesc]=”“”&Me.Controls(“AddRoutDesc”&spot)。Value&”“”rs.FindFirst qr它现在可以工作了。。。我向上帝发誓-我昨天做了这件事,但没有成功。。。也许我的工作比较有问题-谢谢你的专业提示,因为显然这就是我所需要做的为什么不使用Access?让我们说这是一个小企业,接入是廉价、明智的选择,而且一直如此。如果使用不当,几乎任何东西都是“易碎的”。参数查询是一个很好的主意,但它们需要时间,而且有时时间不可用,毕竟Access是一个很好的工具。@Remou
“最好不要使用Access!”“请勿使用Access”
我尝试了qr=“[A500Constants].[ID]=”&Me.Controls(“AddRoutDesc”&spot.Value&“”)这不起作用,您之前的评论是对的,它是qr=“[A500Constants].[OperationDesc]=”&Me.Controls(“AddRoutDesc”&spot.Value&“