链接标准ms访问

时间:2016-08-30 17:38:49

标签: vba ms-access subquery

stLinkCriteria = "[UpdateStatus] = '" & _
    Forms![Updates-Search Menu]!LookupUpdateStatus & "'" & " And _
    [Office] = '" & [Office] & "'"

试图将我的搜索限制在链接条件中。它会查看更新状态和办公室并正确过滤我的表单。上面的代码是我目前工作的基本代码。我想再添加一个标准,看起来像这样 更新状态和(办公室或共享)。这是我的尝试,但不起作用

stLinkCriteria = "[UpdateStatus] = '" & _
    Forms![Updates-Search Menu]!LookupUpdateStatus & "'" & " _
    And [Office] = '" & [Office] & "'" Or _
    "[Forms]![Updates]!ShareWithOtherOffice = -1"

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

应该是

stLinkCriteria = "[UpdateStatus] = '" & _
    Forms![Updates-Search Menu]!LookupUpdateStatus & _
    "' And ([Office] = '" & [Office] & _
    "' Or [Forms]![Updates]!ShareWithOtherOffice <> 0)"

由于AND在默认情况下在OR之前执行,因此必须将办公室内容括在括号中,以便在AND之前执行OR。

我也应该在字符串中。

我使用的函数Build使其更简单

Function Build(ByVal s As String, ParamArray args()) As String
    'Build("LastName = '{0}', FirstName = '{1}'","Doe","John").

    Dim i As Long

    For i = 0 To UBound(args)
        s = Replace(s, "{" & i & "}", Nz(args(i)))
    Next i
    Build = s
End Function
sql = "[UpdateStatus] = '{0}' And ([Office] = '{1}' Or {2})"
stLinkCriteria = Build(sql,
                       Forms![Updates-Search Menu]!LookupUpdateStatus, _
                       [Office], _
                       [Forms]![Updates]!ShareWithOtherOffice <> 0)