Private Function GetWhereClause() As String
On Error Resume Next
Dim dbs As DAO.Database
Dim sSQL As String
Dim ctl As control
Dim sCtlList As String
Dim aCtlLst() As String
Dim iCurr As Integer
Dim sWhere As String
Dim sValLst As String
Dim sCtlName As String
Dim sCtlName2 As String
Dim sFieldName As String
Dim sCommonName As String
Dim sType As String
Dim sValue As String
Dim varValue As Variant
Dim dDateStart As Date
Dim dDateEnd As Date
Dim fCtlExists As Boolean
Dim fCtlVisible As Boolean
Dim fCtlEnabled As Boolean
Set dbs = CurrentDb
sSQL = "DELETE * FROM zwtblWHERE"
dbs.Execute sSQL
' initialize where String
sWhere = " 1=1 " & vbCrLf
sCtlList = Me.lstReports.Column(3)
If Right(sCtlList, 1) <> "," Then sCtlList = sCtlList & ","
aCtlLst = Split(sCtlList, ",")
' Loop through the list of controls
For iCurr = 0 To UBound(aCtlLst)
' For each new control name, reset variables
sValue = ""
varValue = Null
fCtlExists = False
fCtlEnabled = False
fCtlVisible = False
sCtlName = aCtlLst(iCurr)
sType = Left(sCtlName, 3)
' Error Handling is set to RESUME NEXT because our list might
' contain names of controls that don't exist on this form. It
' is a list of "potential" controls. If the control doesn't
' exist, an error will be generated here.
Err.Clear
Set ctl = Me.Controls(sCtlName)
' Use the error (or lack thereof) to decide if the control exists
fCtlExists = (Err.Number = 0)
Err.Clear
If fCtlExists Then
fCtlEnabled = (ctl.enabled = True)
fCtlVisible = (ctl.visible = True)
End If
' Now, process controls that exist, and are Visible & Enabled.
If fCtlExists And fCtlVisible And fCtlEnabled Then
' Retrieve the table field name corresponding to the control
sFieldName = GetFieldNameFromControlName(sCtlName)
sCommonName = GetCommonNameFromControlName(sCtlName)
sSQL = " INSERT INTO zwtblWHERE (FieldName, FieldAlias, FieldType, Delimiter, Operator, ValueList)" & _
" VALUES('" & sFieldName & "','" & sCommonName & "',"
Select Case sType
' txt controls can contain any type of text (delimited with single quotes)
Case "txt"
sValue = Nz(ctl.Value, "")
If sValue <> "" Then
sValue = Replace(sValue, "'", "''")
sWhere = sWhere & " AND ([" & sFieldName & "] = '" & sValue & "') " & vbCrLf
dbs.Execute sSQL & "'Text'," & q & "'" & q & ",'='," & q & "'*" & sValue & "*'" & q & ")"
End If
' lke controls contain text to compare with LIKE operator (*).
Case "lke"
sValue = Nz(ctl.Value, "")
If sValue <> "" Then
sValue = Replace(sValue, "'", "''")
sWhere = sWhere & " AND ([" & sFieldName & "] LIKE '*" & sValue & "*') " & vbCrLf
dbs.Execute sSQL & "'Text'," & q & "'" & q & ",'LIKE'," & q & "'*" & sValue & "*'" & q & ")"
End If
' num controls can only contain numeric data.
Case "num"
sValue = Nz(ctl.Value, "")
If sValue <> "" And IsNumeric(sValue) = True Then
sWhere = sWhere & " AND ([" & sFieldName & "] = " & sValue & ") " & vbCrLf
dbs.Execute sSQL & "'Numeric','','=','" & sValue & "')"
End If
' dte controls may contain only date RANGES.
Case "dte"
' There should always be a pair of date controls. One with
' the START value and one with END. Process only START control.
If InStr(1, sCtlName, "Start") > 0 Then
varValue = Me.Controls(sCtlName)
' Must use valid dates. Continue only if START date is valid.
If IsDate(varValue) Then
dDateStart = CDate(varValue)
sCtlName2 = Replace(sCtlName, "Start", "End")
varValue = Me.Controls(sCtlName2)
' Again, we can only continue if END date is valid
If IsDate(varValue) Then
dDateEnd = CDate(varValue)
sWhere = sWhere & " AND ([" & sFieldName & "] BETWEEN #" & dDateStart & "# AND #" & dDateEnd & "#) " & vbCrLf
dbs.Execute sSQL & "'Date','#','BETWEEN','" & Format(dDateStart, "mmm-dd-yyyy") & " AND " & Format(dDateEnd, "mmm-dd-yyyy") & "')"
End If
End If
End If
' lst controls are multi-select list boxes and use the IN operator
Case "lst"
' (If <All> is selected (first item), then skip this part of WHERE clause.
If ctl.Selected(0) = False Then
sValue = GetSelectedItems(ctl)
sWhere = sWhere & " AND ([" & sFieldName & "] IN " & sValue & ") " & vbCrLf
dbs.Execute sSQL & "'Text'," & q & "'" & q & ",'IN LIST:'," & q & sValue & q & ")"
End If
' cbo controls are single item combo boxes and return single value.
Case "cbo"
sValue = Nz(ctl.Value, "")
If sValue <> "" Then
sValue = Replace(sValue, "'", "''")
' (If <All> is selected, then skip this part of WHERE clause.
If Left(sValue, 4) <> "<All" Then
sWhere = sWhere & " AND ([" & sFieldName & "] = '" & sValue & "') " & vbCrLf
dbs.Execute sSQL & "'Text'," & q & "'" & q & ",'IS','" & sValue & "')"
End If
End If
End Select
End If
' Because we have the error handler set to resume next, we must
' reset the control variable to NOTHING to avoid strange results
Set ctl = Nothing
Next
Set dbs = Nothing
GetWhereClause = sWhere
End Function