On Jun 7, 3:16 pm, DeanL <deanpmlonghu...@yahoo.comwrote:
Hi all,
I'm trying to set up a query that runs from a command button on a form
(simple enough so far), what I want the query to do is take values
from the fields on the form (seven fields in total) but sometimes not
all the fields will be filled. If a field is empty then the
assumption is that no filter will be applied to that field in the
query and should return all results within the limits of the other
field in the query.
How would I set up a query to take a value as a wildcard (*) if no
data is entered into the field?
Probably a simple answer but I think I'm currently suffering from
putty brain or something.
Thanks, Dean...
Here's some working code that filters a schedule of events (Course
Sections) on (optionally) Date/Time and (Optionally) an Instructor's
Department or ID. Basically I check the length of the form fields
("Me...") and if 0 skip appending the where clause onto the filter...
<compiled VBA>
Public Sub SetFilter()
Dim wkFilter As String
Dim wkDate As String
Dim wkTime As String
Dim wkDept As String
Dim wkInst As String
On Error GoTo ProcErr
Me.Filter = ""
Me.FilterOn = False
If (Len(Me.usSetDate) 0) Then
wkDate = CDate(DatePart("m", Me.usSetDate) & "/" & DatePart("d",
Me.usSetDate) & "/" & DatePart("yyyy", Me.usSetDate))
wkTime = CDate(DatePart("h", Me.usSetDate) & ":" & DatePart("n",
Me.usSetDate) & ":" & DatePart("s", Me.usSetDate) & IIf(Right$
(Me.usSetDate, 1) = "M", Right$(Me.usSetDate, 2), ""))
Select Case (wkTime)
Case "12:00:00 AM"
wkFilter = "([EventDate] = #" & wkDate & "#) AND "
Case Else
wkFilter = "([EventDate] = #" & wkDate & "#) AND " & _
"([EventStartTime] <= #" & wkTime & "#) AND " & _
"([EventEndTime] >= #" & wkTime & "#) AND "
End Select
End If
wkDept = Me.usSetDept
wkInst = Me.usSetInst
If (wkDept <"{ALL}") Then
If (Len(wkDept) 0) Then
wkFilter = wkFilter & "([Dept] = '" & wkDept & "') AND "
Else
wkFilter = wkFilter & "(IsNull([Dept]) = True) AND "
End If
End If
If (wkInst <"{ALL}") Then
If (Len(wkInst) 0) Then
wkFilter = wkFilter & "([Instructor] = '" & wkInst & "') AND "
Else
wkFilter = wkFilter & "(IsNull([Instructor]) = True) AND "
End If
End If
If (Len(wkFilter) 0) Then
Me.Filter = Mid$(wkFilter, 1, Len(wkFilter) - 5)
Me.FilterOn = True
End If
ProcExit:
Exit Sub
ProcErr:
Select Case Err
Case 0:
Resume Next
Case Else
Forms.frmMain.Prog 0, "SetFilter Error: " & Err & " " &
Err.Description & " " & Now()
Forms.frmMain.ErrLog "SetFilter", Err, Err.Description
Resume ProcExit
End Select
End Sub
</compiled VBA>
Hopefully there's not too much obscure crap in there to confuse yas.
In this sample, most of the the form fields are drop down lists of
distinct values in the Table.RecordSource UNIONed with the literal
"{ALL}" (the curly braces sort this value to the top of the lists). If
the field is {ALL} then that phrase of the filter is skipped.
Lucks to yas pal!
Ron, King of Chi