Calling a function from query criteria - ms-access

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.

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)

"<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).

Related

How to create rolling/moving average in ACCESS QUERY for the last 7 days,30 days Accumulative Avg?

My query name:
Total Daily Orders Summary
My Input [2 columns]:
business_date,# Daily Orders
I need these 3 columns as calculated output:
Orders# (7 days) Average, Orders# (30 Days) Average ,Orders# (Acuum Average) [i.e.from day one to date]
I have tried my best to find clear answers for how to do moving/rolling average in Microsoft Access query but unfortunately, I couldn't make it work for me. Therefore, I have decided to put my request here to see if someone will put me in the right direction to start working on my files and tasks.
I have trouble with correlated subqueries myself. here is a VBA/Access solution that I think is easier.
Start by adding a code module to your Database by clicking create on the menu and selecting module -which is all the way to the right.
Then create a public function like:
Public Function AverageOrders(CurrentDate As Date, NumberofPriorDays As Integer) As Integer
AverageOrders = DSum("DailyOrders", "MyTable", "business_date BETWEEN #" & CurrentDate & "# AND #" & DateAdd("d", -NumberofPriorDays, CurrentDate) & "#")
End Function
we create the function this way so access intellisense works. This is a powerful technique which I also use to wrap any calls to access form parameters. To find the function in a query right click on the top of a query column and choose build. In the build wizard under expression elements select functions then your database. Average Orders and any other public functions you make will appear. Or just type it.
then you get:
February has 28 days. We don't use # for DateOrders because In strings # denotes a date. To get the accumulated average just pick an absurdly early date or make another function without [NumberofPriorDays].

SSRS dataset filter

Hi I'm trying to build a report in ssrs which gives the output based on current weekending date where end day is saturday. I'm pulling my data from SSAS Cube and I have a weekending date field so I applied a filter in the query designer to get the current weekending date using a parameter I used this expression:
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today)
When I'm executing my query, it throws an error saying the restriction imposed by the constrained flag in the STRTOSET function were violated.
Please let me know how can I fix this.
The error is thrown because you are passing a string that doesn't correspond with a valid member in your cube.
Note the Query Designer builds a MDX query based on the dimensions and members you select, this query uses the STRTOSET() function to convert your string parameter in a valid member.
...
STRTOSET(#CurrentWeekendDate,CONSTRAINED)
...
If you pass the current weekend date to your parameter it produces:
STRTOSET('2016-01-10',CONSTRAINED)
As 2016-01-10 is not a valid member in your Date dimension it throws the error.
You have to pass something like this depending on your cube:
[Date].[Date Key].&[2005-01-01T00:00:00]
So in SSRS you have to set your parameter to Text and use this expression:
CORRECT EXPRESSION:
="[200 Date].[Week Ending Date].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-ddThh:mm:ss"
)
& "]"
UPDATE: If [Week Ending date] level doesn't include time:
="[Date].[Date Key].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-dd"
)
& "]"
Go through this tutorial if you get stuck.
Let me know if this helps.

initialize a parameter in MS Access query

In MS Access 2010 I am trying to declare and initialize a variable in a query, then display the contents of that variable. The reason for doing this is to use the parameter in a more complicated query as part of a filter. Please note that for this particular case this task must be done in a query object, not in VBA. Here is the code thus far:
PARAMETERS #Date DATE;
SELECT TOP 1 FORMAT(LastUpdated, "yyyy-mm-dd") AS #Date FROM Table1 GROUP BY FORMAT(LastUpdated, "yyyy-mm-dd") ORDER BY FORMAT(LastUpdated, "yyyy-mm-dd") DESC;
SELECT #Date;
This results in an error message: "Characters found after end of SQL statement."
If this can be modified to work the last line of code will be replaced with the more complex query that needs to use #Date in a filter. The other requirement is that it has to be contained within one query object.
In Access you don't need to use the # to prefix parameters. Parameters are inferred as any column which cannot be resolved. So if your table does not have a date column then using date is sufficient for "declaring" a parameter. However maybe something like p_date can separate it from the data type DATETIME/DATE
I don't know what exactly you are expecting with this
FORMAT(LastUpdated, "yyyy-mm-dd") AS #Date
It seems to be trying to assign an alias to FORMAT(LastUpdated, "yyyy-mm-dd") as the parameter value of #Date?
An Access query cannot return multiple result sets so your SELECT #Date; is ultimately what is causing the "Characters found after end of SQL statement." error message.

MS Access date range functions slowing down query

I have a query in which I am using a between statement to pull data for a date range. I have a VBA function built, that will evaluate the last date (Max Date) in a table (to get the 1st date for the between) and another function to evaluate the stop date (last date of the between)...So my between date statement looks like this: Between hhMaxDte() And hhLastDte()
For some reason having these 2 functions in my between statement takes what seems to be 8x longer to run the query than if I just manually put the actual dates in myself. Anyone out there know why this is and how I can dynamically do a similar process, but take the same amount of time to run the query, as when I manually enter the dates? My functions code is below:
Function hhMaxDte()
If DMax("row_date", "HH_CIB_Raw_Data") + 1 = Date Then
hhMaxDte = 0
Else
hhMaxDte = DMax("row_date", "HH_CIB_Raw_Data") + 1
End If
End Function
Function hhLastDte()
If DMax("row_date", "HH_CIB_Raw_Data") + 1 = Date Then
hhLastDte = 0
Else
hhLastDte = Date - 1
End If
End Function
I've figured out the answer based off of the feedback in the comments above. The function was running for each row of data, thus significantly slowing down the return of the results. In order to avoid this, I used the built in function Dmax (to analyze the max indexed date in a table) and Date to create a between statement in the Access query criteria section:
Between DMax("row_date","HH_CIB_Raw_Data")+0 And Date()-1
Now the append query is able to dynamically pull data based off of the recent dates missing in the table and runs in a matter of seconds VS 2-3 min.
Thanks for all the input guys!!

Date calculations - how to use today's date if date not specified

I have a question regarding Microsoft Access 2010. I'm working on an inherited complaints database and I've been asked to produce a report outlining how long complaints have/ or were active.
Using the following I'm able to calculate the difference between when a complaint was opened and when it was closed and show the number of days active.
DaysActive: DateDiff("d",[COMPLAINTS]![DateRcvd],[COMPLAINTS]![DateClosed])
My issue is when a complaint hasn't been closed I don't get a value returned. Is there a way to modify the expression so that if the DateClosed is empty it will use the current date instead?
Assuming this is a query expression, the db engine supports the Date() function which returns today's date. So you can use an IIf expression to give you Date() when DateClosed is Null, or otherwise DateClosed
DateDiff("d", COMPLAINTS.DateRcvd, IIf(COMPLAINTS.DateClosed Is Null, Date(), COMPLAINTS.DateClosed))
If the query will always be run from within an Access session, you can use Nz instead of IIf ...
DateDiff("d", COMPLAINTS.DateRcvd, Nz(COMPLAINTS.DateClosed, Date()))
Note that Nz is a VBA function and IIf is supported directly by the db engine, so IIf should theoretically be faster . But the difference may not be perceptible in your context.
Try using this.
DaysActive: DateDiff("d",Nz(COMPLAINTS.DateRcvd, Date()),[COMPLAINTS]![DateClosed])
Nz will check if the date is available or not, if not , it will replace today's date for that, using DATE() function.