Link to home
Start Free TrialLog in
Avatar of timamartin
timamartinFlag for United States of America

asked on

Display report with records between two dates

I have a report that has numerous records each displayed on a single line. I would like to ask the user to enter two dates and the report then show records between those dates.

Here are the mechanics of how I would like it to work...
BUTTON (open report)
ASK (start date/end date)
DISPLAY (Report filtered by start and end date)

Any suggestions on which method to use would be appreciated.
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

private sub btnOpenReport_click()
dim sDate as date, eDate as date

sdate=nz(Inputbox("Enter Start Date"),Date())
eDate=nz(Inputbox("Enter End Date"),Date())

docmd.openreport "reportName",acviewpreview,,"[DateField] Between #"& sDate & "# and #" & edate & "#"


end sub
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of timamartin

ASKER

I lose the date picker that way...I don't think people will like that.
what date picker are you using?
I would concur with the 2nd half of Capricorn1's 2nd comment.  Use textbox controls on your form to enter the start and end dates, then in the command button Click event, check to make sure that the dates are valid, or build a criteria string something like:


private sub btnOpenReport_click()

    dim strCriteria as string

    if me.txtStartDate & ""="" AND Me.txtEndDate & ""="" then
        strCriteria = ""
    elseif me.txtStartDate & ""="" AND Me.txtEndDate & ""<>"" then
        strCriteria = "[DateField] <= #" & me.txtEndDate & "#"
    elseif me.txtStartDate & ""<>"" AND Me.txtEndDate & ""="" then
        strCriteria = "[DateField] >= #" & me.txtStartDate & "#"
    else
        strCriteria = "[DateField] BETWEEN #" & me.txtStartDate & "# AND #" & me.txtEndDate & "#"
    endif

    docmd.openreport "reportName",acviewpreview,,strCriteria

end sub

Open in new window

The way I have been trying to do this is by having a form open with two fields each with a date picker. The user enters two dates and clicks the button then...the report is opened and only records falling between the two dates will populate. The reason I want to use the form is that I want to add other filtering options at a later date. I thought I would be able to pass the two dates as a where condition to filter the report. Is this possible?
<I thought I would be able to pass the two dates as a where condition to filter the report. Is this possible?>

YES, the codes was posted above..

did you try it?

1. post the name of your textboxes that you use for entring the dates.

2. post the record source of your report

3. what is the name of the date field?
I did try your code and it worked fine. Just curious why text boxes are better? I was trying to use a form so I could use the built in date picker when entering the dates.
<I was trying to use a form so I could use the built in date picker when entering the dates.>
you can still use the date picker with the text boxes

when you pick a date from the date picker, it will be displayed in the textbox..
I don't know what version of Access you are using, but with 2007, textboxes have a property (on the Format tab) which allows you to select: Never or  For Dates

If you select the For Dates option, then the date picker will be available.
you also need to set the format of the UNBOUND textbox to Short Date