473,418 Members | 3,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

recordset.bookmark problem

Seth Schrock
2,965 Expert 2GB
I have the following subroutine that I'm using to bookmark the current record, do a requery of the form, and then go back to the record that I bookmarked. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub FormRefresh()
  2. Dim varBookmark As Variant
  3.  
  4. With Me.sfrmCrimes.Form
  5.     varBookmark = .Recordset.Bookmark
  6.     .Requery
  7.     If IsEmpty(varBookmark) Then
  8.         MsgBox "Bookmark is empty"
  9.     Else
  10.         .Recordset.Bookmark = varBookmark
  11.     End If
  12. End With
  13.  
  14. End Sub
When it runs, it stops on line #10 and says
Run-time error '3129':
Not a valid bookmark.


If I select "Debug", line #10 is highlighted. If I then drag it up to line #7 and then step through the code using F8, then the code runs fine. This defies all troubleshooting methods that I have ever heard of, so I have no idea how to test this. I initially put in the If/Then statement to test varBookmark as I wanted to see if it wasn't getting passed the bookmark for some reason. However, I never get the "Bookmark is empty" message. What am I doing wrong?

This is the first time that I have ever used the Recordset.Bookmark property. Here is the link that I was trying to follow when I created this: MSDN Recordset.Bookmark Property
Dec 29 '12 #1

✓ answered by ADezii

Your original concept will actually work:
Expand|Select|Wrap|Line Numbers
  1. Dim varBookmark As Variant
  2.  
  3. With Me.sfrmCrimes.Form
  4.   varBookmark = .Recordset.Bookmark
  5.  
  6.   'Requery the Sub-Form Control, NOT the underlying Form itself
  7.   Me.sfrmCrimes.Requery
  8.  
  9.   If IsEmpty(varBookmark) Then
  10.     MsgBox "Bookmark is empty"
  11.   Else
  12.     .Recordset.Bookmark = varBookmark
  13.   End If
  14. End With
  15.  

12 15952
TheSmileyCoder
2,322 Expert Mod 2GB
If you read the link you posted:
MSDN However, you can't use bookmarks from different Recordset objects interchangeably, even if they were created by using the same object or the same SQL statement.
Consider the bookmark as you would a "real" life bookmark, and the recordset as you would a real life book. Now when you open a recordset its like buying a book. You can place a bookmark in the book (recordset) and return to the bookmark later. But opening a new recordset is like getting a new book. Opening a new recordset with the same sql (Such as when you requery) is like getting a new copy of the same book. Problem is you can't use the bookmark, cause its stuck in the old/first book.

So in short, you cannot use the bookmark in that way. If the records have a unique ID (And they should!!!!) then you can use that instead. Lets assume your primary key for the record is simply called PK_Record and is numeric.

Expand|Select|Wrap|Line Numbers
  1. Private Sub FormRefresh()
  2. dim lngRecordID as long
  3.  
  4. With Me.sfrmCrimes.Form
  5.     lngRecordID = .lngRecordID
  6.     .Requery 
  7.     .RecordSet.FindFirst "lngRecordID=" & lngRecordID 
  8.  
  9. End With
  10.  
  11. End Sub
That is the simplest version of the code, to get more advanced you could add in checks for whether the record is found (what if another user deleted it in the meantime?), or what if you start by standing on a empty(new) record?, however I can't advice on this since the details are missing.
Dec 29 '12 #2
Seth Schrock
2,965 Expert 2GB
I didn't catch that a requery would fall into the category of being created by using the same object or the same SQL statement. Thank-you for pointing this out.

And your suggestion worked perfectly. The only check I added was to make sure that sfrmCrimes was not on a new record.
Dec 29 '12 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Happy to be of assistance. I remember it as not too long ago I had the same/similar learning experience with using bookmarks. :)

Best of Luck with your project
TheSmileyCoder
Dec 29 '12 #4
ADezii
8,834 Expert 8TB
Your original concept will actually work:
Expand|Select|Wrap|Line Numbers
  1. Dim varBookmark As Variant
  2.  
  3. With Me.sfrmCrimes.Form
  4.   varBookmark = .Recordset.Bookmark
  5.  
  6.   'Requery the Sub-Form Control, NOT the underlying Form itself
  7.   Me.sfrmCrimes.Requery
  8.  
  9.   If IsEmpty(varBookmark) Then
  10.     MsgBox "Bookmark is empty"
  11.   Else
  12.     .Recordset.Bookmark = varBookmark
  13.   End If
  14. End With
  15.  
