Hi,
I've got a database (Access 2010, Windows 7) that reserves and issues resources. The problem arises for the macro that codes the reservations for the loan items. I can get the macro to display multiple reservations of the same resource (provided they are all on the same day). However, the price I pay for this is that if I make a reservation for today, while setting the return on a later date, the macro will not permit me to set the time in before the time out. While this makes sense for reservations on the same day, it doesn't necessarily for reservations that should be returned on another day.
This limitation is set through some if statements to test that the 'DueDate' is not before the 'Reservation From date', and that the Time the resources are checked in is not before the time they are checked out:
Provided the reservations do not transcend more than one day, the code works.
If the return date is the next day, the macros will not permit the return time before the issue time. This is clearly a problem.
I was partly able to get around this problem with a third block of code:
When I run this third block of code for an item being returned on a later day, I am able to get it to permit the time in before the time out. However, it will message "date clash" if I try to make more than one reservation on the same day, regardless whether the time in of the first reservation is before the time out of the next.
I have included the full code for the Reserve Form Macro below. I would really appreciate it if someone with more experience than I might be able to spot any glaring mistakes in the logic.
Many thanks,
BitesBoy
I've got a database (Access 2010, Windows 7) that reserves and issues resources. The problem arises for the macro that codes the reservations for the loan items. I can get the macro to display multiple reservations of the same resource (provided they are all on the same day). However, the price I pay for this is that if I make a reservation for today, while setting the return on a later date, the macro will not permit me to set the time in before the time out. While this makes sense for reservations on the same day, it doesn't necessarily for reservations that should be returned on another day.
This limitation is set through some if statements to test that the 'DueDate' is not before the 'Reservation From date', and that the Time the resources are checked in is not before the time they are checked out:
Code:
If Me.DueDate < Me.DateCheckedOut Then MsgBox "Due Date cannot be before the Reservation From date", vbOKOnly + vbExclamation, "Date Error" Cancel = True Exit Function End If If Format(Me.TimeIn, "Short Time") < Format(Me.TimeOut, "Short Time") Then MsgBox "Time In cannot be before Time Out. Please amend times.", vbOKOnly + vbExclamation, "Time Error" Cancel = True Exit Function End If
If the return date is the next day, the macros will not permit the return time before the issue time. This is clearly a problem.
I was partly able to get around this problem with a third block of code:
Code:
If Me.DueDate = Me.DateCheckedOut And Format(Me.TimeIn, "Short Time") < Format(Me.TimeOut, "Short Time") Then MsgBox "Time In cannot be before Time Out. Please amend times.", vbOKOnly + vbExclamation, "Time Error" Cancel = True Exit Function End If
I have included the full code for the Reserve Form Macro below. I would really appreciate it if someone with more experience than I might be able to spot any glaring mistakes in the logic.
Many thanks,
BitesBoy
Code:
Option Compare Database Private Sub Form_Current() varDuration = DLookup("DefaultLoanDuration", "Equipment", "EquipmentID = '" & Nz(Me.EquipmentID, "") & "'") Me.txtDuration = varDuration End Sub Public Function CheckValidReservationOld(Cancel As Integer) Dim db As DAO.Database, rs As DAO.Recordset Dim intnewrec As Integer Set db = CurrentDb() Set rs = db.OpenRecordset("SELECT EquipmentID, DateCheckedOut, DueDate, IsReservation FROM Loan WHERE EquipmentID='" & Me.EquipmentID & "' AND (IsReservation=True OR DateCheckedIn Is Null) ORDER BY DateCheckedOut ASC") If Me.DueDate < Me.DateCheckedOut Then MsgBox "Due Date cannot be before the Reservation From date", vbOKOnly + vbExclamation, "Warning" Cancel = True Exit Function End If If rs.RecordCount > 0 And rs.RecordCount <> -1 Then MsgBox "Reservations or loans exist", vbOKOnly 'Item already has one or more reservations 'Need to check if the new reservation clashes with any existing reservations or bookings rs.MoveFirst Do While Not rs.EOF If (Me.DateCheckedOut >= rs!DateCheckedOut And Me.DueDate <= rs!DueDate) Then MsgBox "Dates clash with an existing booking/reservation", vbOKOnly, "Reservation Exists" Cancel = True Me.DateCheckedOut.SetFocus Exit Function Else 'Continue to save record and close window DoCmd.RunCommand acCmdSaveRecord DoCmd.Close End If Loop Else 'No reservations exist 'Continue to save record and close window DoCmd.RunCommand acCmdSaveRecord DoCmd.Close End If End Function 'Public Function CheckValidReservation(Cancel As Integer) ' 'Dim db As DAO.Database, rs As DAO.Recordset 'Dim intnewrec As Integer ' 'Set db = CurrentDb() ' 'If Me.DueDate < Me.DateCheckedOut Then ' MsgBox "Due Date cannot be before the Reservation From date", vbOKOnly + vbExclamation, "Date Error" ' Cancel = True ' Exit Function 'End If ' 'Set rs = db.OpenRecordset("SELECT EquipmentID, DateCheckedOut, DueDate, IsReservation FROM Loan WHERE EquipmentID='" & Me.EquipmentID & "' AND (IsReservation=True OR DateCheckedIn Is Null) AND (DateCheckedOut>=Date()) ORDER BY DateCheckedOut ASC") ' 'StartDate = Me.DateCheckedOut 'EndDate = Me.DueDate ' 'test = False ' 'If rs.RecordCount = 0 Then ' 'Currently no existing bookings or reservations ' 'Ok to save record and close ' DoCmd.RunCommand acCmdSaveRecord ' DoCmd.Close ' Exit Function 'End If ' 'rs.MoveFirst ' 'Do Until rs.EOF ' 'If StartDate = rs!DateCheckedOut Or EndDate = rs!DueDate Then ' test = True 'ElseIf StartDate >= rs!DateCheckedOut And StartDate <= rs!DueDate Then ' test = True 'ElseIf EndDate <= rs!DueDate And EndDate >= rs!DateCheckedOut Then ' test = True 'End If ' 'If test Then ' rs.MoveLast ' rs.MoveNext 'Else ' rs.MoveNext 'End If ' 'Loop ' 'rs.Close ' 'If test = False Then ' 'Dates selected are ok ' 'Continue to save record and close window ' DoCmd.RunCommand acCmdSaveRecord ' MsgBox "Reservation Saved", vbOKOnly + vbInformation, "Reservation Saved" ' DoCmd.Close 'Else ' 'Display error message to user ' MsgBox "The dates you have selected clash with an existing booking/reservation. Please choose different dates.", vbOKOnly + vbExclamation, "Reservation Clash" ' 'Cancel ' Cancel = True ' Me.DateCheckedOut.SetFocus ' ' Set rs = Nothing ' Set db = Nothing ' ' Exit Function 'End If ' 'Set rs = Nothing 'Set db = Nothing ' 'End Function Public Function CheckValidReservation(Cancel As Integer) Dim db As DAO.Database, rs As DAO.Recordset Dim intnewrec As Integer Set db = CurrentDb() 'If Me.DueDate < Me.DateCheckedOut Then 'MsgBox "Due Date cannot be before the Reservation From date", vbOKOnly + vbExclamation, "Date Error" 'Cancel = True 'Exit Function 'End If 'If Format(Me.TimeIn, "Short Time") < Format(Me.TimeOut, "Short Time") Then ' MsgBox "Time In cannot be before Time Out. Please amend times.", vbOKOnly + vbExclamation, "Time Error" ' Cancel = True ' Exit Function 'End If 'If Me.DueDate = Me.DateCheckedOut And Format(Me.TimeIn, "Short Time") < Format(Me.TimeOut, "Short Time") Then ' MsgBox "Time In cannot be before Time Out. Please amend times.", vbOKOnly + vbExclamation, "Time Error" ' Cancel = True ' Exit Function 'End If If Me.DueDate = Me.DateCheckedOut Then If Format(Me.TimeIn, "Short Time") < Format(Me.TimeOut, "Short Time") Then MsgBox "Time In cannot be before Time Out. Please amend times.", vbOKOnly + vbExclamation, "Time Error" Cancel = True Exit Function End If End If Set rs = db.OpenRecordset("SELECT EquipmentID, DateCheckedOut, DueDate, TimeOut, TimeIn, IsReservation FROM Loan WHERE EquipmentID='" & Me.EquipmentID & "' AND (IsReservation=True OR DateCheckedIn Is Null) AND (DateCheckedOut>=Date()) ORDER BY DateCheckedOut ASC") StartDate = Me.DateCheckedOut EndDate = Me.DueDate TimeOut = Format(Me.TimeOut, "Short Time") TimeIn = Format(Me.TimeIn, "Short Time") test = False If rs.RecordCount = 0 Then 'Currently no existing bookings or reservations 'Ok to save record and close DoCmd.RunCommand acCmdSaveRecord DoCmd.Close Exit Function End If rs.MoveFirst Do Until rs.EOF If StartDate = rs!DateCheckedOut And EndDate = rs!DueDate Then If Me.EquipmentID = "MLAP-SET01" Or Me.EquipmentID = "MLAP-SET02" Or Me.EquipmentID = "MTAB-SET01" Or Me.EquipmentID = "MTAB-SET02" Or Me.EquipmentID = "MTAB-SET03" Or Me.EquipmentID = "MTAB-676c" Or Me.EquipmentID = "MTAB-677c" Or Me.EquipmentID = "MTAB-678c" Or Me.EquipmentID = "MTAB-679c" Or Me.EquipmentID = "MTAB-680c" Or Me.EquipmentID = "MTAB-681c" Or Me.EquipmentID = "MTAB-682c" Or Me.EquipmentID = "MTAB-683c" Or Me.EquipmentID = "MTAB-684c" Or Me.EquipmentID = "MTAB-685c" Then 'Perform additional time tests for netbook sets If IsNull(TimeOut) Or IsNull(TimeIn) Then MsgBox "Please enter time values", vbOKOnly Cancel = True Me.TimeOut.SetFocus rs.Close Set rs = Nothing Set db = Nothing Exit Function Else If TimeOut = rs!TimeOut Or TimeIn = rs!TimeIn Then test = True ElseIf TimeOut >= rs!TimeOut And TimeOut <= rs!TimeIn Then test = True ElseIf TimeIn <= rs!TimeIn And TimeIn >= rs!TimeOut Then test = True End If End If Else test = True End If ElseIf StartDate = rs!DateCheckedOut Or EndDate = rs!DueDate Then test = True ElseIf StartDate >= rs!DateCheckedOut And StartDate <= rs!DueDate Then test = True ElseIf EndDate <= rs!DueDate And EndDate >= rs!DateCheckedOut Then test = True ElseIf StartDate <= rs!DateCheckedOut And EndDate >= rs!DateCheckedOut Then test = True End If If test Then rs.MoveLast rs.MoveNext Else rs.MoveNext End If Loop rs.Close If test = False Then 'Dates selected are ok 'Continue to save record and close window DoCmd.RunCommand acCmdSaveRecord MsgBox "Reservation Saved", vbOKOnly + vbInformation, "Reservation Saved" DoCmd.Close Else 'Display error message to user MsgBox "The dates you have selected clash with an existing booking/reservation. Please choose different dates.", vbOKOnly + vbExclamation, "Reservation Clash" 'Cancel Cancel = True Me.DateCheckedOut.SetFocus Set rs = Nothing Set db = Nothing Exit Function End If Set rs = Nothing Set db = Nothing End Function Private Sub Form_Load() If Me.EquipmentID = "MLAP-SET01" Or Me.EquipmentID = "MLAP-SET02" Or Me.EquipmentID = "MTAB-SET01" Or Me.EquipmentID = "MTAB-SET02" Or Me.EquipmentID = "MTAB-SET03" Or Me.EquipmentID = "MTAB-676c" Or Me.EquipmentID = "MTAB-677c" Or Me.EquipmentID = "MTAB-678c" Or Me.EquipmentID = "MTAB-679c" Or Me.EquipmentID = "MTAB-680c" Or Me.EquipmentID = "MTAB-681c" Or Me.EquipmentID = "MTAB-682c" Or Me.EquipmentID = "MTAB-683c" Or Me.EquipmentID = "MTAB-684c" Or Me.EquipmentID = "MTAB-685c" Then Me.TimeIn.Visible = True Me.TimeOut.Visible = True End If End Sub
Comment