Link to home
Start Free TrialLog in
Avatar of pdvsa
pdvsaFlag for United States of America

asked on

Find button for memofield txt box

Experts,
What is the code behind a button to find a word or number in a memo field?

thank you
Avatar of rockiroads
rockiroads
Flag of United States of America image

have u tried using instr?

instr(1,"StringToCheck","StringToFind")

will return a long value representing the position of the found text

there is a 4th argument to this and that is how you want the compare

vbUseCompareOption -1 Performs a comparison using the setting of the Option Compare statement.
vbBinaryCompare 0 Performs a binary comparison.
vbTextCompare 1 Performs a textual comparison.
vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.

FindRecord can be used in a macro.   Is this an option?
depends on what you want to do.

If you want to go to the first record that matches it would be something like:

Private Sub cmdFind_Click

    Dim rs as DAO.Recordset

    set rs = me.recordsetclone
    rs.FindFirst "[YourFieldName] LIKE '*" & me.txtFindThis & "*'"
    if rs.nomatch then
        msgbox "not found!"
    else
        me.bookmark = rs.bookmark
    end if

    set rs = nothing

End Sub

VBA option is in Access help:
Find Method (VBA Add-In Object Model)
Searches the active module for a specified string.
Syntax
object.Find(target, startline, startcol, endline, endcol [, wholeword] [, matchcase] [, patternsearch]) As Boolean

The Find syntax has these parts: Part Description object Required. An object expression that evaluates to an object in the Applies To list. target Required. A String containing the text or pattern you want to find. startline Required. A Long specifying the line at which you want to start the search; will be set to the line of the match if one is found. The first line is number 1. startcol Required. A Long specifying the column at which you want to start the search; will be set to the column containing the match if one is found. The first column is 1. endline Required. A Long specifying the last line of the match if one is found. The last line may be specified as –1. endcol Required. A Long specifying the last line of the match if one is found. The last column may be designated as –1. wholeword Optional. A Boolean value specifying whether to only match whole words. If True, only matches whole words. False is the default. matchcase Optional. A Boolean value specifying whether to match case. If True, the search is case sensitive. False is the default. patternsearch Optional. A Boolean value specifying whether or not the target string is a regular expression pattern. If True, the target string is a regular expression pattern. False is the default.
Remarks
Find returns True if a match is found and False if a match isn't found.
The matchcase and patternsearch arguments are mutually exclusive; if both arguments are passed as True, an error occurs.
The content of the Find dialog box isn't affected by the Find method.
The specified range of lines and columns is inclusive, so a search can find the pattern on the specified last line if endcol is supplied as either –1 or the length of the line.ofm
© 2010 Microsoft Corporation. All rights reserved.
if it is a continuous form, and you want to filter for that value then it would look something like:

Private Sub cmdFilter_Click

    me.Filter = "[YourFieldName] Like '*" & me.txtFilter & "*'"
    me.FilterOn = true

End sub

If you need a more complex search functionality, check out my article on Complex Text Filters
Avatar of pdvsa

ASKER

I am not trying to find a record but just a piece of text or a number on the memo field.  Much like you would find text on a word doc.  

rocki:  I dont know anything about instr

fyed:  
me.Filter = "[YourFieldName] Like '*" & me.txtFilter & "*'"
me.FilterOn = true
I think this filters records...

knowing this, does it change anything?  I dont have the time to read and  try to absorb rigth now.

INSTR is used to search a string within a string
Avatar of pdvsa

ASKER

rocki:  does it give the option for user to enter text to find?  I would need soem kind of msbox.  
No point intended but I'd back rocki on this. Instr is the way to go
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> rocki:  does it give the option for user to enter text to find?  I would need soem kind of msbox.  

if on a form you can create a textbox on there and a find next to it

so on your form

txtMemo
txtSearch
cmdSearch


Now I would add some code to find it and select but cap has done some code for you already so no point in duplicating

Search on txtSearch on the click event of cmdSearch
Avatar of pdvsa

ASKER

Instead of a box txtsearch i am more interested in having a box appear.   I thk a txtSearch field will make form confusing.  Not sure if caps code addresses this.  

Can we get a msgbox to appear much like you do in qey design [Enter search term]

can we do this?
Avatar of pdvsa

ASKER

Instead of a box txtsearch i am more interested in having a box appear.   I thk a txtSearch field will make form confusing.  Not sure if caps code addresses this.  

Can we get a msgbox to appear much like you do in qey design [Enter search term]

can we do this?
this line
sWord = InputBox("Type word to find")

will prompt you to enter the word.. you can change it if you like to

sWord = InputBox("Enter Search term")
Avatar of pdvsa

ASKER

Ok got it.   Thank you.
Avatar of pdvsa

ASKER

cap:  I think that sWord has to be dimmed?  I get an error when compile "variable not defined"

Avatar of pdvsa

ASKER

hello...can someone let me know the correct way to dim the sWord (if that is the issue...ref above email...variable not defined)
dim sWord as string
Avatar of pdvsa

ASKER

Cap, it does not seem to find the text although it highlights something but just not the text that was input in the box. I dont know if it has something to do with the textbox being format of Rich Text.  Just wondering if this is something easy to correct.

Private Sub cmdFindWord_Click()

    Dim sNote As String, j
    Dim sWord As String
   
sWord = InputBox("Type word to find")
If Len(Trim(sWord)) > 0 Then

    sNote = Me.txtNotes
   
    j = InStr(sNote, sWord)
   
End If
If j <> 0 Then
   
    Me.txtNotes.SetFocus
    Me.txtNotes.SelStart = j - 1
    Me.txtNotes.SelLength = Len(sWord)
    Else
    MsgBox "Word " & Chr(34) & sWord & Chr(34) & " not found!", vbQuestion, "WORD SEARCH"
End If

End Sub
try the codes with a textbox not formatted as RichText, see if it will make a difference.
Avatar of pdvsa

ASKER

Ok will try that...thx
Avatar of pdvsa

ASKER

Cap, it was rich text issue.  After switching it from Rich Text to Standard it worked.  I really wanted to keep it as rich textg though.  thank you sir...
Actually, you can keep the RichText formatting, all you have to do is change this line:

j = InStr(sNote, sWord)

to:

j = InStr(PlainText(sNote), sWord)
Avatar of pdvsa

ASKER

fyed, that worked!  It is not exactly like word though as it highlights sortof much to the right of the word but it is close enough.  Thank you so much!
Glad it helped.

Not sure what you mean by "sort of much to the right of the word".  In the sample database I created, it highlighted the exact word I was searching for.
Avatar of pdvsa

ASKER

Maybe it is because of different text sizes, Diff fonts netween the words in the memo text box.  I have a lot of data in the memo box too... Emails copied and pasted, which accounts for the many diff fonts .  I did not checj out your db yet.  
Avatar of pdvsa

ASKER

Cap or Fyed:  , I am not sure if you are still monitoring.  I have a follow up.  Is it possible to add a "Find Next" code to Caps "Find Code" already implemented?  I see that I do not have this option to "Find Next".  It would be similar to the "Find Next" in MSWord.  I dont know if it is a simple modification.  


Thank you...
Lookup FindNext in Access Help.  Would probably need another command button and code associated with it.