sql - MS Access 查询在查询中返回 0 计数

标签 sql ms-access vba

我在执行 MS Access 2010 查询时遇到困难。我有一个表,大致包含以下字段:ID(自动增量)、ACRES(数字)、体积(数字)、BEGIN_DATE(< em>日期/时间)、CONTRACT_AMT(货币)、COLLECTED_AMT(货币)。

然后,我创建了一个查询表,使用以下表达式计算 BEGIN_DATE 会计年度(7 月 1 日至 6 月 30 日)的字段:

FYBegin: IIf([CalendarMonthBegin] Between 7 And 12,[CalendarYearBegin]+1,
[CalendarYearBegin]) 

我现在需要做的是创建一个查询来计算 ID,并对本财政年度的体积、英亩、契约(Contract)和收集金额进行求和。

我创建了以下 VBA 函数来获取当前会计年度:

Public Function GetCurrentFiscalYear(ByVal CurrentDate As Date)
'Returns the current fiscal year for CurrentFY_Summary query

Dim yearToday As Integer
Dim monthToday As Integer
Dim currentFiscalYear As Integer

yearToday = Year(CurrentDate)
Debug.Print (yearToday)
monthToday = Month(CurrentDate)
Debug.Print (monthToday)

If monthToday > 6 And monthToday < 13 Then
    currentFiscalYear = yearToday + 1
    Debug.Print (currentFiscalYear)
Else
    currentFiscalYear = yearToday
    Debug.Print (currentFiscalYear)
End If
End Function

但是当我尝试使用以下 SQL 创建另一个查询时:

SELECT Count(FiscalYear.ID) AS [Count of Records], Sum(FiscalYear.ACRES) AS 
[Sum Of ACRES], Sum(FiscalYear.VOLUME) AS [Sum Of VOLUME], 
Sum(FiscalYear.CONTRACT_AMT) AS [Sum Of CONTRACT_AMT], 
Sum(FiscalYear.COLLECTED_AMT) AS [Sum Of COLLECTED_AMT]
FROM FiscalYear
WHERE (FiscalYear.FYBegin) = GetCurrentFiscalYear(Now());

我只在计数字段中得到 0,没有其他记录。我可以直观地看到记录符合条件,并且我使用即时窗口来验证 VBA 函数是否返回正确的会计年度并作为整数。 SQL 语句和/或 VBA 函数有什么不正确的地方。

谢谢。

最佳答案

在这种情况下,您的Debug.Print命令欺骗了您。它们打印内部变量,但不打印函数返回值。

您的函数缺少此行:

GetCurrentFiscalYear = currentFiscalYear

否则它将始终返回 NULL。

关于sql - MS Access 查询在查询中返回 0 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43266634/

相关文章:

mysql - 在oracle中查询时创建临时表

mysql - 从结果集中删除 NULL

SQL Server 2008 R2 : Get first not null values from column

ms-access - MS Access 中表单/报告事件的顺序是什么?

Excel宏将特定单元格从行转置到列

excel - 工作表名称与目标工作表名称不匹配时的VBA运行时错误9

html - IE.document.all返回空元素vba

MySQL 不将慢速查询记录到表中

sql 按一列分组,然后过滤一列并为不同的值创建新列

datetime - 如何使用 Ms Access 日期/时间扩展数据类型