Double Booking Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnblack
    New Member
    • Feb 2007
    • 16

    #1

    Double Booking Problem

    I am creating a tennis court booking system. I am trying to create a macro that will not allow the same court to be booked on the same date at the same time.

    I have the following tables and fields

    tblMembers
    MemberID
    FirstName
    LastName
    etc

    tblCourts
    CourtID
    CourtName

    tblSchedule
    ScheduleID
    ScheduleDate
    CourtID

    tblSheduleDetai ls
    SheduleDetailsI D
    SheduleID
    MemberID
    SheduleStartTim e
    SheduleEndTime

    I have tried to modify the code form a similar post but am getting an error for "Cancel = True"

    Code:
    Private Sub Form_Current()
    If Me.NewRecord = True Then
        Dim strWhere As String, strMessage As String
        Dim rsClone As Recordset
    
        strWhere = "(([BookingsSubform].Form![CourtID]=" & Me.[BookingsSubform].Form![CourtID] & _
                   ") AND ([BookingsSubform].[Form]![ScheduleID]=" & Me.[BookingsSubform].[Form]![ScheduleID] & _
                   ") AND ([BookingsTimeSubform].[Form]![ScheduleEndTime]>=#" & _
                   Format(Me.[BookingsTimeSubform].[Form]![ScheduleStartTime], "Medium Time") & _
                   "#) AND ([BookingsTimeSubform].[Form]![ScheduleStartTime]<=#" & _
                   Format(Me.[BookingsTimeSubform].[Form]![ScheduleEndTime], "Medium Time") & _
                   "#))"
    
        Set rsClone = Me.RecordsetClone
        rsClone.MoveFirst
        rsClone.FindFirst strWhere
    
        If rsClone.NoMatch Then
            MsgBox ("test")
           Cancel = True
            Exit Sub
        End If
    End If
    End Sub
    Any help would be very nice indeed.
    John
  • johnblack
    New Member
    • Feb 2007
    • 16

    #2
    Just to let the know the error is "Compile error: variable not defined"

    Comment

    • MSeda
      Recognized Expert New Member
      • Sep 2006
      • 159

      #3
      try moving your code to the form's before update event. that is the correct event for your validation.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        As MSeda says, the code is from a
        Code:
        Private Sub Form_BeforeUpdate(Cancel As Integer)
        End Sub
        ...procedure where the variable Cancel is predefined.
        Trying to use this code in an OnCurrent event procedure will not work as there is no variable defined there for it to make sense. Hence your error message.

        Comment

        • johnblack
          New Member
          • Feb 2007
          • 16

          #5
          Ok i have moved the code to the before update event (thanks Mseda) but double bookings are still allowed (the code has no effect). Any ideas from anyone regarding the code.
          Thanks
          John

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Originally posted by johnblack
            Code:
            Private Sub Form_Current()
            If Me.NewRecord = True Then
                Dim strWhere As String, strMessage As String
                Dim rsClone As Recordset
            
                strWhere = "(([BookingsSubform].Form![CourtID]=" & Me.[BookingsSubform].Form![CourtID] & _
                           ") AND ([BookingsSubform].[Form]![ScheduleID]=" & Me.[BookingsSubform].[Form]![ScheduleID] & _
                           ") AND ([BookingsTimeSubform].[Form]![ScheduleEndTime]>=#" & _
                           Format(Me.[BookingsTimeSubform].[Form]![ScheduleStartTime], "Medium Time") & _
                           "#) AND ([BookingsTimeSubform].[Form]![ScheduleStartTime]<=#" & _
                           Format(Me.[BookingsTimeSubform].[Form]![ScheduleEndTime], "Medium Time") & _
                           "#))"
            
                Set rsClone = Me.RecordsetClone
                rsClone.MoveFirst
                rsClone.FindFirst strWhere
            
                If rsClone.NoMatch Then
                    MsgBox ("test")
                   Cancel = True
                    Exit Sub
                End If
            End If
            End Sub
            Your search criteria is set up wrong. You're using [BookingsSubform].Form![CourtID] when you should be using the name of the field from the table.

            Comment

            • MMcCarthy
              Recognized Expert MVP
              • Aug 2006
              • 14387

              #7
              In other words as Rabbit says ...

              Code:
               
              strWhere = "(([CourtID]=" & Me.[BookingsSubform].Form![CourtID] & _
              Mary

              Comment

              • johnblack
                New Member
                • Feb 2007
                • 16

                #8
                Sorry if i am a slow understandig what you guys mean but i am a newbie to VBA. Now i have this code:
                Code:
                    strWhere = "(([CourtID]=" & Me.[BookingsSubform].Form![CourtID] & _
                               ") AND ([ScheduleDate]=" & Me.[BookingsSubform].[Form]![ScheduleDate] & _
                               ") AND ([BookingsTimeSubform].[Form]![ScheduleEndTime]>=#" & _
                               Format(Me.[BookingsTimeSubform].[Form]![ScheduleStartTime], "Medium Time") & _
                               "#) AND ([BookingsTimeSubform].[Form]![ScheduleStartTime]<=#" & _
                               Format(Me.[BookingsTimeSubform].[Form]![ScheduleEndTime], "Medium Time") & _
                               "#))"
                But it still doesnot work.
                Any more ideas?
                Thanks
                John

                Comment

                • MMcCarthy
                  Recognized Expert MVP
                  • Aug 2006
                  • 14387

                  #9
                  Code:
                  AND [BookingsTimeSubform].[Form]![ScheduleEndTime]>=#" & _
                                 Format(Me.[BookingsTimeSubform].[Form]![ScheduleStartTime], "Medium Time") & _
                                 "#) AND ([BookingsTimeSubform].[Form]![ScheduleStartTime]<=#" & _
                                 Format(Me.[BookingsTimeSubform].[Form]![ScheduleEndTime], "Medium Time") & _
                  All this is doing is comparing values on the form. What is it you are trying to achieve here. If you explain the logic of the criteria I can help further.

                  Mary

                  Comment

                  • johnblack
                    New Member
                    • Feb 2007
                    • 16

                    #10
                    I am trying to not allow double bookings to be entered in the form. This involves not allowing a particular court to be booked on a particular date for a time that is already taken. For example
                    Court 1, 18/02/07, 11:00 AM - 01:00 PM and
                    Court 1, 18/02/07, 11:00 AM - 12:00 PM and
                    Court 1, 18/02/07, 11:00 AM - 01:00 PM should not be allowed as there are repeats and overlaps

                    But other entries such as
                    court 1, 18/02/07, 12:00 PM-01:00 PM and
                    court 1, 18/02/07, 01:00 PM-02:00 PM and
                    court 1, 18/02/07, 02:00 PM-05:00 PM should be allowed as there are no repeats or overlaps.

                    Hope that helps tell me if you need any more detail.
                    Thanks, John

                    Comment

                    • MMcCarthy
                      Recognized Expert MVP
                      • Aug 2006
                      • 14387

                      #11
                      Hi John,

                      Your code won't work because the RecordsetClone won't allow you to reference the start and end times of the bookings. Try the following instead:

                      Code:
                      Private Sub Form_BeforeUpdate(Cancel As Integer)
                      Dim db as DAO.Database
                      Dim rs as DAO.Recordset
                          Dim strMessage As String
                      Dim startTime As Date
                      Dim endTime As Date
                      Dim test As Boolean
                      
                        test = False
                        If Me.NewRecord = True Then
                      	set db = CurrentDb
                      	Set rs = db.OpenRecordset("SELECT [ScheduleStartTime], [ScheduleEndTime] " & _
                      	   "FROM tblSchedule INNER JOIN tblScheduleDetails " & _
                      	   "ON tblSchedule.ScheduleID=tblScheduleDetails.ScheduleID " & _
                      	   "WHERE [CourtID]=" & Me.[BookingsSubform].Form![CourtID] & _
                      	   " AND [ScheduleDate]=#" & Me.[BookingsSubform].[Form]![ScheduleDate] & "#)"
                      
                      	startTime = Me.[BookingsTimeSubform].[Form]![ScheduleStartTime]
                      	endTime = Me.[BookingsTimeSubform].[Form]![ScheduleEndTime]
                      	If rs.RecordCount=0 Then Exit Sub
                      	rs.MoveFirst
                      	Do Until rs.EOF
                      	  If startTime=rs!ScheduleStartTime Or endTime=rs!ScheduleEndTime Then
                      		test = True
                      	  ElseIf startTime>rs!ScheduleStartTime And startTime<rs!ScheduleEndTime Then
                      		test = True
                      	  ElseIf  endTime<rs!ScheduleEndTime And endTime>rs!ScheduleStartTime Then
                      		test = True
                      	  End If
                      	  If test Then 
                      		rs.MoveLast
                      	  Else
                      		rs.MoveNext
                      	  End If
                      	Loop
                      
                      	If test=False Then
                      	  MsgBox ("Time is available")
                      	Else
                      	  MsgBox ("Time is unavailable")
                      	  Cancel = True
                      	End If
                        End If
                      
                        rs.Close
                        Set rs=Nothing
                        Set db=Nothing
                      
                      End Sub
                      I really think there has to be a better way to do this though.

                      Mary

                      Comment

                      • johnblack
                        New Member
                        • Feb 2007
                        • 16

                        #12
                        Mary, thanks for your time on this.

                        Code:
                            Set rs = db.OpenRecordset("SELECT [ScheduleStartTime], [ScheduleEndTime] " & _
                               "FROM tblSchedule INNER JOIN tblScheduleDetails " & _
                               "ON tblSchedule.ScheduleID=tblScheduleDetails.Schedule  ID " & _
                               "WHERE [CourtID]=" & Me.[BookingsSubform].Form![CourtID] & _
                               " AND [ScheduleDate]=#" & Me.[BookingsSubform].[Form]![ScheduleDate] & "#)"
                        This code was highlighted in red with the error "compile error: expected: list seperator or )". I couldnt solve this sorry to be a pain.
                        John

                        Comment

                        • MMcCarthy
                          Recognized Expert MVP
                          • Aug 2006
                          • 14387

                          #13
                          Sorry my fault the ending bracket should be outside the quote as follows:

                          Code:
                              Set rs = db.OpenRecordset("SELECT [ScheduleStartTime], [ScheduleEndTime] " & _
                                 "FROM tblSchedule INNER JOIN tblScheduleDetails " & _
                                 "ON tblSchedule.ScheduleID=tblScheduleDetails.Schedule  ID " & _
                                 "WHERE [CourtID]=" & Me.[BookingsSubform].Form![CourtID] & _
                                 " AND [ScheduleDate]=#" & Me.[BookingsSubform].[Form]![ScheduleDate] & "#")

                          Comment

                          • johnblack
                            New Member
                            • Feb 2007
                            • 16

                            #14
                            Im sorry to say the code has no effect. I dont know what else to try.
                            Any ideas?
                            Thanks for your time anyway.
                            John

                            Comment

                            • MMcCarthy
                              Recognized Expert MVP
                              • Aug 2006
                              • 14387

                              #15
                              Try removing it from the form event altogether and put it in the after update event of the ScheduleEndTime control instead with some small amendments as follows:
                              Code:
                              Private Sub ScheduleEndTime_AfterUpdate()
                              Dim db as DAO.Database
                              Dim rs as DAO.Recordset
                                  Dim strMessage As String
                              Dim startTime As Date
                              Dim endTime As Date
                              Dim test As Boolean
                              
                                test = False
                                If Me.NewRecord = True Then
                              	set db = CurrentDb
                              	Set rs = db.OpenRecordset("SELECT [ScheduleStartTime], [ScheduleEndTime] " & _
                              	   "FROM tblSchedule INNER JOIN tblScheduleDetails " & _
                              	   "ON tblSchedule.ScheduleID=tblScheduleDetails.Schedule  ID " & _
                              	   "WHERE [CourtID]=" & Me.[BookingsSubform].Form![CourtID] & _
                              	   " AND [ScheduleDate]=#" & Me.[BookingsSubform].[Form]![ScheduleDate] & "#)"
                              
                              	startTime = Me.[BookingsTimeSubform].[Form]![ScheduleStartTime]
                              	endTime = Me.[BookingsTimeSubform].[Form]![ScheduleEndTime]
                              	If rs.RecordCount=0 Then Exit Sub
                              	rs.MoveFirst
                              	Do Until rs.EOF
                              	  If startTime=rs!ScheduleStartTime Or endTime=rs!ScheduleEndTime Then
                              		test = True
                              	  ElseIf startTime>rs!ScheduleStartTime And startTime<rs!ScheduleEndTime Then
                              		test = True
                              	  ElseIf  endTime<rs!ScheduleEndTime And endTime>rs!ScheduleStartTime Then
                              		test = True
                              	  End If
                              	  If test Then 
                              		rs.MoveLast
                              	  Else
                              		rs.MoveNext
                              	  End If
                              	Loop
                              
                              	If test=False Then
                              	  MsgBox ("Time is available")
                              	Else
                              	  MsgBox ("Time is unavailable")
                              	  Me.ScheduleStartTime = Null
                              	  Me.ScheduleEndTime = Null
                              	End If
                                End If
                              
                                rs.Close
                                Set rs=Nothing
                                Set db=Nothing
                              
                              End Sub

                              Comment

                              Working...