I use this function to detect conflicts between two date ranges (returns true if conflicting, and false otherwise):
Public Function interlapDate(start1 As Date, end1 As Date, start2 As Date, end2 As Date) As Boolean
'Credits to Martin Fowler's Range Pattern Algorithm
interlapDate = end1 >= start2 And end2 >= start1
End Function
See article here
And to put that into perspective, you may use something like:
Private Function roomIsAvailable() as Boolean
Dim strQuery as string
Dim rs as New ADODB.Recordset
Dim newTimeIn as Date
Dim newTimeOut as Date
'Initialize
roomIsAvailable = True
'Assuming from ur sample code that combo1 and combo2 are the user-input range
newTimeIn = TimeValue(CDate(combo1))
newTimeOut = TimeValue(CDate(combo2))
strQuery = "SELECT time_start, time_end" & _
" FROM tbl_sched" & _
" WHERE room LIKE '" & Combo2.Text & "'" & _
" AND day LIKE '" & Combo3.Text & "'"
rs.open strQuery, con, 3, 3
Do While Not rs.EOF
'Compare new range to each range saved in database
If interlapDate(rs!time_start, rs!time_end, newTimeIn, newTimeOut) Then
GoTo conflictFound
Exit Do
End If
rs.moveNext
Loop
Exit Function
conflictFound:
Msgbox "Overlap found!",vbExclamation
roomIsAvailable = False
End Function
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…