You can use the criteria…
Between [Enter Start Date:] And [Enter End Date:]
This will prompt them for both dates, and report only the data between them..
Is this what you’re looking for?
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Resolved (Again!): Modify Query (97 SR2)
How is the criteria for the date hard-coded?
In your query design, just below the field designation, there’s an area for criteria. if you place the above in that field, it’ll prompt them for the criteria…
You might give it a try, if you’re asking for a programattical solution, you’re more of a wizard at that than I…
Hi Kevin,
Sorry – I was in a bit of a rush when I posted that. I should have added a solution really! If you run the report from a form, you can add two text boxes to the form for your criteria (with default values equal to your required defaults) and then add =Forms!formnamehere!textboxnamehere as the criteria in your query.
If you can’t run it from a form, repost here and we can come up with some code to do it.
Hope that helps.
50,000,000 Rory fans can’t be wrong!
Kevin, I know that you’re considering this resolved, but I’m just getting round to checking the threads from the past couple of days. Here’s my : the source for the report is a query, which is just a SQL statement. It sounds like the SELECT and FROM clauses of your SQL statement will always be the same, and the WHERE clause should default to your pre-specified criteria, but if the user wants to have other criteria, the WHERE clause should change accordingly. Sooo, perhaps when the report is launched, you ask the user to specify a date range, or just press . Behind the scenes, you build up your SQL statement in VBA setting the WHERE clause to your default if the user didn’t enter something, or to their date range if they did. Then you can set the record source of the report to that SQL statement.
Sound like something that you’d be interested in?
Shane,
>>Sound like something that you’d be interested in?
Quite frankly, YES! That’s the idea I was hoping to explore but I don’t know the Access objects well enough to modify the query’s where clause. And since Rory’s “criteria prompts” worked just fine, I figured it best to drop the subject. But…
If you wouldn’t mind pointing me in the right direction I would be unhesitatingly grateful.
See if this does the trick for you. You might consider a different mechanism for capturing user input, and you’d certainly want more robust error handling , but this should give you a push in the right direction for dynamically generating SQL statements to use as record sources for your reports.
I backsaved to Access 97, but haven’t had a chance to road test it in that version; works in Access 2000, SR2. Both versions are in the zip. HTH!
Shane,
Your work looks good. I’m still adapting your insights. The key piece I was ignorant of is me.recordsource = strQry.
You can tell I don’t hang out in the Access world. Anyway, that sets the query and I guess runs it too, right?
I noticed your query statement includes the brackets for the row fields as well as the table name:
>>SELECT [tbl_TestDates].[ID], [tbl_TestDates].[DateIn], [tbl_TestDates].[DateOut]”
The Access generated query that I wrote only puts the brackets on the table name:
“SELECT [Purchase Order Table].PurchaseOrderNumber, [Purchase Order Table].PurchaseOrderType, ”
Is there some mystery to this difference in syntax?
Great! I hope it works out for you.
You’re spot on regarding the record source: when the report opens it checks its record source, notices that it’s a query, executes the query, then displays the data.
As far as the syntax goes – . Maybe a syntactical change between 97 and 2000? At any rate, it appears that the query runs just fine with no brackets, brackets only around the table name, and brackets around both the table name and the field name. Anybody else have any ideas on this one?
[EDITED by Charlotte to cure horizontal scrolling
Shane,
I posted a revelation of how dumb I am and then quickly deleted it. Hope you didn’t see it…
Here’s the result of this should-have-been-simple-exercise-but-was-a-learning-experience-none-the-same adventure.
I’ve added some decent error code and figured out how to abort the report if the selected date range has no records.
Thanks for the push. You and Roy Rogers are right up there on my list of good guys.
Private Sub Report_Open(Cancel As Integer) Dim strStart As String Dim strEnd As String Dim strFiscalYear As String Dim strQry As String strQry = "SELECT [Purchase Order Table].PurchaseOrderNumber, " _ & "[Purchase Order Table].PurchaseOrderType, " strQry = strQry & "[Purchase Order Table].PurchaseOrderDate, " _ & "[Vendor Table].VendorName, [Purchase Order Detail Table].ExtendPrice " strQry = strQry & "FROM ([Vendor Table] INNER JOIN [Purchase Order Table] " _ & "ON [Vendor Table].VendorID = [Purchase Order Table].VendorID) " strQry = strQry & "INNER JOIN [Purchase Order Detail Table] ON " _ & "[Purchase Order Table].RecordID = [Purchase Order Detail Table].RecordID " strStart = IIf(Month(Now) < 7, CStr(Year(Now) - 1), CStr(Year(Now))) strEnd = IIf(Month(Now) < 7, CStr(Year(Now)), CStr(Year(Now) + 1)) strFiscalYear = strStart & "/" & strEnd strStart = "07/01/" & strStart strEnd = "06/30/" & strEnd Do While True strStart = InputBox(Prompt:="Enter Start Date (mm/dd/yyyy): ", _ Title:="Current Fiscal Year = " & strFiscalYear, Default:=strStart) If IsDate(strStart) Then Exit Do End If Loop Do While True strEnd = InputBox(Prompt:="Enter End Date (mm/dd/yyyy): ", _ Title:="Current Fiscal Year = " & strFiscalYear, Default:=strEnd) If IsDate(strEnd) Then Exit Do End If Loop strQry = strQry & "WHERE ((([Purchase Order Table].PurchaseOrderDate) " _ & "Between #" & strStart & "# And #" & strEnd & "# )) " strQry = strQry & "ORDER BY [Purchase Order Table].PurchaseOrderNumber;" Me.RecordSource = strQry If Me.HasData Then 'Run the report Else MsgBox "There are no records to print in that date range. " _ & "Sorry, try again", vbCritical, "No records" Cancel = True End If End Sub
Thank you Char. The lazy are often compensated for…
BTW, I spoke too soon about the me.hasdata property. Does not seem to do what the help makes it appear to seem to do.
Charlotte, chime in here, please. I know there are records for a given date range. If I remove the Hasdata test, the report runs just fine. But not matter what date range I enter, HasData always comes back 0 (zero).
Something fires the query, but it’s not simply setting the recordsource property. When the open event ends, Access fires the query before displaying the report. The question is “How does one fire the query before the report is displayed so as to test whether there are any records in the recordset?
All this is so familiar using ADO, but the Jet database is a mystery to me…
Answer is, “you don’t”. What you do instead is use the NoData event of the report, which fires if there are no records in the recordset. Does that help?
I doubt that you can use the HasData property on the report itself. it’s intended to be used for subreports from the parent report and it is only available while printing or while in Print Preview.
Here’s a suggestion, though. put a break point in your code on the line that sets the recordsource. Then when it breaks, type the SQL string into the immediate window and look it over. Then copy the SQL and paste it into a new query and try to run it. That will show you exactly what is being returned by the recordset and may give you a clue as to what is going wrong.
As soon as I’m back in the office on Monday, I’ll try th OnNoData event in the report.
I’ve done the breakpoint test and when the me.recordset = strQry is executed, the immediate window reports (the contents of me.recordset as the query I just set it to. The query does not fire until after the open event ends. That must be the case.
So, I’ll try the OnNoData event which must fire after the Open but before the display of the report. That should do it. Thanks much!
I’ve written a query that contains default date criteria that I want the user to be able to override when they run th report that attaches to this query.
In the open event of the report I prompt for a start date and and an end date. Now what?
How do I modify the query so that the report uses their dates and not the defaults?
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications