I'm using Allen Browne's method that builds an SQL criteria string based on a form's unbound fields. I'm using the criteria to delete table records. I've gotten it pretty far, but ran into a roadblock. One of the fields i'm grabbing criteria from is a multi-select listbox. The list box contains the days of the week...Monday, Tuesday, etc.. The table in question contains information on hearings. It has a date field called, "HearingDate". Hearing dates can be on any (business) day of the week. I want to delete any hearings that occurred on certain day(s) of the week.
Example scenario: I want the user to input a date range...let's say, 4/1/2022 to 4/30/2022. In that date range, there may be hearings on each day of the week. But i only want to delete these hearings, if the hearing date lands on a Tuesday or a Wednesday.
One site said to use the IN clause which i tried (see below). However that didn't seem to work. I know i can loop through the recordset to figure out which ones to delete, but i imagine it's more efficient to delete using an sql command. I tried different variations of double quotes and single quotes with the IN clause, but nothing seems to work. It runs without error, but doesn't delete what is specified. Any help is appreciated.
This is as far as i got...
'***********************************************************************
'Look at each criteria dropdown and build up the criteria string from the non-blank ones.
'***********************************************************************
'--These are required fields, so we don't have to check if they are blank first before including in the strwhere
strWhere = strWhere & "([JudgeID] = " & Me.cboJudge & ") AND "
strWhere = strWhere & "([HearingDate] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
strWhere = strWhere & "([HearingDate] <= " & Format(Me.txtEndDate, conJetDate) & ") AND "
If Not IsNull(Me.cboCourtClerk) And Me.cboCourtClerk <> "" Then
strWhere = strWhere & "([CourtClerkID] = " & Me.cboCourtClerk & ") AND "
End If
If Not IsNull(Me.cboCourtroom) And Me.cboCourtroom <> "" Then
strWhere = strWhere & "([CourtroomNum] = """ & Me.cboCourtroom & """) AND "
End If
If Not IsNull(Me.cboListType) And Me.cboListType <> "" Then
strWhere = strWhere & "([ListType] = """ & Me.cboListType & """) AND "
End If
If Not IsNull(Me.cboRecorder) And Me.cboRecorder <> "" Then
strWhere = strWhere & "([RecorderID] = " & Me.cboRecorder & ") AND "
End If
'See if the string has more than 5 characters (a trailing " AND ") to remove.
lngLen = Len(strWhere) - 5
strWhere = Left$(strWhere, lngLen)
'--For the multi-select listbox, assign chosen values into an array.
Dim i As Integer
Dim SelectedEntry As Variant
Dim DataEntryDays() As Variant
ReDim DataEntryDays(1 To Me.lstBoxDaysOfWeek.ItemsSelected.Count)
i = 1
For Each SelectedEntry In Me.lstBoxDaysOfWeek.ItemsSelected
DataEntryDays(i) = """" & Me.lstBoxDaysOfWeek.ItemData(SelectedEntry) & """"
i = 1 + i
Next
'--Build a string of whatever days were chosen.
strwhereDaysOfWeek = ""
For i = 1 To UBound(DataEntryDays)
strwhereDaysOfWeek = strwhereDaysOfWeek & DataEntryDays(i) & ","
Next
strwhereDaysOfWeek = Left(strwhereDaysOfWeek, Len(strwhereDaysOfWeek) - 1)
'--Combine everything together.
strWhere = strWhere & " AND (""" & WeekdayName(Weekday(HearingDate)) & """ IN (" & strwhereDaysOfWeek & "))"
mysql = "DELETE * FROM tblHearings WHERE " & strWhere
CurrentDb.Execute mysql