PDA

Click to See Complete Forum and Search --> : ADO - FIND Statement


hyme
Oct 25th, 2001, 09:22 AM
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!

Granty
Oct 25th, 2001, 10:20 AM
Hrmmm...never tried the ADO find but try.....

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

J.

Gary.Lowe
Oct 25th, 2001, 10:25 AM
Hyme

Use the ADO seek method

MSDN


Sub ADOSeekRecord()

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

' Open the connection
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=.\NorthWind.mdb;"

' Open the recordset
rst.Open "Order Details", cnn, adOpenKeyset, adLockReadOnly, _
adCmdTableDirect

' Select the index used to order the data in the recordset
rst.Index = "PrimaryKey"

' Find the order where OrderId = 10255 and ProductId = 16
rst.Seek Array(10255, 16), adSeekFirstEQ

' If a match is found print the quantity of the order
If Not rst.EOF Then
Debug.Print rst.Fields("Quantity").Value
End If

' Close the recordset
rst.Close

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.

Kokomo
Oct 25th, 2001, 10:54 AM
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

Granty
Oct 25th, 2001, 11:06 AM
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.

Gary.Lowe
Oct 25th, 2001, 11:08 AM
The only quarms I would have with kokamo's code is that I think Hyme is using ADO rather than DAO :D

hyme
Oct 25th, 2001, 11:09 AM
YUP!

Granty
Oct 25th, 2001, 11:11 AM
Heh, that he is Gary. Can be changed a bit tho....

POB!!!!

J.

Gary.Lowe
Oct 25th, 2001, 11:12 AM
:D

Lee_S
Oct 25th, 2001, 11:23 AM
POB!!!!
I knew I recognised that face from somewhere! :D :D

Takes me back too many yrs that..:p

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>>>>>>>
Follow the string!!!

hyme
Oct 25th, 2001, 11:24 AM
OK!
What is POB???:D

Gary.Lowe
Oct 25th, 2001, 11:26 AM
Originally posted by Lee_S

I knew I recognised that face from somewhere! :D :D

Takes me back too many yrs that..:p

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>>>>>>>
Follow the string!!!

Ha Haa

Now that was entertainment... :D

Ahhh gone are the days of simple things please simple minds eh?;)

Lee_S
Oct 25th, 2001, 11:28 AM
-Insert Brian Conely voice-

Its a puuppeeeetttt!

Well, sort of. Very old kids tv proggie. Gary's avatar is a piccie of Pob!!

hyme
Oct 25th, 2001, 11:30 AM
Thanx, now I know!

Gary.Lowe
Oct 25th, 2001, 11:32 AM
Hyme

Either your too young or not from the UK

Anyway

did any of the above stuff help you out with your problem:eek:

hyme
Oct 25th, 2001, 11:38 AM
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.

Gary.Lowe
Oct 25th, 2001, 11:40 AM
Old enough :eek:

but wrong country.

Good to see you got it sorted out anyway. :p