Is Dim rs As Object, Set rs = Me.Recordset.Clone, rs.MoveLast and so on a
must? Will it make my database more reliable and stuff?
What this code is doing is simply checking to see if you are on the last
record, and only forcing the focus to the next form if you are on the last
record.
A recordset is a set of records (go figure). Each form has an underlying
recordset, be it directly from a table or from a query (an SQL Statement
returns a recordset...). Recordsets can be created programmatically and be
used to modify data or check stuff or whatever, they don't have to be
assigned to a form (the data in a report is based on a recordset as well).
A bookmark is (simply put) the current position in the recordset.
The StrComp function compares two strings to see if they are alike.
So here's a repost of your code, with comments added to explain how this is
checking to make sure we are on the last record before setting the focus on
the next form (instead of going to the next record in the same form).
Private Sub txtPhoneExt_Exit(Cancel As Integer)
'dim the object to hold the recordset
Dim rs As Object
'Set this new rs you just made to be the same as the one
'in this form (it's Clone). Now we have the form's recordset
'and this one we just created to play around with
Set rs = Me.Recordset.Clone
'rs is the clone, so move to the last record in that
rs.MoveLast
'compare the current position of the form's recordset to
'the current position (last record) of the clone... if they're
'the same (if you know you're on the last record), move
'the focus as desired
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![s_frmPhoneNumbers1]![Child22].SetFocus
End If
End Sub
There's a problem with the above code though. If you exit the control
without any records in there, you can't rs.MoveLast if there's no last record
to move to. This might happen if the subform is blank and you tab through
the empty fields in the New Record to jump to the next subform. You would be
able to tab, but there's no record saved if you don't enter anything. Hence
your No Current Record error.
I would re-write it as so:
Private Sub txtPhoneExt_Exit(Cancel As Integer)
'create and set the recordset
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
'make sure there's records before you try moving to one
If rs.Recordcount <> 0 Then
'if theres records, move to the last one
rs.MoveLast
'run your check and move the focus accordingly
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms!............SetFocus
End If
Else
'if there's no records just go to the next subform
Forms!............SetFocus
End If
End Sub
(..... in replacement of your form names, etc so the word wrap here doesn't
make it hard to read).
Some more recordset info:
rs.BOF (beginning of file, default position when opened... not a record, you
need to MoveFirst to get to the first record. from the first record
rs.MovePrevious puts you at BOF. You can't try and read a bookmark from BOF
because its not a record).
rs.EOF (end of file... you can't reference a bookmark here either, it's not
actually a record... rs.MovePrevious from EOF will put you on the last
record, if there's any records)
Another way to check for recordset is to see if BOF and EOF are true at the
same time. If the are both true, there's no records (and MoveNext,
MoveFirst, MoveLast, MovePrevious will not work)
hope this sheds a little light on what's going on.
--
Jack Leach
www.tristatemachine.com
"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
lmcc007 said:
Hey John Vinson,
Why can't I just enter something like this:
Forms!frmCompanies!Child5.SetFocus
because I tried it and it worked.
Is Dim rs As Object, Set rs = Me.Recordset.Clone, rs.MoveLast and so on a
must? Will it make my database more reliable and stuff?