2015-08-20 65 views
0

我已經看到了很多其他人提出,並通過谷歌回答了這一點,但有一個額外的問題,別人沒有覆蓋的功能。我的情況是我從控制器程序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 

從自動化調用使用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視圖中工作。

回答

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中)有一個「#」側。

2

我打電話從自動化操作,控制器程序Access查詢。

不幸的是,從Access應用程序會話外部運行的查詢不能使用VBA用戶定義的函數。這意味着您的FirstDayPriorMonthLastDayPriorMonth功能在該情況下不可用。

幸運的是,這兩個函數都基於相當簡單的DateSerial表達式,而且任何Access查詢都可以使用DateSerial,而不管查詢是從Access會話內部還是外部運行。

所以不是這個......

Between [FirstDayPriorMonth] And [LastDayPriorMonth] 

使用此...

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