Hello everyone!
I've been trying to use the code below (created by chatgpt essentially)
I'm essentially trying to pop a msgbox (and not save) whenever there is a given DiagID for a given StudID on overlapping days
e.g. diagID 1 for studID 1 on 10-15 Jan (dur 6 days) already exists. User is trying to enter diagID 1 for studID 1 on 12-20 Jan (dur 9 days). This should prompt a msgbox and not save.
DiagID 2 for studID 1 on 10-15 Jan (dur 6 days) is fine.
I'm getting error 3075 on the dcount line.
Any smart ideas?
PS code also includes mandatory fields, we can ignore that bit
Thanks
I've been trying to use the code below (created by chatgpt essentially)
I'm essentially trying to pop a msgbox (and not save) whenever there is a given DiagID for a given StudID on overlapping days
e.g. diagID 1 for studID 1 on 10-15 Jan (dur 6 days) already exists. User is trying to enter diagID 1 for studID 1 on 12-20 Jan (dur 9 days). This should prompt a msgbox and not save.
DiagID 2 for studID 1 on 10-15 Jan (dur 6 days) is fine.
I'm getting error 3075 on the dcount line.
Any smart ideas?
PS code also includes mandatory fields, we can ignore that bit
Thanks
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctrl As Control
Dim missingFields As String
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
If ctrl.Tag = "Required" And IsNull(ctrl.Value) Then
missingFields = missingFields & ctrl.Name & ", "
End If
End If
Next ctrl
If Len(missingFields) > 0 Then
missingFields = Left(missingFields, Len(missingFields) - 2) ' Remove the trailing comma and space
MsgBox "The following fields cannot be left blank: " & missingFields, vbExclamation, "Missing Information"
Cancel = True ' Prevents the form from being saved
Exit Sub ' Exit the sub to avoid further processing
End If
Dim StartDate As Date
Dim EndDate As Date
Dim Dur As Integer
StartDate = Me.StartDate
Dur = Me.Dur
EndDate = StartDate + Dur - 1
' Check for overlapping diagnosis entry
If DiagOverlapExists(StartDate, EndDate, Me.DiagID, Me.StudID) Then
MsgBox "Leave overlap detected! An overlapping leave entry already exists for the selected dates and leave type."
Cancel = True
End If
End Sub
Function DiagOverlapExists(StartDate As Date, EndDate As Date, DiagID As Long, StudID As Long) As Boolean
' Function to check if an overlapping diagnosis entry exists
Dim strSQL As String
Dim overlapCount As Long
' Format dates for use in the SQL query
Dim formattedStartDate As String
Dim formattedEndDate As String
formattedStartDate = Format(StartDate, "yyyy-mm-dd")
formattedEndDate = Format(EndDate, "yyyy-mm-dd")
' Build the SQL string to count overlapping diagnosis entries
strSQL = "SELECT COUNT(*) FROM tblStudDiag " & _
"WHERE StudID = " & StudID & " AND DiagID = " & DiagID & " " & _
"AND ((StartDate <= #" & formattedEndDate & "# AND EndDate >= #" & formattedStartDate & "#) " & _
"OR (StartDate <= #" & formattedStartDate & "# AND EndDate >= #" & formattedEndDate & "#))"
' Debug statement to print the SQL query to the Immediate Window
Debug.Print "SQL Query: " & strSQL
Debug.Print "StudID: " & StudID
Debug.Print "DiagID: " & DiagID
Debug.Print "StartDate: " & Format(StartDate, "yyyy-mm-dd")
Debug.Print "EndDate: " & Format(EndDate, "yyyy-mm-dd")
' Execute the SQL and retrieve the count
overlapCount = DCount("*", "tblStudDiag", strSQL)
Debug.Print "OverlapCount: " & overlapCount
' Return True if there is an overlap, False otherwise
DiagOverlapExists = (overlapCount > 0)
End Function