Option Compare Database
Option Explicit
Sub SearchSQL()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim qdf As DAO.QueryDef
Dim rec As DAO.Recordset
Dim frm As Access.Form
Dim rpt As Access.Report
Dim obj As Access.AccessObject
Dim ctl As Access.Control
Dim strSearch As String
Dim blnObjClose As Boolean
Const cstrTblName As String = "ztblSQLSearch"
Const clngCancel As Long = vbObjectError Or 777&
On Error GoTo ErrHandler
strSearch = InputBox("Enter the search string", "Search All SQL")
If LenB(strSearch) = 0 Then
Err.Raise clngCancel, , "No search text entered"
End If
DoCmd.Hourglass True
Application.Echo False
Set db = CurrentDb
If CreateListTable(db, cstrTblName) = False Then
Err.Raise vbObjectError + 1001, , "Search results table already exists"
End If
db.TableDefs.Refresh
Set tdf = db.TableDefs(cstrTblName)
Set rec = tdf.OpenRecordset(dbOpenDynaset, dbAppendOnly)
For Each qdf In db.QueryDefs
If AscW(qdf.name) <> 126& Then
If InStrB(1, UCase(qdf.SQL), UCase(strSearch), vbDatabaseCompare) Then
AddRecord rec, qdf.DateCreated, qdf.name, "Query", Null, qdf.SQL
End If
End If
Next qdf
For Each obj In Access.CurrentProject.AllForms
blnObjClose = Not obj.IsLoaded
If blnObjClose Then DoCmd.OpenForm obj.name, acDesign, , , acFormReadOnly, acHidden
Set frm = Access.Forms(obj.name)
If InStrB(UCase(frm.RecordSource), UCase(strSearch)) Then
AddRecord rec, obj.DateCreated, frm.name, "Form", Null, frm.RecordSource
End If
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acComboBox, acListBox
If InStrB(UCase(ctl.RowSource), UCase(strSearch)) Then
AddRecord rec, obj.DateCreated, frm.name, "Form", ctl.name, ctl.RowSource
End If
End Select
Next ctl
If blnObjClose Then DoCmd.Close acForm, frm.name, acSaveNo
Next obj
For Each obj In Access.CurrentProject.AllReports
blnObjClose = Not obj.IsLoaded
If blnObjClose Then DoCmd.OpenReport obj.name, acDesign, , , acHidden
Set rpt = Access.Reports(obj.name)
If InStrB(UCase(rpt.RecordSource), UCase(strSearch)) Then
AddRecord rec, obj.DateCreated, rpt.name, "Report", Null, rpt.RecordSource
End If
For Each ctl In rpt.Controls
Select Case ctl.ControlType
Case acComboBox, acListBox
If InStrB(UCase(ctl.RowSource), UCase(strSearch)) Then
AddRecord rec, obj.DateCreated, rpt.name, "Report", ctl.name, ctl.RowSource
End If
End Select
Next ctl
If blnObjClose Then DoCmd.Close acReport, rpt.name, acSaveNo
Next obj
DoCmd.OpenTable cstrTblName
ExitHere:
On Error Resume Next
rec.Close
Set rec = Nothing
Set qdf = Nothing
Set tdf = Nothing
Set obj = Nothing
Set frm = Nothing
Set rpt = Nothing
Set db = Nothing
DoCmd.Hourglass False
Application.Echo True
Exit Sub
ErrHandler:
Select Case Err.Number
Case clngCancel
Case Else
MsgBox Err.Description, vbCritical, "Unexpected Error (SearchSQL: " & Err.Number & ")"
End Select
Resume ExitHere
Resume
End Sub
Function CreateListTable(ByRef db As DAO.Database, ByVal strTable As String) As _
Boolean
Dim tdfCreate As DAO.TableDef
On Error Resume Next
Set tdfCreate = db.TableDefs(strTable)
If Err.Number = 0 Then
If vbYes = MsgBox("There is an existing table named " & _
UCase$(strTable) & "." & vbCrLf & vbCrLf & _
"Did you want to clear all the data and continue?", vbQuestion + _
vbYesNo + vbDefaultButton2, "Search Results Table Exists!") Then
DoCmd.DeleteObject acTable, strTable
Else
Exit Function
End If
End If
Set tdfCreate = db.CreateTableDef(strTable)
With tdfCreate
.Fields.Append .CreateField("DateCreated", dbDate)
.Fields.Append .CreateField("ObjectName", dbText)
.Fields.Append .CreateField("ObjectType", dbText)
.Fields.Append .CreateField("ControlName", dbText)
.Fields.Append .CreateField("SQL", dbMemo)
End With
db.TableDefs.Append tdfCreate
CreateListTable = True
Set tdfCreate = Nothing
End Function
Private Sub AddRecord(ByRef rec As DAO.Recordset, ByVal dtmCreate As Date, _
ByVal strName As String, ByVal strType As String, _
ByVal varControl As Variant, ByVal strSQL As String)
With rec
.AddNew
.Fields(0&).Value = dtmCreate
.Fields(1&).Value = strName
.Fields(2&).Value = strType
.Fields(3&).Value = varControl
.Fields(4&).Value = strSQL
.Update
End With
End Sub
|