Dec 29 '12 #5
Seth Schrock
2,965 Expert 2GB
Perfect ADezii! I just tried it and I like it much better. I always saw the record jump when using the .Recordset.FindFirst method and using the bookmark had no jump at all. Thanks so much.
Dec 29 '12 #6
ADezii
8,834 Expert 8TB
@Seth Schrock
Yopu are quite welcome.
Dec 29 '12 #7
NeoPa
32,556 Expert Mod 16PB
ADezii:
'Requery the Sub-Form Control, NOT the underlying Form itself
Nice. This question has given me an idea as to how I can improve some code I expect to be working on again shortly.

I'm pretty sure this also means (and I'm highlighting this because it seems important to understand rather than as a criticism of Smiley's attempt to be helpful) that a .Requery() does not cause the Recordset object to be considered to be a separate Recordset from before the .Requery().
Dec 31 '12 #8
Seth Schrock
2,965 Expert 2GB
What then would be the reason for my initial code to not work? I assumed that this proved that Smiley was correct and that a requery of the control didn't requery the form's recordsource (allowing the bookmark to work) whereas a requery of the form inside the subform control did cause the form's recordsource to be redone (causing the bookmark to fail). As this is the only difference in the two pieces of code, I'm at a loss for what could be the cause of the original to fail if that is not it.
Dec 31 '12 #9
NeoPa
32,556 Expert Mod 16PB
I'm not sure Seth. I may be wrong - hence the wording of my post. I too, want that to be clarified. I must admit though, that the idea of a .Requery() call meaning that a recordset is no longer the same recordset seems logically quite wrong to me. The recordset is not being recreated. merely requeried/refreshed. Refresh is the term I want to use, but that has a specific meaning with regards to recordsets in Access.

If Smiley is correct, then it's important that point is understood. Whatever the answer, it should really be understood rather than guessed at is my view.
Dec 31 '12 #10
zmbd
5,501 Expert Mod 4TB
:)
Here's something I've done - and very often
.
(Seth, this is an example of one of those things that Neopa pointed out that could be handled directly by the form within the Access structure; however, I have my reasons ;-) )

The form this code is in is setup much like the one in given in the example database Many-to-Many-to-Many relationship problem wherein the form is bound to a record set, direct record manipulation is prohibited, and I have a set of unbound controls in the footer for new record creation.

The reason behind this setup is that I have to have a confirmation that the information entered is correct as once the record is created, it cannot be changed as a lot number is created before being passed to this part of the code. The lot numbers can only be assigned once and have to have a strict tracking.... no mistakes, no omissions, or the paperwork nightmare never ends!

So, to the point which happens at line 12 and then 16 thru 19:

Expand|Select|Wrap|Line Numbers
  1. '<<<Leading Code Omitted>>>
  2. 'Create new entry into the tbl_inventory and refresh the form
  3.     '
  4.     'Setup for the record set
  5.     Set zrs = zdb.OpenRecordset("tbl_inventory", dbOpenDynaset)
  6.     '
  7.     'Pull the information from the form and the new id
  8.     With zrs
  9.         .AddNew
  10. '<Sorry, removed this section of the code, just updates the fields.>
  11.         .Update
  12.         .Bookmark = .LastModified
  13.     End With
  14.     '
  15.     'requery the form and move to the last modified record
  16.     With Me
  17.         .Requery
  18.         .Bookmark = zrs.Bookmark
  19.     End With
  20.     '
  21.     'Close the current recordset,
  22.     zrs.Close
  23.     Set zrs = Nothing'
  24. '<<<Ending Code Omitted>>>
SO, I take a look at the bookmark from the VBA's copy of the book (line 11) and tell the form to turn to that page in its copy of the book (lines 15 thru 18).

