Results 1 to 5 of 5

Thread: Search for text on form?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    2

    Search for text on form?

    I have a simple program for printing off documents for work, basically the names of all my documents in different forms. I would like to put a "Search" box in the forms so if I type in the name of a label the label will turn bold or highlight or something so I can find it easier in my "ocean" of labels LOL. Any help and sample code will be greatly appreciated!

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Search for text on form?

    Welcome to Forums!

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    2

    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.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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
    Last edited by Edgemeal; May 30th, 2008 at 12:24 PM.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width