Code:
Private Sub cmdSubmit_Click()
'This function dynamically generates a SQL string ("sSQL") based on the data the user enters into the text and combo boxes in the form.
'It then plugs the SQL string into the query qryPanelSearch and displays the results in two reports, rptPanelSearch and rptPanelEmails.
On Error Resume Next
Dim ctl As Control
Dim sSQL As String
Dim sWhereClause As String
Dim db As Database
Set db = CurrentDb
'Start the first part of the select statement. If nothing is in any of the fields, return all records in tblContactsContactInfo.
sSQL = "SELECT * FROM qryPanelSearch "
sWhereClause = " WHERE "
'Loop through each control on the form to get its value.
For Each ctl In Me.Controls
With ctl
Select Case .ControlType
'If the function finds a text box, it checks to see if there is data. If there is data, then it checks to see which text box it is, runs the
'code for that text box, and then iterates to the next control. Within each block of code for a particular control, there are two versions of the
'search string. The first is if the criterion is the FIRST criterion in the search string. The second is if the criterion is the second criteron or later,
'meaning that an "AND" has to be placed in front of it.
Case acTextBox
.SetFocus
If Not ctl.Value = "" Then
'Checks the date range. Date range should be entered using proper search syntax.
If .Name = "txtDateRange" Then
If sWhereClause = " WHERE " Then
sWhereClause = sWhereClause & " (" & BuildCriteria("dtePanelDate", dbDate, txtDateRange.Value) & ")"
Else
sWhereClause = sWhereClause & "AND (" & BuildCriteria("dtePanelDate", dbDate, txtDateRange.Value) & ")"
End If
End If
'Generates a string that searches all presentation titles for the criteria entered.
If .Name = "txtPresentationTitle" Then
If sWhereClause = " WHERE " Then
sWhereClause = sWhereClause & " ((" & BuildCriteria("txtPresentationTitle1", dbText, txtPresentationTitle.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("txtPresentationTitle2", dbText, txtPresentationTitle.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("txtPresentationTitle3", dbText, txtPresentationTitle.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("txtPresentationTitle4", dbText, txtPresentationTitle.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("txtPresentationTitle5", dbText, txtPresentationTitle.Value & "*") & "))"
Else
sWhereClause = sWhereClause & " AND ((" & BuildCriteria("txtPresentationTitle1", dbText, txtPresentationTitle.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("txtPresentationTitle2", dbText, txtPresentationTitle.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("txtPresentationTitle3", dbText, txtPresentationTitle.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("txtPresentationTitle4", dbText, txtPresentationTitle.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("txtPresentationTitle5", dbText, txtPresentationTitle.Value & "*") & "))"
End If
End If
'Searches Panel Chair, Discussants 1 and 2, and Panelists 1-5, respectively for the string entered. See qryPanelSearch for more details.
If .Name = "txtParticipantName" Then
If sWhereClause = " WHERE " Then
sWhereClause = sWhereClause & " ((" & BuildCriteria("qryGetAllNames.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_1.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_2.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_3.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_4.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_5.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_6.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_7.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & "))"
Else
sWhereClause = sWhereClause & " AND ((" & BuildCriteria("qryGetAllNames.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_1.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_2.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_3.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_4.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_5.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_6.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_7.txtFullNameWithKorean", dbText, "*" & txtParticipantName.Value & "*") & "))"
End If
End If
'Searches the e-mail fields for Panel Chair, Discussants 1 and 2, and Panelists 1-5 for the criterion entered.
If .Name = "txtParticipantEmail" Then
If sWhereClause = " WHERE " Then
sWhereClause = sWhereClause & " ((" & BuildCriteria("qryGetAllNames.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_1.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_1.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_2.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_2.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_3.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_3.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_4.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_4.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_5.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_5.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_6.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_6.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_7.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_7.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & "))"
Else
sWhereClause = sWhereClause & " AND ((" & BuildCriteria("qryGetAllNames.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_1.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_1.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_2.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_2.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_3.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_3.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_4.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_4.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_5.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_5.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_6.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_6.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_7.txtEmail", dbText, "*" & txtParticipantEmail.Value & "*") & ")"
sWhereClause = sWhereClause & " OR (" & BuildCriteria("qryGetAllNames_7.txtEmail2", dbText, "*" & txtParticipantEmail.Value & "*") & "))"
End If
End If
End If
Case acComboBox
.SetFocus
If Not ctl.Value = "" Then
'The user can select an event from a dropdown box. This searches for the event name specified in the dropdown box
'and compares it with the event name specified in the query.
If .Name = "cboEventName" Then
If sWhereClause = " WHERE " Then
sWhereClause = sWhereClause & " (" & BuildCriteria("txtEventName", dbText, cboEventName.Column(1)) & ")"
Else
sWhereClause = sWhereClause & " AND (" & BuildCriteria("txtEventName", dbText, cboEventName.Column(1)) & ")"
End If
End If
'The user can select the Project Lead from a dropdown box. Searches for the Project Lead's EMPLOYEE ID (contained in a hidden
'column) and compares it with the value in qryPanelSearch.
If .Name = "cboProjectLead" Then
If sWhereClause = " WHERE " Then
sWhereClause = sWhereClause & " (" & BuildCriteria("txtProjectLeadID", dbText, cboProjectLead.Value) & ")"
Else
sWhereClause = sWhereClause & " AND (" & BuildCriteria("txtProjectLeadID", dbText, cboProjectLead.Value) & ")"
End If
End If
'The user can select the Event Coordinator from a dropdown box. Searches for the Event Coordinator's EMPLOYEE ID (contained in a hidden
'column) and compares it with the value in qryPanelSearch.
If .Name = "cboEventCoordinator" Then
If sWhereClause = " WHERE " Then
sWhereClause = sWhereClause & " (" & BuildCriteria("txtEventCoordID", dbText, cboEventCoordinator.Value) & ")"
Else
sWhereClause = sWhereClause & " AND (" & BuildCriteria("txtEventCoordID", dbText, cboEventCoordinator.Value) & ")"
End If
End If
End If
End Select
End With
Next ctl
'Set the form's recordsource equal to the new select statement.
If Not sWhereClause = " WHERE " Then
sSQL = sSQL & sWhereClause
End If
'memoSQL is a temporary field for testing only, used to verify the syntax of the SQL string. This should only be enabled for diagnostic purposes.
memoSQL = sSQL
Set rs = db.OpenRecordset(sSQL, dbReadOnly)
'This command opens the report rptContactSearch and passes the query string sSQL via the Open Arguments parameter.
'The On_Open function in rptContactSearch sets sSQL as the Record Source.
DoCmd.OpenReport "rptPanelSearch", View:=acViewReport, OpenArgs:=sSQL
DoCmd.OpenReport "rptPanelEmails", View:=acViewReport, OpenArgs:=sSQL
End Sub