The point being that if we think of these as three ring binders, then when several people are working, there are several three ring binders open and pages being added to these binders - all of these binders are of course just working copies of the underlying table (over simplified of course). As the user needs to be able to find the record just added to the underlying table once the form is refreshed, and 8 other people might have just added a record too, I thought it nice to just bookmark the VBA copy and then transfer that bookmark over to the Form's copy after being updated... something like calling a friend and telling them that you're on page 8 of the same book you're both reading.
Dec 31 '12 #11
Stewart Ross
2,545 Expert Mod 2GB
I found an interesting problem with the use of a bookmark property in the way suggested by the earlier posts in this thread. Although this was not in the context of a subform, the general principle was the same: the form's current bookmark property was stored (as a result of an After Update event in my case), a requery is done, and the bookmark is restored to locate the same record as was previously in focus.

The problem occurred in the context of a department form in which the name of the department is editable, and is ordered in alphabetical order in the underlying query on which the form was based. If the edit undertaken changes the relative order of the record that was edited then restoring the former bookmark after the form requery restores focus to the wrong record. Reason for this is that the bookmark is referring to an absolute position in the recordset, but if the relative position of the wanted record has changed this invalidates the absolute position previously stored in the bookmark.

I resolved this by going back to the approach shown in the MS examples and also advocated by Smiley - after the requery setting a recordset to the form's recordsetclone, using FindFirst to find the record concerned, then setting the bookmark property of the form to the bookmark of record found in the cloned recordset.

In short, if an edit has taken place which changes the relative order of the recordset concerned then restoring the pre-edit bookmark after a requery may result in the bookmark locating a different record than was originally set. The recordsetclone/find the record and rebookmark approach does not suffer from this problem.

I have not tested what may happen in a multi-user environment but by inference if a user adds a new record which could change the existing sort order of the recordset used by the form then the same problem with restoring a pre-edit bookmark is likely to result.

-Stewart
Jan 8 '13 #12
NeoPa
32,556 Expert Mod 16PB
Stewart:
The recordsetclone/find the record and rebookmark approach does not suffer from this problem.
I don't doubt this point Stewart, but I would point out that the beauty of the whole Bookmark concept is that it obviates the need for doing another find through an index. Getting around the problem, in such a way that loses this benefit, seems disappointing at best.

That said, if all you say is true (and I'm not so stupid that I would need to be convinced of that again.), it may still be a necessary evil :-(
Jan 9 '13 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Dave | last post by:
Hi all, I've been trying to figure this out for the last day and a half and it has me stumped. I've got a web application that I wrote that keeps track of trading cards I own, and I'm moving it...
6
by: HKM | last post by:
Hello, I have a query engine that builds the SQL Query to obtain the recordSet. Following is an Exmaple Query that my QueryBuilder outputted SELECT * FROM BookInfo WHERE BookName LIKE...
3
by: DrFoo | last post by:
(Access 97 and 2003) Hi, Here's a smippit of code that works correctly about 95% of the time. This part of the algorithm... - find the first value = true - if found, find next value =...
2
by: corepaul | last post by:
I am fairly new to Access and I have a problem trying to use bookmarks with a recordset. I have a recordset dimensioned as, Dim rstFoodDesc As ADODB.Recordset ' recordset Dim bMark As...
3
by: Wayne Wengert | last post by:
In VB6/ADO I used to use the code below to put all the records that did not have a valid email address into an array which I used later to print mailing labels. I am not aware of a NET equivalent....
2
by: Tony Rice | last post by:
I"d like to hear critiques on the following method for dealing with the back button and bookmarkability problem with AJAX. Whenever I do something on a page with ajax, I add to...
5
by: Kaur | last post by:
Hi, I am having a strange bookmark problem. I have a main form called frmSurveyQSubQ. This form has a subform called sfrmRespodent. SfrmRespondent has a sfrm in it called sfrsfrmResponses. I have...
1
by: Lazster | last post by:
Hi, I'd really appreciate some help here people. I am trying to insert some values in a table in another database and not getting the results I am expecting. I am simply trying to insert values...
5
by: fieldling | last post by:
I've written the following code to update a recordset but when I run it I get a Run-time error 3020: Update or CancelUpdate without AddNew or Edit. When I debug it highlights the rs.update line. I've...
1
by: nimeshjaiswal | last post by:
Hi, I m doing a migration project from VB6 to VB.NET 2008. I m facing problem in migration the recordsets to dataset specially with properties in fields and bookmark with recordset. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.