Use a form to run queries on a date range - ms-access

I have an MS-Access DB that pulls information in from an Excel file with 6 worksheets, so I get 6 tables. I created 7 queries, all of which make tables from the excel data, 6 of those 7 do a count of an item ie:
SELECT DISTINCT DISPLAYNAME, COUNT(DISPLAYNAME)
FROM [TXFR REC]
GROUP BY DISPLAYNAME
The 7th query pulls those 6 tables together into a sort of report table by using left joins onto one of the tables, where the connection to all the others is a DISPLAYNAME.
I have created a Form with 4 buttons and two date boxes, a start date and an end date. What I want to do is the following:
Choose Start Date
Choose End Date
Press Button that runs the 7 queries in the order I specified by invoking the date range from steps 1 and 2 ** THIS IS WHERE I AM STUCK: I do not know how to force this date range on the queries **
Run the report
Export the report
Close the form
The date boxes are set to General Date and the method used for them is GetDates so you just click in the box and the calendar pops up and you choose your date.
I did see this post here but am not following to well:
Date Range Form
Here is the text from a test query as suggested by a user and in the format suggested by the answering person:
SELECT [NO ADMITTING DX (HEALTH ISSUE)].PHYSICIANDISPLAYNAME, Count([NO ADMITTING DX (HEALTH ISSUE)].PHYSICIANDISPLAYNAME) AS CountOfPHYSICIANDISPLAYNAME INTO NO_ADMIT_DX_COUNT
FROM [NO ADMITTING DX (HEALTH ISSUE)]
WHERE ((([NO ADMITTING DX (HEALTH ISSUE)].PHYSICIANDISPLAYNAME) Is Not Null) And (([NO ADMITTING DX (HEALTH ISSUE)].AdmitDtm) Between Forms!PRINT_REPORT![START DATE] And FORMS!PRINT_REPORT![END DATE]))
GROUP BY [NO ADMITTING DX (HEALTH ISSUE)].PHYSICIANDISPLAYNAME;
** This query now works properly **
Thank you for all the help.

For your query, use something like this.
SELECT DISPLAYNAME, COUNT(DISPLAYNAME)
FROM [TXFR REC]
WHERE [TXFR REC].yourDate BETWEEN [Forms]![yourFormName]![yourStartDate]
AND [Forms]![yourFormName]![yourEndDateField]
GROUP BY DISPLAYNAME
All of your queries that you want to rely on a date range should have these parameters. The advantage to doing it this way is that you can give your two date fields on your form a datePicker. It would still work the same if you did something like this:
SELECT DISPLAYNAME, COUNT(DISPLAYNAME)
FROM [TXFR REC]
WHERE [TXFR REC].yourDate BETWEEN [Please Enter a Start Date]
AND [Please Enter an Ending Date]
GROUP BY DISPLAYNAME
It's all preference though. Just throw a button on there and go to Misc in the wizard and choose run query if you want.

Edit your queries to rely on the value of the form in the where clause:
[Forms]![form's name]![form's control]
Here's a Screenshot example
This will make your queries only work with the form, however. You might consider copying your queries and renaming and editing the copies to reflect its limitation.

Related

MS Access - check-in/check-out system

