FindFirst question

T

terry w

hello
When using code to find a table record that matches a given value, does the
'FindFirst' need to be preceded by 'MoveFirst'? should the code read as...

Dim...
......etc
Set rs = Me.recordset.Clone
rs.FindFirst strSearch
....

or...

Dim...
......etc
Set rs = Me.recordset.Clone
rs.MoveFirst
rs.FindFirst strSearch
....

When studying code samples in this site and others, I've seen it done both
ways. Things seem to work fine without the MoveFirst, but maybe I'm just
getting lucky. Is MoveFirst needed? Why/Why not?

thanks in advance
Terry W.
 
D

Dirk Goldgar

terry w said:
hello
When using code to find a table record that matches a given value, does
the
'FindFirst' need to be preceded by 'MoveFirst'? should the code read
as...

Dim...
.....etc
Set rs = Me.recordset.Clone
rs.FindFirst strSearch
...

or...

Dim...
.....etc
Set rs = Me.recordset.Clone
rs.MoveFirst
rs.FindFirst strSearch
...

When studying code samples in this site and others, I've seen it done both
ways. Things seem to work fine without the MoveFirst, but maybe I'm just
getting lucky. Is MoveFirst needed? Why/Why not?


The .MoveFirst is not needed, since the .FindFirst method always starts at
the beginning of the recordset.

Note that, if you're working in an .mdb or .accdb file, and not an .adp
(Access Data Project), the form's RecordsetClone property is better for this
sort of thing; e.g.:

With Me.RecordsetClone
.FindFirst strSearch
If Not .NoMatch Then
Me.Bookmark = .Bookmark
End If
End With
 
T

terry w

Dirk: thanks for a quick response.

a) If I use the form's recordset property as you suggest, does this mean
there is no need to use a statement like my current 'Set rs =
Me.Recordset.Clone'? Also, is there still a need for any statements to
replace my current statements 'rs.close' or 'Set rs = Nothing'? I'm just
puzzled because I see these used in so many examples from this site.

b) You mention that if using .mdb (I am), "...the form's RecordsetClone
property is better for this sort of thing." For my own understanding of the
situation, can you tell me why it is better?

Much thanks
Terry W.
 
D

Dirk Goldgar

terry w said:
Dirk: thanks for a quick response.

a) If I use the form's recordset property as you suggest,

Actually, I said to use the RecordsetClone property, not the Recordset
property. In recent versions of Access, you can also use the Recordset
property for this sort of thing, but I wasn't talking about that.
does this mean
there is no need to use a statement like my current 'Set rs =
Me.Recordset.Clone'?

Yes, there is no need to define and set a separate recordset object.
Also, is there still a need for any statements to
replace my current statements 'rs.close' or 'Set rs = Nothing'? I'm
just
puzzled because I see these used in so many examples from this site.

No, there is no need for those statements. In the code I posted, the With
statement creates the reference to the RecordsetClone, so you don't need to
Dim a Recordset object; you don't need to (and shouldn't) close the
RecordsetClone; and the scope of the With statement defines the lifetime of
the procedure's reference to the recordset, so there's no object variable
that needs to be set to Nothing.

You *could* accomplish the same thing using an object variable, but still
using the form's RecordsetClone property rather than creating a fresh clone
of the form's recordset:

'------ start of alternate code ------
Dim rs As DAO.Recordset

Set rs = Me.RecordsetClone

rs.FindFirst strSearch
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
End If

Set rs = Nothing
'------ end of alternate code ------

But there's no reason, in my view, to use the extra code.
b) You mention that if using .mdb (I am), "...the form's RecordsetClone
property is better for this sort of thing." For my own understanding of
the
situation, can you tell me why it is better?

It's better because the RecordsetClone property returns a reference to a
hidden clone of the form's recordset that is created the first time you
refer to the property, and then remains in existence for as long as the form
is open. Thus, the clone is created the first time you use it, and the
overhead of creating the clone is incurred only once during the lifetime of
the form instance. If your code explicitly clones the recordset (as
originally written: "Set rs = Me.Recordset.Clone"), then that overhead will
be incurred every time the code is called.

The code you so often see, using "Me.Recordset.Clone", is the code that the
wizards in Access 2000-2003 (if not 2007) build for finding records. I
believe they introduced that specifically for ADP support (ADPs use ADO
recordsets, not DAO recordsets), but I'm not sure why they felt the need to,
and other parts of the wizard code are just plain wrong.

Some people do feel the need to declare an object variable for every object
they intend to manipulate, but I don't see the point when a well-chosen With
statement will do the job, and usually be more efficient, too.
 
T

terry w

Dirk: thanks again for a very thorough and comprehensive answer. For
learning programmers like me, you've given us a lot to chew on!

Terry W.
 
T

terry w

Dirk - one final concern. I like the fact that the first use of the
RecordsetClone Property creates a reference to the RecordsetClone that stays
'alive' as long as the form is open (as opposed to creating a new reference
each time the Sub is used). But, if the Form's recordset changes between
uses of the Sub, does the RecordsetClone automatically reflect these changes,
or does it need to be refreshed somehow. I hope this is clear.

Thanks in advance
Terry W.
 
D

Dirk Goldgar

terry w said:
Dirk - one final concern. I like the fact that the first use of the
RecordsetClone Property creates a reference to the RecordsetClone that
stays
'alive' as long as the form is open (as opposed to creating a new
reference
each time the Sub is used). But, if the Form's recordset changes between
uses of the Sub, does the RecordsetClone automatically reflect these
changes,
or does it need to be refreshed somehow. I hope this is clear.


If the form's recordset is updated, the RecordsetClone reflects those
updates. If the form's recordset is replaced, as by requerying or filtering
the recordset, a new RecordsetClone will be generated the next time the
property is accessed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top