从查询条件调用函数

时间:2015-08-20 18:30:07

标签: ms-access remote-access

我已经通过Google查看了很多其他人提出的要求和回答的内容,但还有一些其他人没有提及的问题。我的情况是我从控制器程序AUTOMATE调用Access查询。该流程将每月针对前一个月的数据运行。

我的意图是使用在Access中的模块中声明的两个函数将日期范围放在WHERE子句中,但是我发现将它放在查询条件行上似乎是一个问题。

以下是两个功能:

'##################################################################################
' This function returns the date string for the first day of the prior month.
'##################################################################################

Public Function FirstDayPriorMonth() As Date
    Dim dateEnd   As Date

    dateEnd = DateSerial(Year(Date), Month(Date), 0)
    FirstDayPriorMonth = dateEnd - Day(dateEnd) + 1
End Function

'##################################################################################
' This function returns the date string for the last day of the prior month.
'##################################################################################

Public Function LastDayPriorMonth() As Date
    LastDayPriorMonth = DateSerial(Year(Date), Month(Date), 0)
End Function

来自AUTOMATE的调用使用DoCmd.OpenQuery来调用查询。完整查询的SQL版本如下所示:

SELECT dbo_V_HB_Outsourced_AR.REPORTING_DATE,  
dbo_V_HB_Outsourced_AR.REPORTING_FISCAL_YEAR AS FY,  
dbo_V_HB_Outsourced_AR.LOC_NAME AS FACILITY,  
dbo_V_HB_Outsourced_AR.REVCBO_LEGACY_FINANCIAL_CLASS,  
dbo_V_HB_Outsourced_AR.EPIC_FINANCIAL_CLASS,  
dbo_V_HB_Outsourced_AR.PRIMARY_FIN_CLASS_NAME,  
dbo_V_HB_Outsourced_AR.ACCT_STATUS_NAME,  
dbo_V_HB_Outsourced_AR.OUTSOURCED_FLAG_YN,  
dbo_V_HB_Outsourced_AR.IN_HOUSE_FLAG_YN,  
dbo_V_HB_Outsourced_AR.DNFB,  
dbo_V_HB_Outsourced_AR.[0-30],  
dbo_V_HB_Outsourced_AR.[31-60],  
dbo_V_HB_Outsourced_AR.[61-90],  
dbo_V_HB_Outsourced_AR.[91-120],  
dbo_V_HB_Outsourced_AR.[121-150],  
dbo_V_HB_Outsourced_AR.[151-180],  
dbo_V_HB_Outsourced_AR.[181-210],  
dbo_V_HB_Outsourced_AR.[211-240],  
dbo_V_HB_Outsourced_AR.[241-365],  
dbo_V_HB_Outsourced_AR.[366+],  
dbo_V_HB_Outsourced_AR.[CR BAL],  
dbo_V_HB_Outsourced_AR.[Total (Debit Only)],  
dbo_V_HB_Outsourced_AR.[Over 90],  
dbo_V_HB_Outsourced_AR.COL_AGNCY_NAME
FROM dbo_V_HB_Outsourced_AR  
WHERE (((dbo_V_HB_Outsourced_AR.REPORTING_DATE) Between [FirstDayPriorMonth] And [LastDayPriorMonth])  
AND ((dbo_V_HB_Outsourced_AR.COL_AGNCY_NAME)='ADVICARE'  
Or (dbo_V_HB_Outsourced_AR.COL_AGNCY_NAME)='CONVERGENT'  
Or (dbo_V_HB_Outsourced_AR.COL_AGNCY_NAME)='MEDALIST MANAGEMENT')  
AND ((dbo_V_HB_Outsourced_AR.LOC_ID)=1010  
Or (dbo_V_HB_Outsourced_AR.LOC_ID)=1011  
Or (dbo_V_HB_Outsourced_AR.LOC_ID)=1012  
Or (dbo_V_HB_Outsourced_AR.LOC_ID)=1013  
Or (dbo_V_HB_Outsourced_AR.LOC_ID)=1014));

我的问题是为什么我可以在查询条件行上看到函数调用,但是必须在SQL视图中更改查询?我仍然在努力让函数调用在SQL视图中工作。

2 个答案:

答案 0 :(得分:2)

  

我正在从控制器程序AUTOMATE调用ACCESS查询。

不幸的是,从Access应用程序会话外部运行的查询无法使用VBA用户定义的函数。这意味着您的 FirstDayPriorMonth LastDayPriorMonth 功能在这种情况下不可用。

幸运的是,这些函数都基于相当简单的DateSerial表达式,DateSerial可用于任何Access查询,无论查询是从Access会话内部还是外部运行。

所以不要这样......

Between [FirstDayPriorMonth] And [LastDayPriorMonth]

使用此...

BETWEEN DateSerial(Year(Date()), Month(Date()) -1, 1)
    AND DateSerial(Year(Date()), Month(Date()), 0)

答案 1 :(得分:0)

"<first part of query>
from dbo_V_HB_Outsourced_AR  
WHERE (((dbo_V_HB_Outsourced_AR.REPORTING_DATE) Between #" & FirstDayPriorMonth() & "# and #" & LastDayPriorMonth() & "#" &
"rest of query"

您需要通过连接函数调用的结果来构造您的查询字符串(不应该在方括号中,因为它们不是字段名称)。此外,日期需要在每一侧都有一个“#”侧(在MS-Access中)。