473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

...fill missing dates between date range

Hello, I work in a charity who help people in need with temp accommodation (houses or units).

I need to add vacant periods for each property to calculate the vacancy rate. The yellow rows in my sample are the ones to be added by vb in a table called Lease.

I have a sub-form who show periods in Lease for that property.

We want the VACANT records to be display too.
Thanks millions in advance - I been going crazy about this for days.
Thanks Colo


Attached Images
File Type: jpg sample.jpg (42.5 KB, 817 views)
File Type: jpg sample form.jpg (44.4 KB, 929 views)
Dec 21 '15 #1
7 2146
zmbd
5,501 Expert Mod 4TB
In your first image I see what looks like an excel/workbook file - is that correct, and now you are trying to do the exactly the same thing in Access that you were doing in the worksheet?

You can do this by expansion query and a few other means; however, if I may:

+ One doesn't normally store a calculated value such a s [RentLength] in the database.
Instead, this is calculated "on the fly" in the query using a calculated field, or a similar concept with a calculated control on a form or report.

+ I notice that your start and end dates for the rental period appear to coincide with the start end dates of the vacant periods so to take your first few rows:
rented from 2012-01-11 thru 2013-11-28
vacant from 2013-11-28 thru 2014-06-30
rented from 2014-06-30 thru 2015-02-23

Thus, you should have all of the information needed to calculate the vacancy directly:

So what I would do is take your earliest rent start date of the period in question (2012-01-11) and the oldest rent end date (or current date) calculate the total days between the two. (you don't have one in the first image so today's date 2015-12-21 which gives 1440 days) sum the calculated rent length from records
Note there appears to be something off in the first image:
2012-01-11 thru 2013-11-28 = 687days
2014-06-30 thru 2014-10-02 = 97days
2015-02-23 thru 2015-07-27 = 519days
2015-11-15 thru today = 36days
Sum is 1339
(1339/1440)*100 = 92.99% occupancy == 7.01% vacancy

This can also be done in the worksheet.
Dec 22 '15 #2
Rabbit
12,516 Expert Mod 8TB
Your result set doesn't make sense.

Why does the second vacancy overlap with an existing lease? And what algorithm do you use to determine that a lease doesn't count towards occupancy?

Also, why does your vacancy range overlap with the end of the previous lease and start of the next lease? If seems to me that you run the risk of having 2 groups of people in the same lease on the same day unless you include some sort of time element.
Dec 22 '15 #3
zmbd
5,501 Expert Mod 4TB
Good catch Rabbit, I didn't take that close of a look at the second data-set shown - instead focusing on the first... I have a feeling that there are other issues here yet to be discovered :)
Dec 22 '15 #4
Thanks for the quick response and help. I try to do this in Access (i copy my query result into Excel only to make a sample).

