Ms access vba access 2007从函数打开查询
为了避免重复代码,我尝试在模块中使用函数,而不是与表单相关的子函数。潜艇已经由其他人制造了,我只需要做一些小的修改,使它作为一个功能正常工作。这样做,我陷入了一个非常简单的问题,可悲的是,我无法在其他地方找到解决办法 在这个函数中,我希望以字符串的形式打开数据库中的查询,因为在执行查询之前,会向查询添加额外的过滤器。我应该如何打开此查询 代码原为子代码,如下所示Ms access vba access 2007从函数打开查询,ms-access,vba,Ms Access,Vba,为了避免重复代码,我尝试在模块中使用函数,而不是与表单相关的子函数。潜艇已经由其他人制造了,我只需要做一些小的修改,使它作为一个功能正常工作。这样做,我陷入了一个非常简单的问题,可悲的是,我无法在其他地方找到解决办法 在这个函数中,我希望以字符串的形式打开数据库中的查询,因为在执行查询之前,会向查询添加额外的过滤器。我应该如何打开此查询 代码原为子代码,如下所示 Private Sub Unhide_Click() Dim query_pre As String Dim wher
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
Private Sub Unhide_Click()
Dim查询\u pre作为字符串
像字符串一样变暗
其中str=“(和((IIF(IsNull([Actions complete].OTL_man),([Actions complete].ATL_man)
现在我想修改这段代码,以便“操作完成”可以根据情况而变化。因此,我转移到一个函数,正如所说的那样,以避免重复代码
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
函数邮件过滤器(过去作为布尔值取消隐藏,不活动作为布尔值取消隐藏)
Dim查询\u pre作为字符串
朦胧如弦
将相关查询设置为字符串
如果Inactive_unhide=False,则
Relevant_Query=DoCmd.OpenQuery“[Actions complete]”这就是我现在的困境
其他的
related_Query=DoCmd.OpenQuery([操作完成,不活动])“这就是我现在的困境
如果结束
其中=“AND”((IIF(isnull([Relevant_Query].OTL_man),([Relevant_Query].ATL_man)DoCmd.OpenQuery将查询的名称作为字符串参数,因此必须使用“”之类的
DoCmd.OpenQuery "[Actoions Complete]"
另一方面,如果希望包含过滤器,可以考虑使用VBa或QueryDef对象来处理传递给查询的参数,甚至修改查询。DoCmd.OpenQuery将查询的名称作为字符串参数,因此必须使用“”之类的
DoCmd.OpenQuery "[Actoions Complete]"
Docmd.OpenQuery "Actions complete"
另一方面,如果希望包含过滤器,可以考虑使用VBa或QueryDef对象来处理传递给查询的参数,甚至修改查询。DoCmd.OpenQuery将查询的名称作为字符串参数,因此必须使用“”之类的
DoCmd.OpenQuery "[Actoions Complete]"
Docmd.OpenQuery "Actions complete"
另一方面,如果希望包含过滤器,可以考虑使用VBa或QueryDef对象来处理传递给查询的参数,甚至修改查询。DoCmd.OpenQuery将查询的名称作为字符串参数,因此必须使用“”之类的
DoCmd.OpenQuery "[Actoions Complete]"
Docmd.OpenQuery "Actions complete"
另一方面,如果希望包含过滤器,可以考虑使用VBa或QueryDef对象来处理传递给查询的参数,甚至修改查询
Docmd.OpenQuery "Actions complete"
应该这样做。OpenQuery
需要一个字符串作为查询的名称
应该这样做。OpenQuery
需要一个字符串作为查询的名称
应该这样做。OpenQuery
需要一个字符串作为查询的名称
应该可以了。OpenQuery
需要一个字符串作为查询的名称。问题有点不清楚。我想你期望的是:
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..."
问题有点不清楚。我想你们期待的是:
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..."
问题有点不清楚。我想你们期待的是:
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..."
问题有点不清楚。我想你们期待的是:
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..."
感谢您的回复。遗憾的是,这不起作用,我收到了错误:编译错误-预期的函数或变量。也许我应该指定我指的是哪个数据库,我应该怎么做?@user3932582您到底在哪里使用代码?您能编辑您的原始帖子以显示完整的代码吗?完成。提前感谢您的查看@user3932582,我认为问题在于您试图将DoCmd.OpenQuery分配给字符串。DoCmd是一个函数,只需将其称为DoCmd.OpenQuery“操作完成”",无需将其分配给某个对象。感谢您的回复。遗憾的是,这不起作用,我收到了错误:编译错误-预期的函数或变量。也许我应该指定我引用的数据库,我应该怎么做?@user3932582您到底在哪里使用代码?您可以编辑您的原始帖子以显示完整的代码吗恩,提前谢谢你给我上厕所