Results 1 to 17 of 17

Thread: ADO - FIND Statement

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    148

    ADO - FIND Statement

    Howdy!

    OK.. Here's one I've never tried!

    Using the FIND Statement with 1 field works fine, but can you it to find 2 Fields????


    This works:

    Lastname = "DOE"
    Criteria = "Last_Name = '" & Lastname & "'"
    adoPrimaryRS.MoveFirst
    adoPrimaryRS.Find Criteria, , adSearchForward, 1

    This doesn't:

    Lastname = "DOE"
    FirstName = "JOHN"
    Criteria = "Last_Name = '" & Lastname & "'" & " AND First_Name = '" & FirstName & "'"
    adoPrimaryRS.MoveFirst
    adoPrimaryRS.Find Criteria, , adSearchForward, 1

    HELP!!!!! Thanx!

  2. #2
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Hrmmm...never tried the ADO find but try.....

    Criteria = "Last_Name = '" & Lastname & "' AND First_Name = '" & FirstName & "'"

    J.

  3. #3
    Fanatic Member Gary.Lowe's Avatar
    Join Date
    May 2000
    Location
    In my sphere of influence
    Posts
    621
    Hyme

    Use the ADO seek method

    MSDN

    VB Code:
    1. Sub ADOSeekRecord()
    2.  
    3.    Dim cnn As New ADODB.Connection
    4.    Dim rst As New ADODB.Recordset
    5.  
    6.    ' Open the connection
    7.    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    8.       "Data Source=.\NorthWind.mdb;"
    9.  
    10.    ' Open the recordset
    11.    rst.Open "Order Details", cnn, adOpenKeyset, adLockReadOnly, _
    12.       adCmdTableDirect
    13.  
    14.    ' Select the index used to order the data in the recordset
    15.    rst.Index = "PrimaryKey"
    16.  
    17.    ' Find the order where OrderId = 10255 and ProductId = 16
    18.    rst.Seek Array(10255, 16), adSeekFirstEQ
    19.  
    20.    ' If a match is found print the quantity of the order
    21.    If Not rst.EOF Then
    22.    Debug.Print rst.Fields("Quantity").Value
    23.    End If
    24.  
    25.    ' Close the recordset
    26.    rst.Close
    27.  
    28. End Sub

    Because Seek is based on an index, it is important to specify an index before searching. In the previous example, this is not strictly necessary because Microsoft Jet will use the primary key if an index is not specified.

    In the ADO example, the Visual Basic for Applications Array function is used when specifying a value for more than one column as part of the KeyValues parameter. If only one value is specified, it is not necessary to use the Array function.

    As with the Find method, use the NoMatch property with DAO to determine whether a matching record was found. Use the BOF and EOF properties as appropriate with ADO.

    The Seek method will work correctly only for Microsoft Jet 4.0 databases. It will fail with a run-time error for all earlier formats, even if you use the Microsoft Jet 4.0 database engine to open the database. This will cause a problem if the application is written to support older database formats. If so, use the Supports method of the Recordset object to determine whether the Seek method is available for the open Recordset. However, if all client applications use the newer format, this check is unnecessary. Use either Microsoft Access 2000 or the CompactDatabase method to convert older databases to the newer format.
    Gary Lowe
    VB6 (Enterprise) SP5
    ADO 2.6
    SQL Server 7 SP3

    OK I know my spelling and grammer is crap so don't quote me on it!

    To err is human to take the P! is only natural !!

    Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip


  4. #4
    New Member
    Join Date
    Sep 2001
    Posts
    14
    I wouldn't bother messing around with the .Find method. Here's
    how I would approach this problem.

    Dim qry As QueryDef
    Dim rs As Recordset
    Dim sSQL As String

    sSQL = "SELECT tblUsers.[Last_Name], tblUsers.[First_Name] " & _
    "FROM tblUsers WHERE (tblUsers.[Last_Name])=""DOE"") " & _
    "AND tblUsers.[First_Name]=""JOHN""));"

    Set qry = CurrentDb.CreateQueryDef("", sSQL)
    Set rs = qry.OpenRecordset

    If rs.RecordCount > 0 Then
    ----- MsgBox "FoundRecord"
    Else
    ----- MsgBox "No Record Found"
    End If

  5. #5
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    I agree with the above 2 guys about what to use (hence my lack of knowledge of ADO Find), was just trying to fix the find problem :P

    But in the above example I would test for EOF rather than use recordcount, because I find .recordcount is a bit....dodgy...

    J.

  6. #6
    Fanatic Member Gary.Lowe's Avatar
    Join Date
    May 2000
    Location
    In my sphere of influence
    Posts
    621
    The only quarms I would have with kokamo's code is that I think Hyme is using ADO rather than DAO
    Gary Lowe
    VB6 (Enterprise) SP5
    ADO 2.6
    SQL Server 7 SP3

    OK I know my spelling and grammer is crap so don't quote me on it!

    To err is human to take the P! is only natural !!

    Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip


  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    148
    YUP!

  8. #8
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Heh, that he is Gary. Can be changed a bit tho....

    POB!!!!

    J.

  9. #9
    Fanatic Member Gary.Lowe's Avatar
    Join Date
    May 2000
    Location
    In my sphere of influence
    Posts
    621
    Gary Lowe
    VB6 (Enterprise) SP5
    ADO 2.6
    SQL Server 7 SP3

    OK I know my spelling and grammer is crap so don't quote me on it!

    To err is human to take the P! is only natural !!

    Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip


  10. #10
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250
    POB!!!!
    I knew I recognised that face from somewhere!

    Takes me back too many yrs that..

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>>>>>>>
    Follow the string!!!
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    148

    Talking

    OK!
    What is POB???

  12. #12
    Fanatic Member Gary.Lowe's Avatar
    Join Date
    May 2000
    Location
    In my sphere of influence
    Posts
    621
    Originally posted by Lee_S

    I knew I recognised that face from somewhere!

    Takes me back too many yrs that..

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>>>>>>>
    Follow the string!!!
    Ha Haa

    Now that was entertainment...

    Ahhh gone are the days of simple things please simple minds eh?
    Gary Lowe
    VB6 (Enterprise) SP5
    ADO 2.6
    SQL Server 7 SP3

    OK I know my spelling and grammer is crap so don't quote me on it!

    To err is human to take the P! is only natural !!

    Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip


  13. #13
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250
    -Insert Brian Conely voice-

    Its a puuppeeeetttt!

    Well, sort of. Very old kids tv proggie. Gary's avatar is a piccie of Pob!!
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    148
    Thanx, now I know!

  15. #15
    Fanatic Member Gary.Lowe's Avatar
    Join Date
    May 2000
    Location
    In my sphere of influence
    Posts
    621
    Hyme

    Either your too young or not from the UK

    Anyway

    did any of the above stuff help you out with your problem
    Gary Lowe
    VB6 (Enterprise) SP5
    ADO 2.6
    SQL Server 7 SP3

    OK I know my spelling and grammer is crap so don't quote me on it!

    To err is human to take the P! is only natural !!

    Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip


  16. #16

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    148
    I'm 28 and from Canada - I don't know if I qualify!

    While I was waiting for a response. I came up with this.
    It does a double check (so, it's not built for optimum performance)
    But, for the one-time I need to use it, it worked

    Here it is:

    Do While Not adoSecondaryRS.EOF

    TempName = adoSecondaryRS!Name
    TempName2 = InStr(1, TempName, ",")

    Lastname = Mid$(TempName, 1, TempName2 - 1)
    FirstName = Mid$(TempName, TempName2 + 1, 3)
    Criteria = "Last_Name = '" & Lastname & "'"

    adoPrimaryRS.MoveFirst
    adoPrimaryRS.Find Criteria, , adSearchForward, 1

    Do Until adoPrimaryRS.EOF = True
    If Left$(adoPrimaryRS!First_Name, 3) = FirstName Then
    adoPrimaryRS!Employee_ID = adoSecondaryRS!EmployeeID
    adoPrimaryRS.Update
    adoPrimaryRS.MoveLast
    Else
    adoPrimaryRS.MoveNext
    End If
    Loop
    adoSecondaryRS.MoveNext

    Loop

    This is built for finding firstname & lastname (in two seperate fields in 1 table and ONLY in 1 field in the 2nd table) Then adding an employee number to the table.

  17. #17
    Fanatic Member Gary.Lowe's Avatar
    Join Date
    May 2000
    Location
    In my sphere of influence
    Posts
    621
    Old enough

    but wrong country.

    Good to see you got it sorted out anyway.
    Gary Lowe
    VB6 (Enterprise) SP5
    ADO 2.6
    SQL Server 7 SP3

    OK I know my spelling and grammer is crap so don't quote me on it!

    To err is human to take the P! is only natural !!

    Click on the top section of image for Marcus Miller website and bottom section of image for 'Run For Cover' sound clip


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