Private Sub cmdSearch_Click()
On Error GoTo Err_Handler
'This allows for updating of LstSearchText if accessed via Back/Next buttons
If NextFlag = True Or BackFlag = True Then GoTo FlagStart:
intScreenCount = 1
SetFormConditions
Me.Requery
Me.txtDummy.SetFocus
FlagStart:
If Nz(Me.txtSearch, "") = "" Then
Me.LstSearchText.visible = False
Me.lblSearchHeader.Caption = "No search topic entered"
Exit Sub
End If
'=========================================
'CR v4691
'This section builds a list of items from each word in the search box
'The search text is split into separate words and a WHERE clause is built up from each word
'The code looks for the search words in any of the fields Outline, SearchText, Details
'Only active topics are listed
'End users can only see available topics (depends on program features set in Change Constants form; SDA managers can see all topics)
'The topic list is then restricted further depending on user job role
'Its complicated but it works
'clear data for all previous searches
strSearch = ""
strWordList = ""
strOutline = ""
strSearchText = ""
strDetails = ""
strWhere = ""
'================================
'This filter section is used repeatedly as code builds up
'code to limit access according to job role
If GetSDAManagerStatus = True Then
strSDAManager = "" 'topics not restricted
Else 'hide topics where SDAManager field = True
strSDAManager = " AND ((tblSDAHelp.SDAManager)=False)"
End If
If GetCPManagerStatus = True Then
strCPManager = "" 'topics not restricted
Else 'hide topics where CPManager field = True
strCPManager = " AND ((tblSDAHelp.CPManager)=False)"
End If
If GetPastoralManagerStatus = True Then
strPastoralManager = "" 'topics not restricted
Else 'hide topics where Pastoral field = True
strPastoralManager = " AND ((tblSDAHelp.Pastoral)=False)"
End If
If GetCalendarEditorStatus = True Then
strCalendarEditor = "" 'topics not restricted
Else 'hide topics where Pastoral field = True
strCalendarEditor = " AND ((tblSDAHelp.CalendarEditor)=False)"
End If
'code to limit to available topics (except for SDAManagers)
If GetSDAManagerStatus = True Then
strAvailable = "" 'topics not restricted
Else 'hide unavailable topics
strAvailable = " AND ((tblSDAHelp.Available)=True)"
End If
'code to limit to active topics
strActive = " AND ((tblSDAHelp.Active)=True))" 'extra closing bracket as it forms end of Where clause
'=========================================
'start the search
strSearch = Me.txtSearch
Me.txtSearchHeader = strSearch
'===================================
'filter the search text to remove words like 'and', 'or', 'the'
'as these will occur throughout Details section and would give false results
'CR v4699 - put space before & after each word to ensure search only done for whole word
If InStr(1, strSearch, " and ") > 0 Then
Do Until InStr(1, strSearch, " and ") = 0
strSearch = Left(strSearch, InStr(1, strSearch, " and ") - 1) & Mid(strSearch, InStr(1, strSearch, " and ") + 4)
Loop
End If
If InStr(1, strSearch, " & ") > 0 Then
Do Until InStr(1, strSearch, " & ") = 0
strSearch = Left(strSearch, InStr(1, strSearch, " & ") - 1) & Mid(strSearch, InStr(1, strSearch, " & ") + 2)
Loop
End If
If InStr(1, strSearch, " the ") > 0 Then
Do Until InStr(1, strSearch, " the ") = 0
strSearch = Left(strSearch, InStr(1, strSearch, " the ") - 1) & Mid(strSearch, InStr(1, strSearch, " the ") + 4)
Loop
End If
If InStr(1, strSearch, " or ") > 0 Then
Do Until InStr(1, strSearch, " or ") = 0
strSearch = Left(strSearch, InStr(1, strSearch, " or ") - 1) & Mid(strSearch, InStr(1, strSearch, " or ") + 3)
Loop
End If
If InStr(1, strSearch, " of ") > 0 Then
Do Until InStr(1, strSearch, " of ") = 0
strSearch = Left(strSearch, InStr(1, strSearch, " of ") - 1) & Mid(strSearch, InStr(1, strSearch, " of ") + 3)
Loop
End If
If InStr(1, strSearch, " a ") > 0 Then
Do Until InStr(1, strSearch, " a ") = 0
strSearch = Left(strSearch, InStr(1, strSearch, " a ") - 1) & Mid(strSearch, InStr(1, strSearch, " a ") + 2)
Loop
End If
If InStr(1, strSearch, " an ") > 0 Then
Do Until InStr(1, strSearch, " an ") = 0
strSearch = Left(strSearch, InStr(1, strSearch, " an ") - 1) & Mid(strSearch, InStr(1, strSearch, " an ") + 3)
Loop
End If
'Debug.Print strSearch
If strSearch = "" Then
Me.txtSearchHeader = strSearch
Me.LstSearchText.visible = False
Me.lblSearchHeader.Caption = "Common words like 'and', 'or' were removed from the search results" & _
" as these can give false results. Please omit these words from your search"
Me.lblSearchHeader.ForeColor = vbRed
Exit Sub
End If
'restore normal colour
Me.lblSearchHeader.ForeColor = ColDarkBlue
'========================================
'split the search text into separate words and manage each separately
strWordList = strSearch
Do Until strWordList = ""
If strWordList > "" Then
If InStr(1, strWordList, " ") = 0 Then 'only 1 word in list
GoTo Done 'this deals with final (or only) word in list
Else
'search each word separately
strSearch = Left(strWordList, InStr(1, strWordList, " ") - 1)
strWordList = Mid(strWordList, InStr(1, strWordList, " ") + 1)
'=====================================================
'build up strWhere clause
'If there is more than one word in search list the code loops to search each word separately adding filters from filter section
If strOutline = "" Then
strOutline = "(((tblSDAHelp.Outline) Like '*' & '" & strSearch & "' & '*')"
Else
strOutline = strOutline & " OR (((tblSDAHelp.Outline) Like '*' & '" & strSearch & "' & '*')"
End If
If strSearchText = "" Then
strSearchText = "(((tblSDAHelp.SearchText) Like '*' & '" & strSearch & "' & '*')"
Else
strSearchText = strSearchText & " OR (((tblSDAHelp.SearchText) Like '*' & '" & strSearch & "' & '*')"
End If
If strDetails = "" Then
strDetails = "(((tblSDAHelp.Details) Like '*' & '" & strSearch & "' & '*') AND ((tblSDAHelp.Details) Not Like '*Dummy text*')"
Else
strDetails = strDetails & " OR (((tblSDAHelp.Details) Like '*' & '" & strSearch & "' & '*') AND ((tblSDAHelp.Details) Not Like '*Dummy text*')"
End If
End If
End If
strOutline = strOutline & strSDAManager & strCPManager & strCalendarEditor & strPastoralManager & strAvailable & strActive
strSearchText = strSearchText & strSDAManager & strCPManager & strCalendarEditor & strPastoralManager & strAvailable & strActive
strDetails = strDetails & strSDAManager & strCPManager & strCalendarEditor & strPastoralManager & strAvailable & strActive
Loop
Done:
'now add a WHERE clause for the final word in the list - modified v4690
If strOutline = "" Then
strOutline = "(((tblSDAHelp.Outline) Like '*' & '" & strWordList & "' & '*')"
Else
strOutline = strOutline & " OR (((tblSDAHelp.Outline) Like '*' & '" & strWordList & "' & '*')"
End If
If strSearchText = "" Then
strSearchText = "(((tblSDAHelp.SearchText) Like '*' & '" & strWordList & "' & '*')"
Else
strSearchText = strSearchText & " OR (((tblSDAHelp.SearchText) Like '*' & '" & strWordList & "' & '*')"
End If
If strDetails = "" Then
strDetails = "(((tblSDAHelp.Details) Like '*' & '" & strWordList & "' & '*') AND ((tblSDAHelp.Details) Not Like '*Dummy text*')"
Else
strDetails = strDetails & " OR (((tblSDAHelp.Details) Like '*' & '" & strWordList & "' & '*') AND ((tblSDAHelp.Details) Not Like '*Dummy text*')"
End If
strOutline = strOutline & strSDAManager & strCPManager & strCalendarEditor & strPastoralManager & strAvailable & strActive
strSearchText = strSearchText & strSDAManager & strCPManager & strCalendarEditor & strPastoralManager & strAvailable & strActive
strDetails = strDetails & strSDAManager & strCPManager & strCalendarEditor & strPastoralManager & strAvailable & strActive
strSelect = "SELECT tblSDAHelp.ID, tblSDAHelp.Outline, tblSDAHelp.SearchText, tblSDAHelp.Details FROM tblSDAHelp"
strOrderBy = " ORDER BY tblSDAHelp.SearchText;"
strWhere = " WHERE " & strOutline & " OR " & strSearchText & " OR " & strDetails
strSQL1 = strSelect & strWhere & strOrderBy
'Debug.Print strSQL1
Me.LstSearchText.RowSource = strSQL1
'=====================================================
Me.LstSearchText.ColumnWidths = "0cm;0cm;4cm;0cm;0cm"
If Me.LstSearchText.ListCount > 1 Then
Me.LstSearchText.visible = True
Me.LstSearchText = ""
Me.lblSearchHeader.Caption = Me.LstSearchText.ListCount & " results for: "
Me.LblTryBrowse.visible = False
ElseIf Me.LstSearchText.ListCount = 1 Then
Me.LstSearchText.visible = True
Me.LstSearchText = ""
Me.lblSearchHeader.Caption = Me.LstSearchText.ListCount & " result for: "
Me.LblTryBrowse.visible = False
Else
Me.LstSearchText.visible = False
Me.lblSearchHeader.Caption = "No results for: "
Me.LblTryBrowse.visible = True
End If
Me.lblBrowse.Caption = ""
If NextFlag = True Or BackFlag = True Then Exit Sub
'else add user log record to tblSDAHelpUserHistoryTEMP
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO tblSDAHelpUserHistoryTEMP ( IntScreenCount, ScreenType, TeacherID, EventTime, SDAHelpID, Outline, SearchText, MoreDetail, HelpFile )" & _
" SELECT DISTINCTROW 1 AS IntScreenCount, 'Search Results' AS ScreenType, GetLoggedOnTeacher() AS TeacherID, Now() AS EventTime," & _
" 0 As SDAHelpID, '' AS Outline, '" & strSearch & "' AS SearchText, 0 AS MoreDetail, '' AS HelpFile;"
DoCmd.SetWarnings True
'save initial record ID for later use
Me.txtUserLogID = Nz(DMax("ID", "tblSDAHelpUserHistoryTEMP"), 0)
Exit_Handler:
Exit Sub
Err_Handler:
'create error message & log
strProc = Application.VBE.ActiveCodePane.CodeModule.ProcOfLine(Application.VBE.ActiveCodePane.TopLine, 0)
PopulateErrorLog
Resume Exit_Handler
End Sub