Time Sensitive Calculation Question

R

Rose

Currently I have a query that states Nights:[CheckOutDate]-[CheckInDate].
The problem is that I am putting a parameter of Between two dates in the
checkin field.

Nights: IIf(DateDiff("d",[CheckInDate],IIf([CheckOutDate] Is
Null,Date(),[CheckOutDate]))=0,1,DateDiff("d",[CheckInDate],IIf([CheckOutDate] Is Null,Date(),[CheckOutDate])))

My challenge is if I input Between 09/01/2006 and 09/30/2006. How can I
only count the days in September and not those dates that may have happend
prior for example if people checked in on August 28 and Check out on
September 3. I would only want the query to count the two nights in
September. Also if they
checked in on September 29 and Checked Out on October 4th I would only want
the days in September. Any Ideas.

Check In Date Check Out Date Nights
09/01/2006 09/05/2006 4
09/10/2006 09/11/2006 1
09/12/2006
 
A

Allen Browne

Rose, the simplest way to do this will be to create a table of dates, and
then use it in a query to give you a record for each night of the booking.
You can then turn it into a Totals query to group by the month, and give the
count of nights booked for each month.

1. Create a table with one date/time field named (say) TheDate.
Mark the field as primary key.
Save the table as tblDate.
Enter records for all the dates you could need.
(Use the function below to enter dates programmatically if you wish.

2. Create a query using both tblDate and your existing table (called Table1
in the example below.)
There must be no line joining the tables in the upper pane of query design.
This is called a Cartesian Product, and gives you every combination.
Drag tblDate.TheDate into the grid, and enter this criteria under it:
Between Table1.CheckInDate And Nz(Table1.CheckOutDate, Date())
This gives you a record for each day in the range.

3. Depress the Total icon on the toolbar (upper sigma icon).
Access adds a Total row to the grid.
In the Total row under tblDate.TheDate, choose Where.
If you want to limit results to a particular month, add tblDate.TheDate
again, choose Where, and enter your limiting dates as criteria.

4. Enter this expression into a fresh column in the Field row:
TheYear: Year(tblDate.TheDate)
Accept Group By in the Total row.

5. Enter this expression into a fresh column in the Field row:
TheMonth: Month(tblDate.TheDate)
Accept Group By in the Total row.

6. Drag the primary key from Table1 into the grid.
In the Total row, under this field choose Count.

The query output now shows the number of nights booked for each month.

Here's the function that can populate the dates for you.
To add all dates between 1/1/2007 and 12/31/2010:
a) Press Ctrl+G to open the Immediate window.
b) Enter:
? MakeDates(#1/1/2007#, #12/31/2010#)


Function MakeDates(dtStart As Date, dtEnd As Date) As Long
Dim dt As Date
Dim rs As DAO.Recordset

Set rs = DBEngine(0)(0).OpenRecordset("tblDate")
With rs
For dt = dtStart To dtEnd
.AddNew
!TheDate = dt
.Update
Next
End With
rs.Close
Set rs = Nothing
End Function


--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Rose said:
Currently I have a query that states Nights:[CheckOutDate]-[CheckInDate].
The problem is that I am putting a parameter of Between two dates in the
checkin field.

Nights: IIf(DateDiff("d",[CheckInDate],IIf([CheckOutDate] Is
Null,Date(),[CheckOutDate]))=0,1,DateDiff("d",[CheckInDate],IIf([CheckOutDate]
Is Null,Date(),[CheckOutDate])))

My challenge is if I input Between 09/01/2006 and 09/30/2006. How can I
only count the days in September and not those dates that may have happend
prior for example if people checked in on August 28 and Check out on
September 3. I would only want the query to count the two nights in
September. Also if they
checked in on September 29 and Checked Out on October 4th I would only
want
the days in September. Any Ideas.

Check In Date Check Out Date Nights
09/01/2006 09/05/2006 4
09/10/2006 09/11/2006 1
09/12/2006
 
A

Allen Browne

So if someone checks in on 1/31/07, and checks out on 2/1/07, that counts
only as the night of Jan 31?

If so, try subtracting 1 from the check out date:
Between Table1.CheckInDate And Nz(Table1.CheckOutDate - 1, Date())

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Rose said:
Hi Allen,

Your suggestions are good and give me the correct format but not the
numbers
are come back higher, I believe the calculation is counting the times it
sees
the dates and not taking into account that it should be calculating the
nights. I.e. CheckOutDate-CheckInDate. Any suggestions.
--
Rose

--
Rose


Allen Browne said:
Rose, the simplest way to do this will be to create a table of dates, and
then use it in a query to give you a record for each night of the
booking.
You can then turn it into a Totals query to group by the month, and give
the
count of nights booked for each month.

1. Create a table with one date/time field named (say) TheDate.
Mark the field as primary key.
Save the table as tblDate.
Enter records for all the dates you could need.
(Use the function below to enter dates programmatically if you wish.

2. Create a query using both tblDate and your existing table (called
Table1
in the example below.)
There must be no line joining the tables in the upper pane of query
design.
This is called a Cartesian Product, and gives you every combination.
Drag tblDate.TheDate into the grid, and enter this criteria under it:
Between Table1.CheckInDate And Nz(Table1.CheckOutDate, Date())
This gives you a record for each day in the range.

3. Depress the Total icon on the toolbar (upper sigma icon).
Access adds a Total row to the grid.
In the Total row under tblDate.TheDate, choose Where.
If you want to limit results to a particular month, add tblDate.TheDate
again, choose Where, and enter your limiting dates as criteria.

4. Enter this expression into a fresh column in the Field row:
TheYear: Year(tblDate.TheDate)
Accept Group By in the Total row.

5. Enter this expression into a fresh column in the Field row:
TheMonth: Month(tblDate.TheDate)
Accept Group By in the Total row.

6. Drag the primary key from Table1 into the grid.
In the Total row, under this field choose Count.

The query output now shows the number of nights booked for each month.

Here's the function that can populate the dates for you.
To add all dates between 1/1/2007 and 12/31/2010:
a) Press Ctrl+G to open the Immediate window.
b) Enter:
? MakeDates(#1/1/2007#, #12/31/2010#)


Function MakeDates(dtStart As Date, dtEnd As Date) As Long
Dim dt As Date
Dim rs As DAO.Recordset

Set rs = DBEngine(0)(0).OpenRecordset("tblDate")
With rs
For dt = dtStart To dtEnd
.AddNew
!TheDate = dt
.Update
Next
End With
rs.Close
Set rs = Nothing
End Function


--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Rose said:
Currently I have a query that states
Nights:[CheckOutDate]-[CheckInDate].
The problem is that I am putting a parameter of Between two dates in
the
checkin field.

Nights: IIf(DateDiff("d",[CheckInDate],IIf([CheckOutDate] Is
Null,Date(),[CheckOutDate]))=0,1,DateDiff("d",[CheckInDate],IIf([CheckOutDate]
Is Null,Date(),[CheckOutDate])))

My challenge is if I input Between 09/01/2006 and 09/30/2006. How can
I
only count the days in September and not those dates that may have
happend
prior for example if people checked in on August 28 and Check out on
September 3. I would only want the query to count the two nights in
September. Also if they
checked in on September 29 and Checked Out on October 4th I would only
want
the days in September. Any Ideas.

Check In Date Check Out Date Nights
09/01/2006 09/05/2006 4
09/10/2006 09/11/2006 1
09/12/2006
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top