0

I have viewed a lot of what others have asked and answered for this through Google, but have an additional question others haven't covered. My situation is that I am calling an Access query from AUTOMATE, a controller program. The process will run monthly against the data of the preceding month.

My intent is to use two functions declared in a module within Access to put the date range in the WHERE clause, but am finding putting it in on the query criteria line seems to be a problem.

Here are the two functions:

'##################################################################################
' 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

The call from AUTOMATE uses DoCmd.OpenQuery to call the query. The SQL version of the full query looks like this:

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));

My question is why can I see the function calls on the query criteria line, but have to change the query in the SQL view? I am still working on getting the function calls to work in the SQL view as well.

2 Answers 2

2

I am calling an ACCESS query from AUTOMATE, a controller program.

Unfortunately, a query run from outside an Access application session can not use VBA user-defined functions. That means your FirstDayPriorMonth and LastDayPriorMonth functions are not available in that situation.

Fortunately both those functions are based on fairly simple DateSerial expressions, and DateSerial is usable in any Access query regardless of whether the query is run from within or outside an Access session.

So instead of this ...

Between [FirstDayPriorMonth] And [LastDayPriorMonth]

Use this ...

BETWEEN DateSerial(Year(Date()), Month(Date()) -1, 1)
    AND DateSerial(Year(Date()), Month(Date()), 0)
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"

You need to construct your query string by concatenating the results of the function calls (which should not be in square brackets as they are not field names). Also, dates need a "#" side on each side (in MS-Access).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.