Using Bookmarks

  • Thread starter Thread starter John C.
  • Start date Start date
J

John C.

I have a form that needs updated every 60 seconds. To do
this, I placed the following code for the form's timer
event:

Private Sub Form_Timer()
varBookMark = Me.Bookmark
Me.Requery
Me.Bookmark = varBookMark
End Sub

When opening the form initially, this works.

BUT, if the forms record source is changed (by code of a
command button), I received "Not a valid bookmark." error
message.

Any way around this?
 
I think, the bookmark is a property of a recordset, and the recordset
changes on requery. If you think about it, 60-second refreshes are an
indication that data can change, and you are trying to stay on the same
record using a bookmark. What do you expect to happen if the record you
are staying in changes or disappears?
I would store the record's ID and then using Seek or FindFirst to go to
that ID after a refresh.

Pavel
 
I think you might be needing your reference to be outside
of your form. Looking at your code, I don't believe
Access allows you to have the two Bookmark=Bookmark
references in the same Sub routine.

I would start by creating a Module with

Public Sub()

Dim VarBookmark as String

End Sub

Then in a form event (I'm guessing Deactivate or Unload
for when the form is "closing" on the refresh)

VarBookmark = Me.UniqueIndexField

Then when the form is refreshed, in a different event
(Activate or On Load for when it is "opening" on the
refresh)

Private Sub()

Dim db as DAO.database
Dim recClone as Recordset

Set db=CurrentDb()
Set recClone.Bookmark = Me.RecordsetClone

recClone.MoveFirst

Do Until recClone.EOF
If recClone!UniqueIndexField = VarBookmark Then
Me.Bookmark=recClone.Bookmark
recClone.Close
db.Close
Exit Sub
End If

recClone.MoveNext
Loop

recClone.Close
db.Close

End Sub

Hope that helps!
 
Back
Top