I need to add those VACANT records into the Lease table. I highlight in yellow the records to be added. Rabbit thanks for pointed the overlapping in the dates.
Thanks guys
Attached Images
File Type: jpg sample.jpg (32.8 KB, 264 views)
Dec 22 '15 #5
Expand|Select|Wrap|Line Numbers
  1. Public Function FillVacancy(PropertyID As String, TenantID As String, DATEEFF As Long)
  2.  
  3.   Dim dbs As Database
  4.   Dim rst As Recordset
  5.   Dim strSelect As String
  6.  
  7.   strSelect = "SELECT * FROM Lease WHERE [PropertyID] = " & "'" & PropertyID & "'"
  8.  
  9.   Dim datMin  As Date
  10.   Dim datMax  As Date
  11.   Dim datLog  As Date
  12.   Dim datNew  As Date
  13.   Dim datOld  As Date
  14.  
  15.   Set dbs = CurrentDb
  16.   Set rst = dbs.OpenRecordset(strSelect)
  17.   With rst
  18.     ' Find first and last logging date.
  19.     .MoveLast
  20.     If Not IsNull(!EndDate.Value) Then
  21.                             datMax = !EndDate.Value
  22.                             Else
  23.                             datMax = Now
  24.     End If
  25.     .MoveFirst
  26.     datMin = !StartDate.Value
  27.     ' Set initial low value of empty interval.
  28.     datOld = datMin
  29.     ' Set initial high value of current empty interval.
  30.     datLog = datMin
  31.     .MoveNext
  32.     ' Locate empty intervals.
  33.     ' Stop when reaching the last of the old records.
  34.     While .EOF = False And [PropertyID] = PropertyID And datLog < datMax
  35.       ' Set high value of a possible empty interval.
  36.       datLog = !StartDate.Value
  37.       ' Increment low value of possible empty interval by one hour.
  38.       datNew = DateAdd("h", 1, datOld)
  39.       ' Fill empty intervals with hourly recordings of Null values.
  40.       Do While DateDiff("h", datNew, datLog) >= 1 And datNew < datMax
  41.         .AddNew
  42.           !PropertyID.Value = PropertyID
  43.           !LeaseID.Value = "VACANT"
  44.           !TenantID.Value = TenantID
  45.           !DATEEFF = DATEEFF
  46.           !StartDate.Value = datNew
  47.         .Update
  48.  
  49.         datNew = DateAdd("h", 1, datNew)
  50.  
  51.       Loop
  52.       ' Set low value of the next possible empty interval.
  53.       datOld = datLog
  54.       .MoveNext
  55.     Wend
  56.     .Close
  57.   End With
  58.  
  59.   Set rst = Nothing
  60.   Set dbs = Nothing
  61.  
  62. End Function
Dec 22 '15 #6
I starting with this code but it got too confused and I don't think I am in the right path. too messy
Dec 22 '15 #7
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code or formatted data.

There's no need for any code, you can do it in just SQL.

What you do is join the table to itself on the leases that occur after the end of the lease.

The end date plus one day from the first reference becomes the start date of the vacancy.

You take the minimum date of the start date from the second table and subtract a day as the end of the vacancy.

And in the having clause, you filter out those that start right after each other.

Also, I agree with zmdb, you should just keep the results as a query. It's a bad idea to store it in the table.
Dec 22 '15 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Dennis M. Marks | last post by:
What is the maximum valid date range for the date(yyyy,mm,dd) function. -- Dennis M. Marks http://www.dcs-chico.com/~denmarks/ Replace domain.invalid with dcsi.net -----= Posted via...
24
by: PromisedOyster | last post by:
Is there a way that I can get a resultset that contains unique dates in a given date range without the need to have a temporary table and a cursor? perhaps something like: declare @start_date...
4
by: Mark | last post by:
Hi I have been trying to convert the week number to a range of dates that I can use. It should be fairly simple for you guru's out there but for us mere mortals it is beyond our grasp. I know...
2
by: Adam | last post by:
Hi All, This may be a really obvious thing that I'm missing ... but if anyone can help, I'd appreciate it. I have MS Access 2000: I'm using it for a CRM type database. I have a table with...
2
by: Sara | last post by:
I have followed instructions on the http://allenbrowne.com/tips.html for limiting a report to a date range. At the bottom there is a note that says You will end up using this form for all sorts...
10
by: RoadRunner | last post by:
Hi, I have a employee vacation database that has a vacation table that has the employee name, pay week and date of vacation. I have another lookup table with pay week code and date range for the...
3
by: rugger81 | last post by:
I am currently working in the sql server 2000 environment and I want to write a function to pull all dates within a given date range. I have created several diferent ways to do this but I am...
0
by: rdemyan via AccessMonster.com | last post by:
I have a need to highlight a date range in a bar chart. If the "highlighting" is accomplished by changing the bar column color for the specific months in the date range, that would be great. ...
3
by: Deano | last post by:
The short version; In short, given one date range (start and end dates) how can I find the period that overlaps with another date range? The long version; I have knocked up a little application...
0
Jerry Maiapu
by: Jerry Maiapu | last post by:
I HAVE THIS Sql Query ( qrysummary) : SELECT .Salutation, ., ., IIf((WorkingDays(!!, !!))-()<0,0, (WorkingDays(!!,!!))-()) AS , IIf(IsNull(),0,) AS , .SumOfHrtMintNoAOT,
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.