Brian said:
On occasion, I have to rename a query after creating a reference to
it in one ormore ControlSource or DefaultValue of controls (generally
text/combo boxes) on forms. I found a way to look up such a string
when it exists in a RowSource by cycling through the QueryDefs
collection.
How can I cycle through all controls on all forms, looking for a
particular string in the .ControlSource or .DefaultValue property?
Here's a very unpolished and only lightly tested routine for the
purpose. You'll have to fix up any line breaks caused by the
newsreader.
'----- start of code -----
Sub SearchControlSourcesAndDefaultValues(strSought As String)
' Search the ControlSources and DefaultValue properties of all
' controls on all forms for the specified string.
On Error GoTo Err_Handler
Dim db As DAO.Database
Dim doc As DAO.Document
Dim frm As Form
Dim ctl As Control
Dim lngFormCount As Long
Dim lngControlCount As Long
Dim lngFoundCount As Long
Dim lngControlFoundCount As Long
Dim strOpenForm As String
Dim strControlSource As String
Dim strDefaultValue As String
Debug.Print "*** Beginning search ..."
Set db = CurrentDb
For Each doc In db.Containers("Forms").Documents
DoCmd.OpenForm doc.Name, acDesign, WindowMode:=acHidden
strOpenForm = doc.Name
Set frm = Forms(strOpenForm)
With frm
lngFormCount = lngFormCount + 1
lngControlFoundCount = 0
For Each ctl In .Controls
On Error GoTo Err_NoSource
strControlSource = ctl.ControlSource
strDefaultValue = ctl.DefaultValue
On Error GoTo Err_Handler
lngControlCount = lngControlCount + 1
If InStr(strControlSource, strSought) Then
lngFoundCount = lngFoundCount + 1
lngControlFoundCount = lngControlFoundCount + 1
If lngControlFoundCount = 1 Then
Debug.Print "Form " & .Name & " -- string found
in control"
End If
Debug.Print , "Control " & ctl.Name & "
ControlSource: " & strControlSource
End If
If InStr(strDefaultValue, strSought) Then
lngFoundCount = lngFoundCount + 1
lngControlFoundCount = lngControlFoundCount + 1
If lngControlFoundCount = 1 Then
Debug.Print "Form " & .Name & " -- string found
in control"
End If
Debug.Print , "Control " & ctl.Name & "
DefaultValue: " & strDefaultValue
End If
Next_Control:
Next ctl
DoCmd.Close acForm, .Name
strOpenForm = vbNullString
End With
Set frm = Nothing
Next doc
Exit_Point:
On Error Resume Next
Set ctl = Nothing
Set doc = Nothing
Set db = Nothing
If Len(strOpenForm) > 0 Then
DoCmd.Close acForm, strOpenForm, acSaveNo
End If
Debug.Print "*** Searched " & lngFormCount & _
" forms and " & lngControlCount & " controls, found " &
_
lngFoundCount & " occurrences."
Exit Sub
Err_NoSource:
If Err.Number = 438 _
Or Err.Number = 2455 Then
Resume Next_Control
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End Sub
'----- end of code -----