Option Compare Database
Option Explicit
'------------------------------------------------------------------------
'frmEdit has txt, cmb, and chk controls, as well as an option group with 2 radio buttons on it.
'The combo boxes are meant to allow the user to choose how their results are filtered, so
'users can select "begins with," "ends with," "contains," "is bewteen," etc. for many of the
'text box controls.
'
'I've abridged and edited the code, but it's still a rather lengthy. The sections for First Name,
'Last Name, and Sponsor are mostly identical to the Project ID section, so I cut them out
'except for a reference to their location.
'BuildFilter function used with InstantSearch():
Public Function BuildFilter() As String
'Declare and define all variables
'Filters
Dim strWhere As String 'Main ("AND") filter
'Sub ("OR") filters
Dim strIDOr As String 'Project ID "[NOT] IN" and "Between/Outside" cmb subfilter
Dim strRep As String 'Representative chk subfilter
Dim strLastOr As String 'Last name "[NOT] IN" cmb subfilter
Dim strFirstOr As String 'First name "[NOT] IN" cmb subfilter
Dim strSponsorOr As String 'Sponsor "[NOT] IN" cmb subfilter
Dim strStatus As String 'Status chk subfilter
Dim strType As String 'Type chk subfilter
'Condition Variables
Dim strIDCond1 As String 'Start of Project ID condition
Dim strIDCond2 As String 'End of Project ID condition
Dim strLastCond1 As String 'Start of last name condition
Dim strLastCond2 As String 'End of last name condition
Dim strFirstCond1 As String 'Start of first name condition
Dim strFirstCond2 As String 'End of first name condition
Dim strSponCond1 As String 'Start of sponsor condition
Dim strSponCond2 As String 'End of sponsor condition
'Check cmbs for conditions and apply comparison operator ("LIKE") to Condition Variables
'Project ID
If Len(Me.txtProjectID & vbNullString) > 0 Then
'For Project ID "begins with"
If Me.cmbProjectIDCond = "begins with" Then
strIDCond1 = "[Project_ID] LIKE """
strIDCond2 = "*"" AND "
End If
'...
'For Project_ID "ends with"
If Me.cmbProjectIDCond = "ends with" Then
strIDCond1 = "[Project_ID] LIKE ""*"
strIDCond2 = """ AND "
End If
'...
'For Project_ID "contains"
If Me.cmbProjectIDCond = "contains" Then
strIDCond1 = "[Project_ID] LIKE ""*"
strIDCond2 = "*"" AND "
End If
'...
'For Project_ID "equals"
If Me.cmbProjectIDCond = "equals" Then
strIDCond1 = "[Project_ID] = """
strIDCond2 = """ AND "
End If
'...
'For Project_ID "is between"
If Me.cmbProjectIDCond = "is between" Then
'Trim and standardize the user input
strIDOr = strIDOr & Me.txtProjectID
strIDOr = Trim(Replace(strIDOr, ", ", """ AND [Project_ID] <= """))
strIDOr = Trim(Replace(strIDOr, " ,", """ AND [Project_ID] <= """))
strIDOr = Trim(Replace(strIDOr, " , ", """ AND [Project_ID] <= """))
strIDOr = Trim(Replace(strIDOr, ",", """ AND [Project_ID] <= """))
'Strip trailing """ AND [Project_ID] <= """ if present (trailing comma entered)
If Right(strIDOr, 23) = """ AND [Project_ID] <= """ Then
strIDOr = Left(strIDOr, Len(strIDOr) - 23)
End If
'Strip leading """ AND [Project_ID] <= """ if present (leading comma entered)
If Left(strIDOr, 23) = """ AND [Project_ID] <= """ Then
strIDOr = Right(strIDOr, Len(strIDOr) - 23)
End If
'Append """ AND "
strIDOr = strIDOr & """ AND "
'Finalize construction of the strIDOr Condition Variable
strIDOr = "[Project_ID] >= """ & strIDOr
End If
'...
'Check if cmbProjectIDCond uses "[NOT] IN" subfilter (strIDOr)
If (Me.cmbProjectIDCond = "is between") Then
'Integrate "[NOT] IN" subfilter (strIDOr) with main ("AND") filter
'(strWhere)
strWhere = strWhere & strIDOr
Else
'Integrate with main ("AND") filter (strWhere)
strWhere = strWhere & strIDCond1 & Me.txtProjectID & strIDCond2
End If
End If
'Last Name (basically the same as Project ID)
'...
'First Name (basically the same as Project ID)
'...
'Sponsor (basically the same as Project ID)
'...
'That's it for the cmbs and associated txts.
'Below are the chks, date txts, and the option group.
'Construct Filters: Check txts; check optGrps (rads); check if cmbs use "[NOT] IN" subfilters,
'and if not, integrate with main ("AND") filter (strWhere); check chks and build sub ("OR")
'filter; integrate sub ("OR") filter with main ("AND") filter (strWhere)
'Representatives
'Check chk for Charlene
If Me.chkCharlene = -1 Then
'Build sub ("OR") filter (strRep)
strRep = strRep & "[Representative] = ""Charlene"" OR "
End If
'Check chk for Gail
If Me.chkGail = -1 Then
'Build sub ("OR") filter (strRep)
strRep = strRep & "[Representative] = ""Gail"" OR "
End If
'Check chk for Horia
If Me.chkHoria = -1 Then
'Build sub ("OR") filter (strRep)
strRep = strRep & "[Representative] = ""Horia"" OR "
End If
'...
'Integrate sub ("OR") filter (strRep) with main ("AND") filter (strWhere)
'Test for subfilter
If Len(strRep & vbNullString) > 0 Then
'Strip last " OR " in the sub ("OR") filter (strRep)
If Right(strRep, 4) = " OR " Then
strRep = Left(strRep, Len(strRep) - 4)
End If
'Concatenate with strWhere and add parentheses around the sub ("OR") filter (strRep)
strWhere = strWhere & "(" & strRep & ") AND "
End If
'Dates
'Check txt for txtStartDateBefore
If Len(Me.txtStartDateBefore & vbNullString) > 0 Then
strWhere = strWhere & "[StartDate] <= #" & Me.txtStartDateBefore & "# And "
End If
'Check txt for txtStartDateAfter
If Len(Me.txtStartDateAfter & vbNullString) > 0 Then
strWhere = strWhere & "[StartDate] >= #" & Me.txtStartDateAfter & "# And "
End If
'Check txt for txtEndDateBefore
If Len(Me.txtEndDateBefore & vbNullString) > 0 Then
strWhere = strWhere & "[EndDate] >= #" & Me.txtEndDateBefore & "# And "
End If
'Check txt for txtEndDateAfter
If Len(Me.txtEndDateAfter & vbNullString) > 0 Then
strWhere = strWhere & "[EndDate] >= #" & Me.txtEndDateAfter & "# And "
End If
'Status
'Check chk for Prop App
If Me.chkPropApp = -1 Then
'Build sub ("OR") filter (strStatus)
strStatus = strStatus & "[Status] = ""Prop App"" OR "
End If
'Check chk for Under Neg
If Me.chkUnderNeg = -1 Then
'Build sub ("OR") filter (strStatus)
strStatus = strStatus & "[Status] = ""Under Neg"" OR "
End If
'Check chk for Sigs in Progress
If Me.chkSigs = -1 Then
'Build sub ("OR") filter (strStatus)
strStatus = strStatus & "[Status] = ""Sigs in Progress"" OR "
End If
'...
'Integrate sub ("OR") filter (strStatus) with main ("AND") filter (strWhere)
'Test for subfilter
If Len(strStatus & vbNullString) > 0 Then
'Strip last " OR " in the subfilter
If Right(strStatus, 4) = " OR " Then
strStatus = Left(strStatus, Len(strStatus) - 4)
End If
'Concatenate with strWhere and add parentheses around the sub ("OR") filter (strStatus)
strWhere = strWhere & "(" & strStatus & ") AND "
End If
'Industry
'Check optGrp (rads) for Industry and Non-industry
If Me.optGrpInd = 1 Then
'Integrate with main ("AND") filter (strWhere)
strWhere = strWhere & "[Industry] = -1 AND "
End If
If Me.optGrpInd = 2 Then
'Integrate with main ("AND") filter (strWhere)
strWhere = strWhere & "[Industry] = 0 AND "
End If
'Type
'Check chk for 001
If Me.chk001 = -1 Then
strType = strType & "[Type] = ""001"" OR "
End If
'Check chk for 002
If Me.chk002 = -1 Then
strType = strType & "[Type] = ""002"" OR "
End If
'Check chk for 003
If Me.chk003 = -1 Then
strType = strType & "[Type] = ""003"" OR "
End If
'...
'Integrate sub ("OR") filter (strType) with main ("AND") filter (strWhere)
'Test for subfilter
If Len(strType & vbNullString) > 0 Then
'Strip last " OR " in the subfilter
If Right(strType, 4) = " OR " Then
strType = Left(strType, Len(strType) - 4)
End If
'Concatenate with strWhere and add parentheses around the sub ("OR") filter (strType)
strWhere = strWhere & "(" & strType & ") AND "
End If
'Check for/tidy/finish constructing Main Filter
'Check for Main Filter
If Len(strWhere & vbNullString) > 0 Then
If Right(strWhere, 5) = " And " Then
strWhere = Left(strWhere, Len(strWhere) - 5)
'Finish constructing Main Filter
strWhere = "WHERE (" & strWhere & ")"
End If
End If
'Assign Main Filter to BuildFilter() function
BuildFilter = strWhere
End Function