Solved

How to find a count of each day between two dates

Posted on 2014-11-20
8
195 Views
Last Modified: 2014-11-20
I am trying to get a count of hotel nights for an upcoming meeting,  I have a table with each persons "Arrival Date" and "Departure Date" and a query with those 2 dates and the "Number of Total Nights" (used DateDiff formula).  How do I find the number of rooms I need for each night?

Ex:
Arrival Date      Departure Date      Number of Nights
3/9/2014         3/14/2014                          5
3/9/2014         3/13/2014                         4
3/10/2014         3/11/2014                         1

Number of Rooms needed for each night:  (This is the part I cant figure out how to do???)
                  
3/9/2014      3/10/2014      3/11/2014      3/12/2014      3/13/2014
2                      3                      2                      2                      1
0
Comment
Question by:snoopyswimer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 40455314
I generally start by creating a table (tbl_Numbers) which contains 1 field (lng_Number) and 10 records, the values 0-9.

I then create a query (qry_Numbers) that generates the numbers 0-99:
SELECT Tens.lng_Number * 10 + Ones.lng_Number as lng_Number
FROM tbl_Numbers as Tens, tbl_Numbers as Ones.

With this query, I can generate a list of 100 consecutive days:

SELECT DateAdd("d", lng_Number, [StartDate]) as MyDate
FROM qry_Numbers
WHERE DateAdd("d", lng_Number, [StartDate]) < [EndDate]

Using this as a subquery, you can include your table and generate the count of records per day.

SELECT sq.MyDate, SUM(iif(T.ArrivalDate <= sq.MyDate AND T.DepartureDate > sq.MyDate, 1, 0)) as RoomCount
FROM (
SELECT DateAdd("d", lng_Number, [StartDate]) as MyDate
FROM qry_Numbers
WHERE DateAdd("d", lng_Number, [StartDate]) < [EndDate]
) as sq, YourTable as T
GROUP BY sq.MyDate

You could refine that further so that you are not comparing the dates in the MyDate query agains all dates in your other table by filtering the other table first.  Something like:

SELECT sq.MyDate, SUM(iif(AD.ArrivalDate <= sq.MyDate AND AD.DepartureDate > sq.MyDate, 1, 0)) as RoomCount
FROM (
SELECT DateAdd("d", lng_Number, [StartDate]) as MyDate
FROM qry_Numbers
WHERE DateAdd("d", lng_Number, [StartDate]) < [EndDate]
) as sq, (
SELECT * FROM YourTable
WHERE YourTable.ArrivalDate <=[EndDate]
AND yourTable.DepartureDate >= [StartDate]
) as AD
GROUP BY sq.MyDate
0
 

Author Comment

by:snoopyswimer
ID: 40455559
I guess I should have mentioned that I am not great with coding. ;-)

I am going to try and mess with what you gave me and see if I can make it work.
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40455707
Each person has a room to themselves?
Code on a form
Option Compare Database
Option Explicit

Private Sub txtDate_AfterUpdate()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Select PersonID from tblStays where arrival <= #" & Me.txtDate & "# and departure >= #" & Me.txtDate & "#;", dbOpenDynaset, dbSeeChanges)
If rs.RecordCount = 0 Then
    Me.txtRooms = 0
Else
    rs.MoveLast
    Me.txtRooms = rs.RecordCount
End If

End Sub

Open in new window


Fire up a recordset of stays that a given date falls between arrivals and departures.
The count is the number of rooms required.

Sample attached
hotels.mdb
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:snoopyswimer
ID: 40455793
Yes, each person has their own room.  That looks like it will work.  I actually have all my data in one table (tbl_main).  Here are the fields in that table: FirstName, LastName, EmpId (unique number), Arrival, & Departure.    Will that form / code work with just one table?
0
 
LVL 26

Assisted Solution

by:Nick67
Nick67 earned 500 total points
ID: 40455809
Sure.
The idea is the same.
Change the line in the sample to
Set rs = CurrentDb.OpenRecordset("Select EmpId from tbl_main where arrival <= #" & Me.txtDate & "# and departure >= #" & Me.txtDate & "#;", dbOpenDynaset, dbSeeChanges)
Should git 'er dun
0
 

Author Closing Comment

by:snoopyswimer
ID: 40455899
This worked perfectly!
0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 40455977
I'm confused.  That only returns the value for a single day, that is not what you asked in your original post.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40456031
@fyed

It's not a far stretch to put multiple boxes onto a form to run it for multiple entered dates.
It's also not a far stretch to turn it into a function that you feed date in, and get # of rooms out.
Or to dummy up a form that figured out the maximum number of days between [arrival] & [departure] and to do the calculation for each day -- that's more complex as you need a dummy table to bind to get sequential numbers -- like you suggested.
Which I could have done for the Asker if requested.

But the question was "How do I find the number of rooms I need for each night"  Singular
I didn't read it as 'How do I find the number of rooms I need for each and every night"  Totality

:)

But then I always think in terms of VBA recordsets as my weapon of choice -- yours is SQL.
I thought about suggesting DCount -- but Domain Aggregates are just evil.

Nick67
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question