VB Validation if Date Falls between Range - vb.net

I am wondering if some one might be able to assist me with my VB code. I believe I am close, because a lot of the time my code works just fine but I noticed that there might be a weakness to it because at times I get unexpected results. I am thinking I am missing something pretty basic here so bear with me!
Basically, I am trying to create a simple hotel reservation system in a Data Table for a VB project that I created just playing around in my spare time.
What I hope to be able to accomplish is for a user to be able to select a room number from a list box then pick an arrival and departure date. After this happens and they click a button I want the program to be able to compare the Room Number along with the beginning date of the reservation and the end date of the reservation with the room number, beginning Dates and end dates in each row already inside of the Data Table. Anyway, here is basically how my If Statement is structured so far:
For Each data As DataRow In dtRoomReserveData.Rows
If data.Item("RoomNum").ToString.Contains(lstSelectRoomNum.Text) AndAlso data.Item("DateArrive") >= dateArrive.Value.Date AndAlso dr2.Item("DateArrive") <= dateLeave.Value.Date Then
'if there is a reservation in room exists during that Date range display error message to user:
MessageBox.Show("Sorry! That room is already reserved during that date range!")
Exit Sub
End If
Next
Any help offered is greatly appreciated!

Related

Check whether a form date field is equal to 1899-12-30 00:00:00.000

I know this should be simple but after hours of googling I am still failing...
I have a vb.net form application. Basically I have an SQL database with an Employee table with an EmpFinish column in datetime, null format (Employee Finish Date). The data is imported from a linked SQL DB (populated from an external app). Any "blank" dates show as 1899-12-30 00:00:00.000 in SQL.
The Employee table data is shown on a datagridview form, a row is selected, then I want to perform a check to test whether that rows EmpFinish date from the form is earlier or equal to today and is not equal to it's "blank" value i.e. has the employee left employment.
Code excerpt:
Dim currentDateTime = DateTime.Now
Dim selectedEmpFinishDate As DateTime =
EmployeesDataGridView.SelectedRows(0).Cells(5).Value
If selectedEmpFinishDate.CompareTo(currentDateTime) <= 0 Then
' code to do
End If
I didn't start out using the CompareTo() above util I started Googling this issue and it seems this is the best way to compare dates. Yes???
This test works for actual dates e.g. yesterday but also catches all 1899 dates, I have tried lots of tests (using 1 If statement of separate ones) to check for the 1899 blank dates but cannot get anything to work. If I debug the code at the test line, selectedEmpFinishDate supposedly equals #12/30/1899#. If I show the field value onscreen via
MsgBox("selectedFinDate = " & selectedEmpFinishDate) it reports as 00:00:00
I know this must be possible but cannot figure it out.
Could someone please offer a solution as I am running out of hair to pull out.
I have followed djv's advice (I had the same thought last night) and done an override to set these date values to NULL. I was then able to use IsDBNull() successfully.
To clarify where these dates came from... Data is imported into this database from another SQL DB created by a huge business software application. Not sure if they want the dates stored this way or whether the import sets them to be this "blank" default 1899 date.
They actually showed on all forms as 1899 etc so resetting them to NULL is the best option all around.
To anyone else who has these 1899 dates I would advise to update them to NULL.
Thank you to everyone who responded especially djv.

Get Difference in Dates Over Time SQL/Excel

I am trying to get a date difference to determine cycle time of when something arrives to completion. However, I need the product to count towards cycle time average for all days it is here. So, something along the lines if arrivdate='8/16' but completiondate='8/24', I need for the cycle time for this product to be 1 on '8/17', 2 on '8/18', etc until it is 8 on '8/24' and then stops counting. I am willing to do it in either Excel or SQL, if there is a fast way to do it. Below is an example of the data in an Excel sheet
https://drive.google.com/open?id=0B4xYGwf8uS7ZdE5YRDYzXzNuOTQ is a link to the file, as I'm not sure how to insert a table in here.
Does this suit?
If I understand your question properly this simple formula should work. You can replace the 0 with "" to leave it blank

Excel Forumla/VBA to skip blank columns for calculation

