473,408 Members | 1,707 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,408 software developers and data experts.

How to prevent overlapping schedules?

I am using Microsoft Office XP and I have developed an Access database that is used to schedule conference and training rooms within buildings.

I have one main form named "Events" that is used to schedule these rooms.
The "Events" form has several fields on it such as "Building" which is a Combo box with the name "cmbBuilding". There's a field below "Building" named "Location" which is called "cmbLocation". The 2 fields Building and Location are related because when someone chooses a buildling from the list, it modified what options are available in the Location drop down box since different buildings have different rooms on different floors. I have a Locations table, and a Buildings table, which the form uses to populate the drop down boxes.

Below the Location field is "cmbStartDate", "cmbEndDate", "txtStartTime", "txtEndTime" and other fields that are not related to my question.

The "Events" form updates an "Events" table which has these Field Names:
"Location", "Building", "StartDate", "EndDate", "StartTime", "EndTime".

I want to prevent locations from being double booked.

Example:
Person 1 schedules wants to reserve a room in Atlanta (cmbBuilding on form, Building on table). They choose Atlanta from the Building drop down box on the form and the Location drop down box below it gets modified to only show rooms in Atlanta. They then pick a date from a calendar control that populates the StartDate and EndDate fields on the form (cmbStartDate and cmbEndDate on the form but StartDate and EndDate on the table). The dates are autoformatted to this format : MM/DD/YYYY ... or M/D/YYYY if the month and day happen to be less than 10 (in other words, it doesn't put in the leading 0 if less than 10 e.g. not 01/03/2008, simply 1/3/2008). They then enter a StartTime and EndTime (txtStartTime and txtEndTime on the form, StartTime and EndTime on the table).

So... Atlanta - 5th Floor - 3/23/2008 through 3/25/2008 from 09:00am untill 01:00pm on each of those days.

If this reservation was made, I don't want anyone to be able to schedule a date for Atlanta on the 5th floor, between 3/23/2008 and 3/25/2008 between the hours of 9 am to 1pm. However they should still be able to schedule a meeting from 8am to 9am on between 3/23/2008 and 3/25/2008, and they should also still be able to schedule something after 1pm on 3/23/2008 through 3/25/2008.

I reviewed the discussion on these forums at the following pages:
http://www.thescripts.com/forum/thread604404.html
http://www.thescripts.com/forum/thread607917.html

And thought it might be possible to modify the code they suggested that would help him in his situation:

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim rs As DAO.Recordset
  3. Dim strMessage As String
  4. Dim StartTime As Date
  5. Dim EndTime As Date
  6. Dim test As Boolean
  7.  
  8. test = False
  9.  
  10. If Me.NewRecord = True Then
  11.  
  12.     Set db = CurrentDb
  13.     Set rs = db.OpenRecordset("SELECT [ScheduleStartTime], [ScheduleEndTime] " & _
  14.                               "FROM Schedule INNER JOIN ScheduleDetails " & _
  15.                               "ON Schedule.ScheduleID=ScheduleDetails.ScheduleID " & _
  16.                               "WHERE [CourtID]=" & Me.[BookingsSubform].Form![CourtID] & " " & _
  17.                               "AND [ScheduleDate]=#" & Me.[BookingsSubform].Form![ScheduleDate] & "#")
  18.  
  19.     StartTime = Me.[BookingsTimeSubform].[Form]![ScheduleStartTime]
  20.  
  21.     EndTime = Me.[BookingsTimeSubform].[Form]![ScheduleEndTime]
  22.  
  23.     If rs.RecordCount = 0 Then Exit Sub
  24.  
  25.         rs.MoveFirst
  26.  
  27.     Do Until rs.EOF
  28.  
  29.         If StartTime = rs!ScheduleStartTime Or EndTime = rs!ScheduleEndTime Then
  30.  
  31.                 test = True
  32.  
  33.                     ElseIf StartTime > rs!ScheduleStartTime And StartTime < rs!ScheduleEndTime Then
  34.  
  35.                         test = True
  36.  
  37.                     ElseIf EndTime < rs!ScheduleEndTime And EndTime > rs!ScheduleStartTime Then
  38.  
  39.                         test = True
  40.  
  41.         End If
  42.  
  43.  
  44.                     If test Then
  45.  
  46.                         rs.MoveLast
  47.  
  48.                     Else
  49.  
  50.                         rs.MoveNext
  51.  
  52.                     End If
  53.  
  54.     Loop
  55.  
  56.                     If test = False Then
  57.  
  58.                         MsgBox ("Time is available")
  59.  
  60.                     Else
  61.  
  62.                         MsgBox ("Time is unavailable")
  63.                         Me.ScheduleStartTime = Null
  64.                         Me.ScheduleEndTime = Null
  65.  
  66.                     End If
  67.  
  68.     rs.Close
  69.  
  70.     Set rs = Nothing
  71.  
  72.     Set db = Nothing
  73.  
  74.   End If
  75.  
  76.  
They were instructed to put this code into the AfterUpdate event of his EndDate field on his form I believe, which would be ok with me as well, as long as it would work. I do not have any subforms as that person in the links, and he was only using one Date field, which I wouldn't necessarily be opposed to if it made things any easier.

Please help if at all possible.

Thank You
Mar 4 '08
62 8408
NeoPa
32,556 Expert Mod 16PB
Having now looked at the issue you report it seems that this is in fact your problem.

The current code only checks for the dates. The time fields are ignored as they are not included in your Date/Time fields.

If you try to add an item for a date that doesn't match any of your existing events, does it work as expected then?

If so, we know we are on the right track.
Mar 14 '08 #51
NeoPa
32,556 Expert Mod 16PB
If you absolutely have to keep the dates and times separate, you can use the following code :
Expand|Select|Wrap|Line Numbers
  1. ...
  2.   Dim strFilter As String, strFormat As String
  3.  
  4.   strFormat = "m/d/yyyy HH:nn:ss"
  5.   strFilter = "(([Building]='%B') AND " & _
  6.               "([Location]='%L') AND " & _
  7.               "([StartDate]+[StartTime]<=#%E#) AND " & _
  8.               "([EndDate]+[EndTime]>=#%S#))"
  9.   strFilter = Replace(strFilter, "%B", Me.Building)
  10.   strFilter = Replace(strFilter, "%L", Me.cmbLocation)
  11.   strFilter = Replace(strFilter, "%E", Format(Me.cmbEndDate, strFormat))
  12.   strFilter = Replace(strFilter, "%S", Format(Me.cmbStartDate, strFormat))
  13.  
  14.   If DCount("[StartDate]", "[Events]", strFilter) > 0 Then
  15.     Call MsgBox("Selected Range overlaps an existing range in the table")
  16.     Cancel = True
  17.   End If
Please note :
This is certainly not an approach I would recommend.
Mar 14 '08 #52
I could not get the above posted code to produce the "Could not schedule" msg in any way, regardless of what I was scheduling.
Mar 17 '08 #53
NeoPa
32,556 Expert Mod 16PB
Do you have any understanding as to where or why it was failing?

Have you ever tried using the debugging facilities (Debugging in VBA)?
Mar 17 '08 #54
I read through your posts regarding each of the available windows and their possible uses when trying to figure out what your code is doing, however after adding the code to display error codes in the Immediate pane, nothing appears, and there are no errors produced that I can see. It simply allows the record to be scheduled regardless of conflict.
Mar 17 '08 #55
NeoPa
32,556 Expert Mod 16PB
In the margin of the debug window, click on the DCount() line (14 in posted code) to toggle a breakpoint.

When the code stops here, type in, in the Immediate pane (Ctrl-G) :
Expand|Select|Wrap|Line Numbers
  1. ?strFilter & " - " & DCount("[StartDate]", "[Events]", strFilter)
...and see what is displayed in the line below it.
Mar 17 '08 #56
Expand|Select|Wrap|Line Numbers
  1. (([ServiceCenter]='Atlanta') AND ([Location]='Atlanta 4th Floor (Conference Room - 60 w/no tables)') AND ([StartDate]+[StartTime]<=#3/17/2008 00:00:00#) AND ([EndDate]+[EndTime]>=#3/17/2008 00:00:00#)) - 0
  2.  
  3.  
Mar 17 '08 #57
NeoPa
32,556 Expert Mod 16PB
There we go :)

I didn't include any reference in the code to the two time controls on the form. I assume you have something like Me.cmbStartTime & Me.cmbEndTime as ComboBox controls where the operator selects the times?

If you can confirm this and provide me with the names of the two contols I can post a new version of the code that should work.

This is still not a way I would recommend, but it can be made to work :)
Mar 18 '08 #58
The names of the two fields for time are "txtStartTime" and "txtEndTime".

I'd still like to use this way currently but I am open to reorganizing the database in a different manner if it would help in the long run. Could you please suggest any changes that could be made?
Mar 18 '08 #59
NeoPa
32,556 Expert Mod 16PB
Thanks for the control names and please see post #50 for the recommendation I had of how it could be done better. This would apply to the set of Date/Time fields in the table as well as the controls on the form.

AfterUpdate procedures can be used to prepare other controls which contain the full Date/time value. These can be hidden if required.

I will look at the code for the immediate problem (a bit later now).
Mar 18 '08 #60
So you're saying the time fields should be appended to the end of the date field(s)?

Sorry, trying to make sure I understand what it was you're suggesting.

Thanks,
Mar 18 '08 #61
NeoPa
32,556 Expert Mod 16PB
Expand|Select|Wrap|Line Numbers
  1. Dim strFilter As String, strFormat As String
  2. Dim datStartEnd As Date
  3.  
  4. strFormat = "m/d/yyyy HH:nn:ss"
  5. strFilter = "(([Building]='%B') AND " & _
  6.             "([Location]='%L') AND " & _
  7.             "([StartDate]+[StartTime]<=#%E#) AND " & _
  8.             "([EndDate]+[EndTime]>=#%S#))"
  9. strFilter = Replace(strFilter, "%B", Me.Building)
  10. strFilter = Replace(strFilter, "%L", Me.cmbLocation)
  11. datStartEnd = CDate(Me.cmbEndDate & " " & Me.txtEndTime)
  12. strFilter = Replace(strFilter, "%E", Format(datStartEnd, strFormat))
  13. datStartEnd = CDate(Me.cmbStartDate & " " & Me.txtStartTime)
  14. strFilter = Replace(strFilter, "%S", Format(datStartEnd, strFormat))
  15.  
  16. If DCount("[StartDate]", "[Events]", strFilter) > 0 Then
  17. Call MsgBox("Selected Range overlaps an existing range in the table")
  18. Cancel = True
  19. End If
Mar 18 '08 #62
NeoPa
32,556 Expert Mod 16PB
So you're saying the time fields should be appended to the end of the date field(s)?

Sorry, trying to make sure I understand what it was you're suggesting.

Thanks,
Essentially yes.

Have a look at how I've done it in the code and see if that makes sense to you.
Mar 18 '08 #63

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

Similar topics

1
by: km | last post by:
Hi all, python re module deals with only nonoverlapping matches. how to go for if i want to find out overlapping matches and their span ? regards, KM
1
by: André Søreng | last post by:
With the re/sre module included with Python 2.4: pattern = "(?P<id1>avi)|(?P<id2>avi|mp3)" string2match = "some string with avi in it" matches = re.finditer(pattern, string2match) .......
11
by: Max M | last post by:
I am writing a "find-free-time" function for a calendar. There are a lot of time spans with start end times, some overlapping, some not. To find the free time spans, I first need to convert the...
3
by: Phil Sandler | last post by:
All, I have a table with start and end dates/times in it, and would like to be able to calculate the number of hours represented, accounting for overlapping records. Note that I am looking...
4
by: Simon Elliott | last post by:
Is there an equivalent of std::copy which works on STL containers for overlapping ranges? -- Simon Elliott http://www.ctsn.co.uk
1
by: teddysnips | last post by:
Below is the script of a Job called "eFIMS_SendEmail" that I wish to run. The intention is that every day of the week the job will execute a SPROC at timed intervals. For example, the SundayRun...
4
by: Charlie Brown | last post by:
I have a form with 2 custom controls that can be dragged around by a user. How can I check if they overlap each other without performing some kind of Collision detection on them? Is there...
4
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire | last post by:
Hello all, I'm learning C and I still am struggling to understand some basic concepts. For example, I read in the standard that with functions such as strcpy, 'If copying takes place between...
3
by: cowboyrocks2009 | last post by:
Hi, I am trying to write a Java program to plot rectangles with different colors side by side non overlapping but unfortunately I am unable to do that as of now. Suppose I want to create 3...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.