I have an issue writing a query in MS Access that would pull information from two tables.
Quick description:
Employees start the job and click the button in Excel macro. The macro saves the date and a comment that the employee provides (not required). Next, the macro pushes the information into a table in Access database which gathers all 'check-ins' of all employees. There's a possibility for a person to check-in twice by accident. This is an example how the table would look like after two work days, in a company with three employees:
check-in table example
Similarly, the macro lets a user to check-out, also with optional comment:
check-out table example
What I need is a query that would consolidate two of these tables and show check-in/check-out of employees for a previous day, together with provided comment. So the result of the query would look like this:
outcome of a table for previous day
Column order is absolutely irrelevant here
SELECT min(checkin.[check-in time]), max(checkout.[check-out time]), checkin.[comment] AS [check-in comment], checkout.[comment] AS [check-out comment]
FROM checkin
RIGHT JOIN chekout ON checkin.analyst = checkout.analyst
WHERE checkin[check-in time] = Date()-3;
Bad thing is, MS Access throws:
Syntax error in JOIN operation
Could anyone help me out and advise how should I proceed? Unfortunately I have no control how the system was implemented (Macro in Excel connected with Access) and sadly I have not much experience in MS Access.
EDIT:
Thank you very much #Erik, much appreciated. It helped a lot and I'll be more diligent next time when it comes to typos. The new problem presented itself however. It is possible for a person to check-in, without checking-out and vice versa. Lets say there's another row in the first table -> Meg015 checked in at 7:20 on 08.06.2018 but forgot to check out:
see an example
I would like MS Access to present this information accordingly (without forgotten check-out time):
How it should look like
Since there's no Full Outer Join in MS Access, I was thinking about following UNION/LEFT and RIGHT JOINs:
SELECT checkin.analyst, min(checkin.[check-in time]), max(checkout.[check-out time]), checkin.comment, checkout.comment
FROM checkin RIGHT JOIN checkout ON (checkin.analyst = checkout.analyst OR checkout.analyst = NULL OR checkin.analyst = NULL)
WHERE (format(checkin.[check-in time], "Short Date") = format(checkout.[check-out time], "Short Date") OR checkout.[check-out time] = NULL) AND format(checkin.[check-in time], "Short Date") = date()-3
GROUP BY checkin.analyst, checkin.comment, checkout.comment
UNION
SELECT checkout.analyst, min(checkin.[check-in time]), max(checkout.[check-out time]), checkin.comment, checkout.comment
FROM checkin LEFT JOIN checkout ON (checkin.analyst = checkout.analyst OR checkin.analyst = NULL OR checkout.analyst = NULL)
WHERE (format(checkin.[check-in time], "Short Date") = format(checkout.[check-out time], "Short Date") OR checkin.[check-in time] = NULL) AND format(checkin.[check-in time], "Short Date") = date()-3
GROUP BY checkout.analyst, checkin.comment, checkout.comment;
Sadly, Meg's check-in details are not visible in a result of the query above. Any idea how to resolve this issue?
You're missing a dot, have misspelt checkout in your join, and are using aggregates without grouping by the other columns.
Also, you're testing if a date is equal to a date with time, which will pretty much always be false.
Anyway, this is how your query should look like:
SELECT checkout.analyst, min(checkin.[check-in time]), max(checkout.[check-out time]), checkin.[comment] AS [check-in comment], checkout.[comment] AS [check-out comment]
FROM checkin
RIGHT JOIN checkout ON checkin.analyst = checkout.analyst
WHERE DATEDIFF('d',checkin.[check-in time], Date()) = 3
GROUP BY checkout.analyst, checkout.[comment], checkin.[comment]
(Do note that questions with this many errors can get closed as too broad).

MS Access 2010 input date once for multiple sub reports

