Writing Code to find text in specific field (1 Viewer)

lana

Registered User.
Local time
Tomorrow, 01:43
Joined
Feb 10, 2010
Messages
92
Hi everybody,

Can anybody help me on this one?

I want to design a form to do the search job as the access "Find" form.

For example, in a form 5 fields are displayed. I want to search in a field which the cursor is in there. I need another form which I can type the search string and finds it in that field.

I would appreciate your help.

Cheers
 

MStef

Registered User.
Local time
Today, 22:13
Joined
Oct 28, 2004
Messages
2,251
Hello lana!
As I can understand you, you can do it via FILTER BY FORM, CLEAR GRID, APPLY FILTER, and REMOVE FILTER icons.
Look at attacment (word, zip).
 

Attachments

  • FilterForm.zip
    94.5 KB · Views: 198

lana

Registered User.
Local time
Tomorrow, 01:43
Joined
Feb 10, 2010
Messages
92
Thanks for the reply,

This is no what I need. I would like to design a form exactly like the "find" form which access has. It searches in the field on your form where the cursor is.

Any idea?

Cheers
 

vbaInet

AWF VIP
Local time
Today, 22:13
Joined
Jan 22, 2010
Messages
26,374
If you want to have the same functionality as that of the search feature in Office applications, then you should be looking for an add-in or have expert coding skills.

In any case, here's code from the Access Help files:

Access Developer Reference TextBox.SelStart Property
Show All​
Hide All​

The SelStart property specifies or determines the starting point of the selected text or the position of the insertion point if no text is selected. Read/write Integer. expression.SelStart
expression A variable that represents a TextBox object.
Remarks

The SelStart property uses an Integer in the range 0 to the total number of characters in the text box portion of a combo box.
To set or return this property for a control, the control must have the focus . To move the focus to a control, use the SetFocus method.
Changing the SelStart property cancels the selection, places an insertion point in the text, and sets the SelLength property to 0.
Example

The following example uses two event procedures to search for text specified by a user. The text to search is set in the form's Load event procedure. The Click event procedure for the Find button (which the user clicks to start the search) prompts the user for the text to search for and selects the text in the text box if the search is successful.
Visual Basic for Applications
Code:
Private Sub Form_Load()
    
    Dim ctlTextToSearch As Control
    Set ctlTextToSearch = Forms!Form1!Textbox1
    
    ' SetFocus to text box.
    ctlTextToSearch.SetFocus
    ctlTextToSearch.Text = "This company places large orders twice " & _
                           "a year for garlic, oregano, chilies and cumin."
    Set ctlTextToSearch = Nothing
    
End Sub

Public Sub Find_Click()
    
    Dim strSearch As String
    Dim intWhere As Integer
    Dim ctlTextToSearch As Control
    
    ' Get search string from user.
    With Me!Textbox1
        strSearch = InputBox("Enter text to find:")
        
        ' Find string in text.
        intWhere = InStr(.Value, strSearch)
        If intWhere Then
            ' If found.
            .SetFocus
            .[B]SelStart[/B] = intWhere - 1
            .SelLength = Len(strSearch)
        Else
            ' Notify user.
            MsgBox "String not found."
        End If
    End With
    
End Sub
 

lana

Registered User.
Local time
Tomorrow, 01:43
Joined
Feb 10, 2010
Messages
92
VbaInet, thanks again, but this code searches a string inside a text.

I need to do like "find and replace" form in access. It searches inside a field where the cursor is.

Cheers
 

vbaInet

AWF VIP
Local time
Today, 22:13
Joined
Jan 22, 2010
Messages
26,374
No worries Lana. Try and investigate the InStr(), Mid(), InStrRev() functions to see what you can come up with. Then just post your code and I'll have a look.

If (however) you get stuck or are struggling just come back and give us a post. :)
 

Users who are viewing this thread

Top Bottom