Re: Search for text on form?
Welcome to Forums! :wave:
This quick sample should give some idea about how it can be done:
Code:
Private Sub Command1_Click()
Dim ctl As Control
'loop through controls collection
For Each ctl In Me.Controls
'you are only interested in labels
If TypeOf ctl Is Label Then
'reset font first
ctl.Font.Bold = False
'check if text is matching label's caption
If InStr(1, LCase(ctl.Caption), Trim(Text1.Text)) > 0 Then
ctl.Font.Bold = True
End If
End If
Next ctl
End Sub
Re: Search for text on form?
Thank you very very very much! This code is great, can I get it to work on both forms in the project at once? Right now I have to put it on the two separate forms and search twice.
Re: Search for text on form?
You could make it a public function inside a module so then you can use it on any form.
Code:
Public Sub BoldLabels(frm As Form, Txt As String)
Dim ctl As Control
'loop through controls collection
For Each ctl In frm.Controls
'you are only interested in labels
If TypeOf ctl Is Label Then
'reset font first
ctl.Font.Bold = False
'check if text is matching label's caption
If InStr(1, LCase(ctl.Caption), LCase(Trim(Txt))) > 0 Then
ctl.Font.Bold = True
End If
End If
Next ctl
End Sub
' search form1 & form2
BoldLabels Form1, Text1.Text
BoldLabels Form2, Text1.Text
'search all forms with "Label1" and make bold
Code:
Dim i As Integer
For i = 0 To Forms.Count - 1
BoldLabels Forms(i), "Label1"
Next i
Re: Search for text on form?
Yes, of course but you'd have to change few things:
Code:
'add this to a form (or each form perhaps)
Private Sub Command1_Click()
HighlightLabels Text1.Text
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
HighlightLabels Text1.Text
End If
End Sub
'add this code to a module
Public Sub HighlightLabels(sText As String)
Dim ctl As Control
Dim frm As Form
'loop through each form
For Each frm In Forms
'loop through each control on the current form
For Each ctl In frm.Controls
If TypeOf ctl Is Label Then
'reset font first
ctl.Font.Bold = False
'check if text is matching label's caption
If InStr(1, LCase(ctl.Caption), sText) > 0 Then
ctl.Font.Bold = True
End If
End If
Next ctl
Next frm
End Sub