I've been banging my head against this for weeks, have Googled every permutation of the question that i can think of, and have still got nowhere, so any help would be really appreciated.
WHAT I NEED:
I need to generate a report, which pulls summaries of our referrals from the database. I have two reports which use the following queries as their Record Source:
SELECT referrals.origin_country, Count(*) AS ['number']
FROM referrals
WHERE (((referrals.referral_date) Between [Enter Start Date] And [Enter End Date:]))
GROUP BY referrals.origin_country;
And
SELECT referrals.first_language, Count(*) AS ['number']
FROM referrals
WHERE (((referrals.referral_date) Between [Enter Start Date:] And [Enter End Date:]))
GROUP BY referrals.first_language;
The queries are nearly identical, and the date range is the same for each one.
The issues is that when I generate a report which uses these two reports as subreports, I then have to enter the date range for the subreports twice (once for the Country of Origin Subreport, and once for the First Language Subreport.
My Access skills are not as advanced as I would like, and I'm wondering if anybody can tell me how to ensure that the user only has to enter the date range once?
I've tried
Using VBA to create a variable onLoad, but then couldn't work out how to use this variable in the Record Source Query...
Using VBA to create a function which sets a variable onLoad, but then couldn't work out how to use the function in my Record Source query...
Any suggestions would be greatly appreciated!
Create a form with fields for the start and end date, and a button on it, which launches your main form. Use the 'Embedded Macro' Wizard to open the chosen Report On Click.
Then, use [Forms]![FORM NAME]![FIELD NAME] in the query to access the date, for example:
SELECT referrals.origin_country, Count(*) AS ['number']
FROM referrals
WHERE (((referrals.referral_date) Between [Forms]![EAL Referral Search Form]![dat_termly_report_start] And [Forms]![EAL Referral Search Form]![dat_termly_report_end]))
GROUP BY referrals.origin_country;

Check to see if one year has passed

I run a web design firm, and we also sell hosting on a yearly basis to our customers. I'm writing a Ruby script to read all of the customers from a MySQL database and check to see if a year has passed. If it has (or is at least close to expiring), I want it to send an Email to them.
I have the Emailing part down, but how would I check to see if one year has passed using the Time class, or another solution based in SQL?
(looking back with this edit, it seems as though I don't quite know the SQL needed, so any help on that is appreciated)
If you really want to solve this issue using ruby you can use >> when dealing with a Date to increment it by N months (such as 11, which will give your customers some time to deal with the payment).
When you've got this incremented date you should compare it to what the current date is to see if it's less or equal (ie. the date is in the past), if so; send out your email.
>> (Date.strptime('2012-07-07') >> 11).to_s
=> "2013-06-07"
>> if (Date.strptime('2011-06-15') >> 11) < DateTime.now
>> print "More than 11 months has passed!"
>> end
More than 11 months has passed!=> nil
Though, you are much better of solving this issue directly in the SQL query, which would boil down to something as the below:
SELECT field1, field2, ..., fieldN
FROM `customers_table`
WHERE ADDDATE(last_payment, INTERVAL 11 MONTHS) <= NOW ()

Calculating days between dates and parameters in MS Access

I'm trying to write a query that allows me to get records for all users whose items are more than X number of days overdue. I want to be able to specify whether that number be 30 or 40 or 50. I am able to get the results I need when I specify a specific number in the sql query:
SELECT USER_TRANSACTIONS.PATRON_ID, USER_TRANSACTIONS.CURRENT_DUE_DATE,
DateDiff("d",USER_TRANSACTIONS.CURRENT_DUE_DATE,Date()) AS Expr1
FROM USER_TRANSACTIONS
WHERE (DateDiff("d", USER_TRANSACTIONS.CURRENT_DUE_DATE, Date()))>50
However, when I try to use a parameter in place of the number, 50. And then type in the number 50 when I run the query and am prompted
SELECT USER_TRANSACTIONS.PATRON_ID,USER_TRANSACTIONS.CURRENT_DUE_DATE,
DateDiff("d",USER_TRANSACTIONS.CURRENT_DUE_DATE,Date()) AS Expr1
FROM USER_TRANSACTIONS
WHERE (DateDiff("d", USER_TRANSACTIONS.CURRENT_DUE_DATE, Date()))>[MinimumNumDays]
The query gives me everything instead of limiting the records to those overdue by more than 50 days. What am I missing??
You will need to add [MinimumNumDays] as an Integer query parameter. (Click [?] Parameters on the upper right the query builder)
if you are running this query from a form you can add an unbouand text box to the form. Enter the number of days in this. In query definition right click on in the criteria row for Expr1 and select build, locate your text box in
forms -> Loaded Forms -> your form
double click on it and click OK

Access Report - Show Week Ending Date

I have an Access 2000 report based on a query like this
SELECT
...
FROM Clients AS wc INNER JOIN ...
WHERE ((wo.OfferStatusID)=3) AND
((DatePart("ww",[wo.StatusTimeStamp]))=DatePart("ww",[Enter Week End Date]))
AND ((Year([wo.StatusTimeStamp]))=Year(Date())));
The where clause allows you to enter the 'Week End Date' and it finds all of the records for the Sunday-Saturday week that contains the end date.
My problem is I need to include the Saturday end date on the report so that the header reads '... for week ending 5/9/09' and I can't figure out how to get that date without the query asking me for it a second time.
Is there a way to force Access to return the parameter entered as another field in the results, or another way to get that week ending date?
Continuing to poke around in the query designer I discovered that I could add this to the SELECT clause and get the value entered added to each row:
[Enter Week End Date] AS WeekEndDate
This works, but I am still open to other suggestions.
You could stack two queries (make one the source of the other). This would be pretty MS Access'y. However, if you have it working now, I'd stick with what you have. It's probably cleaner.
Another approach is to use a form to grab the query params first, and reference the form control in the query. For instance, with your example, create a form called frmGetDateParam, and add a textbox called txtDate. Change the format to a date, perhaps add some validation, doesn't matter. Add a command button to the form which opens up the report. Then, change your query to look like this :-
SELECT
...
FROM Clients AS wc INNER JOIN ...
WHERE ((wo.OfferStatusID)=3) AND
((DatePart("ww",[wo.StatusTimeStamp]))=DatePart("ww",Forms!frmGetDateParam!txtDate))
AND ((Year([Forms!frmGetDateParam!txtDate]))=Year(Date())));
You can also reference Forms!frmGetDateParam!txtDate as a field on your report.
The only downside to this approach is that if you try to open the query/report without the parameter form being open, then the query will still prompt you for the date value.