I have a particular data set that consists of information for projects our company works on. Each project can go through five different statuses and we have a column that records each date the project is put into that particular status.
Now for the Excel part. We are trying to calculate the days in each status and find the total project time. The total is easy to do, because I can use network days between the project submitted date and the project go live date. The statuses, however, are giving me issues because sometimes a project will skip a status, leaving the date field empty. So what happens is a project goes from status B to status D, the formula for days in status "B" look for a date in column "C" to use as the second date in a NETWORKDAYS formula. When it is empty, the IF argument tells the formula to use TODAY() as the second date. What I need it to do is the search the columns to the right (within the given range of A:E for that row) and use that date if it exists. If not, then it can default to TODAY() because this would be an "active" project that has not moved on.
=IF(IF(OR([#STALLED]<>"",[#CANCELED]<>""),"",IF([#INTAKEDATE]="","",IF([#SCOPEATE]="",NETWORKDAYS([#INTAKEDATE],TODAY()),(NETWORKDAYS([#STATUSADATE],[#SCOPEATE])))))<0,"",(IF(OR([#STALLED]<>"",[#CANCELED]<>""),"",IF([#INTAKEDATE]="","",IF([#SCOPEATE]="",NETWORKDAYS([#INTAKEDATE],TODAY()),(NETWORKDAYS([#STATUSADATE],[#SCOPEATE])))))))
This is done for each of the statuses so the table looks something like this:
PROJECTID PROJECTNAME INTAKEDATE SCOPEATE BUILDDATE TESTDATE GOLIVEDATE INTAKEDAYS SCOPEDAYS BUILDDAYS TESTDAYS
If there is any Macro or better formula someone could help me figure out, I'd appreciate it. There is also another field that gives the current status or if the project is considered live if that helps at all. I have excel-block right now and cant think of anything that makes sense for this.
So I figured it out using MIN and by adjusting the IF formulas a bit. Whoever it was that posted about MAX earlier, really helped me out getting me down this path. Kudos to you sir/madam.
I had to add the check to see if SCOPEDATE=BUILDDATE because without that it was returning 1 if the dates matched, with was adding one more day when compared to running the networkdays from INTAKEDATE to GOLIVEDATE.
=IF(OR([#STALLED]<>"",[#CANCELED]<>""),"",
IF([#SCOPEDATE]="","",
IF([#SCOPEDATE]=[#BUILDDATE],0,
IF(NETWORKDAYS([#SCOPEDATE],(MIN([#BUILDDATE],[#TESTDATE],[#GOLIVEDATE])))<0,NETWORKDAYS([#SCOPEDATE],TODAY()),NETWORKDAYS([#SCOPEDATE],(MIN([#BUILDDATE],[#TESTDATE],[#GOLIVEDATE])))))))
I hope this is helpful for someone else.

Find All 'Rental' that dont have 'Bookings' within a given DateRange

Proposal
What im trying to do, is allow people to input a date range, and find all 'Rentals' places that have open days between the provided date range from the user.
My Rentals have a has_many association with "bookings" which contain a start and end date.
What would be the best possible way to get every rental article that has a day that is open within that range.
Issue
I can find all the Rentals that have Bookings that dont overlap with the given time, but that doesnt account for each day. So if there was a booking from 7 Sept to 10 Sept, and the User entered the range 5 Sept to 9 Sept, that record would simply not be included, but in theory there is the 5th-7th open.
My Possible Solution
Im still thinking this problem through, so i have not started coding. But what i was going to do, is find all the Rentals that DONT have bookings that overlap with the provided Date Range, store those in a variable(because these are correct results, because it means not a single day overlaps).
Then i will find all the Rentals that have bookings that DO overlap with the provided Date Range. I will then loop through those bookings, and within that, loop through the Range of Dates provided by the user(DateRange.Each) and check if each single day is within that bookings date ranges, as soon as a single day results in a false value(meaning at least one day doesnt overlap) it will end the loop and add the associated "rental" article to the articles collected early with no overlaps at all.
Concerns
The only issue i have with this solution, is i feel it might not be very optimized or run well, im not sure. At the worst case, i would have hundreds of "rentals' and im not sure how many different "Bookings". I dont think something like this could be done in a single SQL query could it ?
Sample data
Rentals Model
name: string
has_many Bookings
Bookings Model
rental_id: integer
start_date: Date
end_date: Date
The user inputs a DateRange, i want all Rentals that have open days(none taken by a 'Booking' in the range provided.
EDIT
I have been thinking, and i think the best solution is using postgres db, which allows for arrays. Ill then store all the dates of the booking, in the array, instead of storing the range.
Postgres supports checking if all the values in an given array is present in an array in the DB. So i could check if the array of 'wanted days' is 'contained' by a DB array, if it is, it means none of the days are free.

MAQL - Select all records that enclose a list of dates

I'm a bit of a noob in MAQL so I would like to know if it is possible to do the following thing :
I got a list of reservations with a Start Date and a End Date
I would like to be able to compute a metric that would give me (for reservations that match some criteria) the number of reservations that include a given night.
In the end, I want my metric to exist for each date x:
MyMetric(x) = Number of reservations with Start > x and End < x
I tried to do the following request:
SELECT COUNT(Voucher) WHERE Date(Start)<THIS AND Date(End)>THIS
but I'm having trouble setting up a report saying "Well now I want THIS to take all the values in the calendar".
(Voucher is just an attribute from my "Reservation" dimension).
Anyone knows how I can proceed ? The user manual is very difficult to read for someone who does not have an extensive experience with MAQL.
I unfortunately don't believe that what you're trying to achieve is possible with the data model you have. The reason behind this is because each date is associated with a specific action, such as the date in which a trip begins and when it ends. In order for this to work exactly how you want, it would be necessary to add a date dimension in the project that has a record for each date between the Begin and End date (e.g. Event date=generic date to capture a range of different events or snapshot date).
Hope this helps!