The code I posted in my original reply is that for the WithinDateRange function. You should paste it into any standard module in the database, and be sure to save the module under a name which differs from that of the function or any other object in the database. In my case the module is named basDateStuff.
In my case the function is called in the RowSource query of a combo box to restrict the combo box's list to those rooms where the booked date ranges do not intersect with the date range defined by the start and end dates entered into the booking form, i.e. those rooms which are available for the whole of the desired data range. In the query the Nz() function is used to return a date far into the future if no end date is entered. Consequently the booking is open ended , and will only be allowed for rooms which have no bookings on or after the desired start date.
If you want to use it to validate a range entered into a form then the form's BeforeUpdate event procedure would be an appropriate place. To do this you should first create a query which calls the function, in the same way that the query I posted in my original reply does, referencing the controls in your form as parameters. To detect where there is an intersection of the ranges, and the booking is therefore invalid, you'd call the Dlookup function to determine whether a row is returned by the query where there would be an intersection of the ranges. The query would consequently be like this:
PARAMETERS Forms!YourFormName!Booking_tbl_FHtblUI LONG,
Forms!YourFormName!ArrivalDate DATETIME;
SELECT *
FROM Booking_tbl
WHERE Booking_tbl_FHtblUI = Forms!YourFormName!Booking_tbl_FHtblUI
AND WITHINDATERANGE(Forms!YourFormName!ArrivalDate,
Forms!YourFormName!ArrivalDate+Forms!YourForm!Number_Of_Nights,
Arrival_Date, Arrival_Date+Number_Of_Nights);
The code to validate the dates entered in the form would be like this:
Const MESSAGE_TEXT = "One or more of the dates selected have already been booked."
If Not IsNull(DLookup("Booking_tbl_UI","YourQueryName")) Then
MsgBox MESSAGE_TEXT, vbExclamation, "Invalid Operation"
Cancel = True
End If
You'll appreciate that I'm unable to test the above without access to your file, so cannot rule out the possibility that some debugging might be required.