Yes, there are several ways to do this. Which you use will depend on the
circumstances. To address a specific, based on your post, one way is to
create a stored query that has no criteria at all (no WHERE). Then,
programmatically create the WHERE portion of the query. Now, retrieve the
SQL of a "template" version of the query, add the WHERE part to it, and save
it back to the stored query.
Here is an example:
Dim strWhere As String
Dim strSQL As String
strSQL = CurrentDb.QueryDefs("qselLaborWeeklyDetailXLTemplate").SQL
strWhere = BuildWhere()
If Len(strWhere) > 0 Then
strSQL = Replace(strSQL, ";", "Having " & strWhere)
End If
CurrentDb.QueryDefs("qselLaborWeeklyDetailXL").SQL = strSQL
Here is an example of how I build a Where condition string when there are
multiple controls that may or may not be included in the criteria:
Private Function BuildWhere() As String
Dim strWhere As String
On Error GoTo BuildWhere_Error
If Len(Me.cboActivity & "") > 0 Then
strWhere = "[CISAttributeTable_ME]![Activity] = '" & Me.cboActivity
& "'"
End If
If Len(Me.cboAcctgUnit & "") > 0 Then
If Len(strWhere) > 0 Then
strWhere = strWhere & " And "
End If
strWhere = strWhere & "[PerformAcctUnit] = '" & Me.cboAcctgUnit & "'"
End If
If Len(Me.cboEmployeeID & "") > 0 Then
If Len(strWhere) > 0 Then
strWhere = strWhere & " And "
End If
strWhere = strWhere & "[EmployeeNum] = '" & Me.cboEmployeeID & "'"
End If
BuildWhere = strWhere
BuildWhere_Exit:
On Error Resume Next
Exit Function
BuildWhere_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure BuildWhere of VBA Document
Form_frmLaborWeeklySelection"
GoTo BuildWhere_Exit
End Function