Results 1 to 16 of 16

Thread: ADO: .Find How to use the like operator correct?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2001
    Location
    Bangkok
    Posts
    969

    ADO: .Find How to use the like operator correct?

    Hi,

    I use rst.Find to find records, but need to use the like operator.
    And I need to use parameters for the fieldnames and searchstrings.
    Anybody can give me a working example?

    This doesn't work:

    strFiendField = "CNR"
    strSearchString = 100

    varReturn = rsCustomer.Find(strFindField & " = " & Trim(strSearchString), , adSearchForward)

    varReturn = rsCustomer.Find(strFindField & " LIKE " & Trim(strSearchString), , adSearchForward)


    Franky

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. varReturn = rsCustomer.Find(strFindField & " = '" & Trim(strSearchString) & "'", , adSearchForward)
    2.  
    3. varReturn = rsCustomer.Find(strFindField & " LIKE '" & Trim(strSearchString) & "%'", , adSearchForward)
    -= a peet post =-

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2001
    Location
    Bangkok
    Posts
    969

    Hi Peet

    thanks for your reply ....... but unfortunatly it give the same error message:

    Compile Error: Expected Function or Variable


    The Errorcode complain about the word FIND, but if I use it without all the parameters then it works.

    rsCustomer.Find strFindField & " = '" & Trim(strSearchString) & "'"


    Your code doesn't work unfortunatly ...... sniff

    varReturn = rsCustomer.Find(strFindField & " = '" & Trim(strSearchString) & "'", , adSearchForward)


    Any idea why?

    Franky

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Hi there Franky,

    just to make sure I understand this,

    you have a recordset rsCustomer, this recset is pulled from a table that have a field named CNR

    you want to find the next record that has the value 100 or "100" ?

    is this a numeric field or a text field ?
    -= a peet post =-

  5. #5
    vbCowboy
    Guest
    Try this:

    VB Code:
    1. rsCustomer.Find strFindField & " = '" & Trim(strSearchString) & "'"
    2.  
    3. varReturn = rsCustomer!CNR

    I don't know if a variable can be subsituted for a field name in the second line, you may need the literal field.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2001
    Location
    Bangkok
    Posts
    969

    H peet and cowboy (John Wayne? hehe)

    Ok, I explain what I want to do.
    I created a form in which the use can select a field that he want to search, he can select for search backward of forward and to search with the like operator or not. I want to use this parameters then to create the .find

    I read in MSDn that it is possible to use .Find with the parameters as I want. Cowboys example works for sure, but how can I search then backwards? The Like Paramater works well if I don't use () for the .Find
    Let's give a example:
    I want to find Mr. Clinton and Mr. Clanton. I use now Cl?nton and the like, I want to search backwards.

    About the question from peet about numeric fields and stringfields I am aware of it and will use it as needed.

    I am happy that you try to help me, but it seems that until now nobody here used parameters for the ADO.find
    If we find a solution for it then I will post the form here (because it is a very nice form) including the code, so that everybody can use a selfcreated .find


    Thanks again and I wait for replys, hehe

    Franky

  7. #7
    Lively Member Wally Pipp's Avatar
    Join Date
    Jan 2002
    Location
    Carnivàle
    Posts
    79
    rscomMyrecSet.Filter = MyFindField & " Like '*" & trim(strSearchstring) & "*'"

    The stars act as wildcards so it's up to you to see whether you need wildcards in front or after your string
    A post brought to you by the Grim Reaper Appreciation Society™

    "Buy your lifetime subscription now and save on your coffin"

  8. #8
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    a comment to that... I think you have to use % when dealing with ado... I think the * will not work ...
    -= a peet post =-

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2001
    Location
    Bangkok
    Posts
    969

    Hi Wally and peet

    Wally, I don't want to use a filter for my records. I want really to use the .Find

    peet, I will keep it in mind with the %


    ..... but the question still remain the same .....


    Franky

  10. #10
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    hehe... think I totally lost it now...

    but hey.. I'm learning how to use Find

    I created this little sample.
    it works...

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim cnn As New ADODB.Connection
    3.     Dim rs As New ADODB.Recordset
    4.     Dim SQL As String
    5.     Dim iRes As Integer
    6.     SQL = "Select * From Customer"
    7. '    SQL = "Customer"
    8.    
    9.     cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb"
    10.     rs.Open SQL, cnn, adOpenDynamic
    11.    
    12.     Do
    13.         rs.Find "CNR = '100'", , adSearchForward
    14.         If rs.EOF Then
    15.             iRes = MsgBox("record not found Start from the beginning?", vbYesNo)
    16.             If iRes = vbNo Then
    17.                 Exit Sub
    18.             Else
    19.                 rs.MoveFirst
    20.             End If
    21.         Else
    22.             MsgBox rs("Description")
    23.             rs.MoveNext
    24.         End If
    25.     Loop
    26. End Sub
    -= a peet post =-

  11. #11
    Lively Member Wally Pipp's Avatar
    Join Date
    Jan 2002
    Location
    Carnivàle
    Posts
    79
    Peet, it works

    If you're going to look for values that answer to criteria then why not fill a recordset with an SQL-statement or filter an existing recordset with the criteria ?

    It's pretty much the same result really. But if you insist on using find, perhaps the wildcards will work there also.

    Try it out some time
    A post brought to you by the Grim Reaper Appreciation Society™

    "Buy your lifetime subscription now and save on your coffin"

  12. #12
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by Wally Pipp
    Peet, it works

    If you're going to look for values that answer to criteria then why not fill a recordset with an SQL-statement or filter an existing recordset with the criteria ?

    It's pretty much the same result really. But if you insist on using find, perhaps the wildcards will work there also.

    Try it out some time
    Hey Wally my favorite hare (*looks around hoping BonkerG does not see that* )

    I found use of Find, when searching a list of items visible to the
    user (grid) ...

    I used some sql to show them a group of items. Some times
    these groups are a bit big. then I use Find to make it possible for
    the user to search in the group of items found. I think that makes
    sense.

    When one item found, it moves to that item in the list... if thats
    not the item the user was looking for, he/she hits the find button
    once more, and it moves to the next.

    It makes it a better userinterface I think.

    Only used this with DAO though... now also in ADO. the difference
    is that FindNex, FindPrev have been removed form ADO

    how about you Franky, did that sample I made shed any lights on
    what you are trying to do ?

    If not, you will have to elaborate even more
    -= a peet post =-

  13. #13
    vbCowboy
    Guest
    Also without having to go through all the streching, the Find method has a few options:

    Find (Criteria, SkipRows, SearchDirection, Start)
    VB Code:
    1. rsCustomer.Find strFindField & " = '" & Trim(strSearchString) & "'",,adSearchBackwards
    2.  
    3. varReturn = rsCustomer!CNR

    MSDN ADO Programmer's Reference

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2001
    Location
    Bangkok
    Posts
    969

    Hi peet and cowboy

    I had nochance to try it, because I am since 2 days in Korea. When I am back home in Bangkok I willtry it, or I check it on my Computer. I will use for sure the Do ... loop because it makes my code shorter. I knew that there exist the parameters in find and exactly this was it what i tried to use.

    As you said it is much better to use .Find as Filter or a SQl-statement in many situations because it most times the recordset is populated and the Find/Seek is only to jump to the corect record and not to make the selection smaller.

    I will answer soon with my experience about it.

    Thanks

    Franky

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2001
    Location
    Bangkok
    Posts
    969

    Hi peet and cowboy ... and whoever can help!!!

    I changed the way to the way that you showed here, cowboy.

    The Find = is absolutly under control now. it workse perfect. What is still a problem is the LIKE. How to get a result with the like operator?

    Franky

  16. #16
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    It's been shown already by bugman

    VB Code:
    1. rsCustomer.find strfindfield & " LIKE '%" & Trim(strSearchString) & "%'",,adSearchBackwards


    explanation

    LIKE 'F%' --> gets all records starting with F
    LIKE '%F' --> ending with F
    LIKE '%Finger%' --> with a Finger in the middle. A middle Finger.

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