hello people!!
I have a tblCovid where essentially I keep hospital admissions (StudID, StartDate, EndDate). EndDate can be null if the Stud is already admitted
what I'm trying to do is create a vba code which will prevent users from entering overlapping admissions.
This how far I've come so far but still not quite there
Note that if there is an existing admission with no enddate , then the user can create another record only if the enddate of the new record is before the start date of the existing admission.
I have a tblCovid where essentially I keep hospital admissions (StudID, StartDate, EndDate). EndDate can be null if the Stud is already admitted
what I'm trying to do is create a vba code which will prevent users from entering overlapping admissions.
This how far I've come so far but still not quite there
Note that if there is an existing admission with no enddate , then the user can create another record only if the enddate of the new record is before the start date of the existing admission.
Code:
Public Function CheckAdmissionOverlap(StartDate As Date, EndDate As Variant, StudID As Long, TableName As String, ByVal FormName As String, ByVal IDFieldName As String) As Boolean
' Function to check if there is an overlap with existing admission records
Dim strSQL As String
Dim overlapCount As Long
' Format dates for use in the SQL query (dd/mm/yyyy format)
Dim frmStartDate As String
Dim frmEndDate As String
frmStartDate = "#" & Format(StartDate, "dd/mm/yyyy") & "#"
frmEndDate = "#" & Format(EndDate, "dd/mm/yyyy") & "#"
' Build the SQL string to count overlapping entries
strSQL = "StudID = " & StudID
' Change to recordset (arnelgp)
Dim db As DAO.Database, SameStudID As Boolean
Set db = CurrentDb
If Len(FormName) > 0 And Len(IDFieldName) > 0 Then
Dim RecordID As Long
RecordID = Forms(FormName).Controls(IDFieldName).Value
With db.OpenRecordset("SELECT ID, StartDate AS tblStartDate, EndDate AS tblEndDate FROM " & TableName & " WHERE " & strSQL, dbOpenSnapshot, dbReadOnly)
SameStudID = (.RecordCount <> 0)
If SameStudID Then
.MoveFirst
Do Until .EOF
If ![ID] <> RecordID Then
If frmEndDate = "" Or IsNull(![tblenddate]) Then
' Handle the case where either frmEndDate is an empty string or tblEndDate is null
If frmEndDate = "" And IsNull(![tblenddate]) Then
overlapCount = overlapCount + 1
MsgBox "There is an overlap, please check again", vbExclamation, "OverlapCheck"
ElseIf Not IsNull(![tblenddate]) Then
If ![tblenddate] >= StartDate Then
overlapCount = overlapCount + 1
MsgBox "This student is already admitted on " & Format(StartDate, "dd-mm-yyyy") & ".", vbExclamation, "OverlapCheck"
End If
ElseIf Not frmEndDate = "" Then
If frmEndDate >= ![tblstartdate] Then
overlapCount = overlapCount + 1
MsgBox "This student is already admitted on " & frmEndDate & ".", vbExclamation, "overlapcheck"
End If
End If
Else
' No empty string or null values, use the strSQL condition
strSQL = "StudID = " & StudID & " " & _
"AND ((StartDate <= " & frmEndDate & " AND EndDate >= " & frmStartDate & ") " & _
"OR (StartDate <= " & frmStartDate & " AND EndDate >= " & frmEndDate & "))"
' For demonstration purposes, printing the final strSQL
Debug.Print "Final SQL Query: " & strSQL
Debug.Print "OverlapCount: " & overlapCount
End If
End If
.MoveNext
Loop
End If
End With
End If
' Return True if there is an overlap, False otherwise
CheckAdmissionOverlap = (overlapCount > 0)
End Function