【问题标题】:vba access 2007 open query from functionvba access 2007从函数打开查询
【发布时间】:2020-07-13 21:23:30
【问题描述】:

为了避免重复代码,我尝试在模块中使用函数而不是与表单相关的子函数。该潜艇已经由其他人制作,我只需要进行一些小改动即可使其作为一项功能正常工作。这样做我遇到了一些非常简单的问题,遗憾的是我无法在其他地方找到解决方案。

在这个函数中,我想打开一个在我的数据库中作为字符串的查询,因为在查询被执行之前,额外的过滤器会被添加到查询中。我应该如何打开这个查询?

作为子代码的代码如下所示。

Private Sub Unhide_Click()
    Dim query_pre As String
    Dim whereStr As String

    whereStr = " (AND ((IIF(IsNull([Actions complete].OTL_man), ([Actions complete].ATL_man) < DateAdd('m', 6, Date()),
                                                               ([Actions complete].OTL_man) < DateAdd('m', 6, Date())))" & _
               " AND (([Actions complete].Finished) Is Null)))"

    owner_id = Nz(DLookup("ID", "Owners", "[Full Name] like '*" & prefilter.Value & "*' "), -1)
    sender_id = Nz(DLookup("ID", "Senders", "[Sender] like '*" & prefilter.Value & "*' "), -1)

    query_pre = " [Actions complete].[Ref_man] Like '*" & prefilter.Value & "*'" & _
                " OR [Actions complete].[owner_man] = " & owner_id & _
                " OR [Actions complete].[action_man] Like '*" & prefilter.Value & "*'" & _
                " OR [Actions complete].[Sender_man] = " & sender_id & _
                " OR [Actions complete].[ATL_man] Like '*" & prefilter.Value & "*'" & _
                " OR [Actions complete].[OTL_man] Like '*" & prefilter.Value & "*'" & _
                " OR [Actions complete].[finished] Like '*" & prefilter.Value & "*' "

    final_query2 = "SELECT [Actions complete].* FROM [Actions complete] WHERE (( " & query_pre & " ) " & whereStr & ") ORDER BY [OTL_man] ASC "

    final_query3 = "SELECT [Actions complete].* FROM [Actions complete] WHERE ( " & query_pre & " ) ORDER BY [OTL_man] ASC"

    If Unhide.Value = True And CurrentDb.OpenRecordset(final_query3).RecordCount <> 0 Then
        Me.RecordSource = final_query3
    ElseIf Unhide.Value = False And CurrentDb.OpenRecordset(final_query2).RecordCount <> 0 Then
        Me.RecordSource = final_query2
    Else
        MsgBox "There are no records matching the filter criteria. The filter was ignored"
    End If
End Sub

现在我想更改此代码,以便“操作完成”可以根据情况而变化。所以我转成一个函数,据说是为了避免重复代码。

Function mail_filter(Past_unhide As Boolean, Inactive_unhide As Boolean)
    Dim query_pre As String
    Dim where As String
    Dim Relevant_Query As String

    If Inactive_unhide = False Then
        Relevant_Query = DoCmd.OpenQuery "[Actions complete]"               'This is where i am stuck right now
    Else
        Relevant_Query = DoCmd.OpenQuery([Actions complete with inactive])  'This is where i am stuck right now
    End If

where = "AND ((  IIF(isnull([Relevant_Query].OTL_man), ([Relevant_Query].ATL_man)<DateAdd('m',6,Date()) ,([Relevant_Query].OTL_man)<DateAdd('m',6,Date()) ) ) AND (([Relevant_Query].Finished) Is Null)) "
query_pre = " "

owner_id = Nz(DLookup("ID", "Owners", "[Full Name] like '*" & prefilter.Value & "*' "), -1)
sender_id = Nz(DLookup("ID", "Senders", "[Sender] like '*" & prefilter.Value & "*' "), -1)

