Function FindString(searchTerm As String) As String
Dim folderPath As String, line As String, tempFile As String
Dim f As AccessObject, r As AccessObject
Dim output As String, counter As Long, fileNum As Integer
Dim found As Boolean
folderPath = CurrentProject.Path
output = "Results found:"
For Each f In CurrentProject.AllForms
counter = counter + 1
tempFile = folderPath & "\" & f.FullName & ".tmp"
found = False
On Error GoTo CleanupForm
SaveAsText acForm, f.FullName, tempFile
fileNum = FreeFile()
Open tempFile For Input As #fileNum
Do While Not EOF(fileNum) And Not found
Line Input #fileNum, line
If InStr(1, line, searchTerm, vbTextCompare) > 0 Then
output = output & vbCrLf & f.Name
found = True
End If
Loop
CleanupForm:
If fileNum > 0 Then Close #fileNum: fileNum = 0
If Dir(tempFile) <> "" Then Kill tempFile
' DoEvents @ every 5th object
If counter Mod 5 = 0 Then DoEvents
Next f
For Each r In CurrentProject.AllReports
counter = counter + 1
tempFile = folderPath & "\" & r.FullName & ".tmp"
found = False
On Error GoTo CleanupReport
SaveAsText acReport, r.FullName, tempFile
fileNum = FreeFile()
Open tempFile For Input As #fileNum
Do While Not EOF(fileNum) And Not found
Line Input #fileNum, line
If InStr(1, line, searchTerm, vbTextCompare) > 0 Then
output = output & vbCrLf & r.Name
found = True
End If
Loop
CleanupReport:
If fileNum > 0 Then Close #fileNum: fileNum = 0
If Dir(tempFile) <> "" Then Kill tempFile
' DoEvents every 5th object
If counter Mod 5 = 0 Then DoEvents
Next r
MsgBox output & vbCrLf & "(" & counter & " objects read)" ' Optional
FindString = output
End Function