query_pre = " [Relevant_Query].[Ref_man] Like '*" & prefilter.Value & "*' OR [Relevant_Query].[owner_man] = " & owner_id & " OR [Relevant_Query].[action_man] Like '*" & prefilter.Value & "*' OR [Relevant_Query].[Sender_man] = " & sender_id & " OR [Relevant_Query].[ATL_man] Like '*" & prefilter.Value & "*' OR [Relevant_Query].[OTL_man] Like '*" & prefilter.Value & "*' OR [Relevant_Query].[finished] Like '*" & prefilter.Value & "*' "

final_query2 = "SELECT [Relevant_Query].* FROM [Relevant_Query] WHERE ( " & query_pre & " ) " & where & " ORDER BY [OTL_man] ASC"
final_query3 = "SELECT [Relevant_Query].* FROM [Relevant_Query] WHERE ( " & query_pre & " ) ORDER BY [OTL_man] ASC"
If Forms!mail_v3!Past_unhide.Value = True And CurrentDb.OpenRecordset(final_query3).RecordCount <> 0 Then
    Forms!mail_v3.RecordSource = final_query3
ElseIf Forms!mail_v3!Past_unhide.Value = False And CurrentDb.OpenRecordset(final_query2).RecordCount <> 0 Then
    Forms!mail_v3.RecordSource = final_query2
Else
    MsgBox "There are no records matching the filter criteria. The filter was ignored"
End If

End Function

不知何故,我不清楚我要打开什么查询。我该怎么做?

【问题讨论】:

  • 对代码进行了一些编辑,使其更具可读性,并添加了一些参数以使其更易读。

标签: ms-access vba


【解决方案1】:
Docmd.OpenQuery "Actions complete"

应该可以解决问题。 OpenQuery 需要一个字符串作为查询名称。

【讨论】:

  • 感谢您的回复。遗憾的是这不起作用,我收到错误:编译错误 - 预期的函数或变量。也许我应该指定我指的是哪个数据库,我应该怎么做?
  • 因为它是一个字符串,我非常怀疑方括号是名称的一部分。
  • 在哪一行?我认为该指令不能给出编译错误
  • 我也认为Relevant_Query = 永远不会起作用,这是你的意思吗?
  • @Fionnuala 我正在尝试选择八个“操作完成”查询或“操作完成但不活动”查询,具体取决于取消隐藏不活动的值。
【解决方案2】:

问题有点不清楚。我想你所期望的是这样的:

If Inactive_unhide = False Then
    Relevant_Query = "Actions complete" 
Else
    Relevant_Query = "Actions complete with inactive"
End If
'more stuff here
'....
final_query2 = "SELECT * FROM [" & Relevant_Query & "] WHERE..."

【讨论】:

  • 您好,我认为您最好将此添加到您之前的答案中。
  • 就是这样!现在我终于明白出了什么问题。结合更改其他字符串,我已经能够让它工作。非常感谢!
  • @Fionnuala:你是对的,但我认为现在有点晚了,不是吗? :-/
【解决方案3】:

DoCmd.OpenQuery 将 Query 的名称作为字符串参数,因此您必须使用 "" 之类的东西。

DoCmd.OpenQuery "[Actoions Complete]"

另一方面,如果您希望包含过滤器,您可能会考虑使用 VBa 或 QueryDef 对象来处理传递给 Query 的参数,或者甚至修改 Query。

【讨论】:

  • 感谢您的回复。遗憾的是这不起作用,我收到错误:编译错误 - 预期的函数或变量。也许我应该指定我指的是哪个数据库,我应该怎么做?
  • @user3932582 你到底在哪里使用代码?您可以编辑您的原始帖子以显示完整的代码吗?
  • 完成。提前感谢您的调查。
  • @user3932582,我认为问题在于您正在尝试将 DoCmd.OpenQuery 分配给字符串。 DoCmd 是一个函数,它需要简单地调用为 Docmd.OpenQuery "Actions Complete",无需将其分配给某个东西。
猜你喜欢
  • 1970-01-01
  • 2018-12-29
  • 2011-09-26
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 2012-09-25
相关资源
最近更新 更多