Question on "frm.Bookmark = rst.Bookmark" Error

Posted on 2008-10-14
Last Modified: 2013-11-28
User sometimes get a Run-Time Error '2001": "You cancel the previous operation"
....and thie error occur when performing a search for a record using the code in the Code Snippet.

How can the user get rid of this error and still be able to perform the seach for a record as per the routine dictates.

frm.Bookmark = rst.Bookmark           '  <<=== Error line
Private Sub DoSearch(myMode As Integer)
   'myMode = 0 means first search
   'myMode = 1 means next search
   'Validation, just double check
    If IsNull(OpenArgs) = True Then
        MsgBox "Missing argument from calling Form"
        Exit Sub
    End If
    Set frm = Forms(Me.OpenArgs)
    If frm.Dirty = True Then
    Dim txStr As String
          txStr = "You made changes of form '" & frm.Name & "'?. Click the Update button to continue"
                Select Case MsgBox(txStr & vbCrLf & _
                "(To cancel, press Cancel button or ('ESC') key.).")
            End Select
                Exit Sub
            End If
    strSearch = ""
       If Not IsNull(cboFindBNo) And Len(Trim(cboFindBNo)) > 0 Then
        strSearch = "BNo = " & Val(cboFindBNo)
        End If
' .........................
' ........................ 
Dim rst As DAO.Recordset
    Set rst = frm.RecordsetClone 
    If rst.EOF = False Or rst.BOF = False Then
      If myMode = 0 Then
        rst.FindFirst strSearch
        GoTo Mode0
        rst.FindNext strSearch
        GoTo Mode1
      End If
    End If
    If rst.NoMatch Then
        MsgBox "No matching record was found", vbInformation, "No Record Found"
        ' set the form to the found record
        frm.Bookmark = rst.Bookmark 
        rst.FindNext strSearch
    End If
 If rst.NoMatch Then
 '       Call requery1         '<<=== Ignore this part for now
        Set rst = Nothing
        rst.FindPrevious strSearch
        btnFindNext.Enabled = True
        CmdFind.Enabled = False
        btnStop.Enabled = True
    End If
    Exit Sub
    If rst.NoMatch Then
      CmdFind.Enabled = True
      btnFindNext.Enabled = False
      MsgBox "There are no more matching records", vbInformation, "No Record Found"
      btnStop.Enabled = False
      ' set the form to the found record
      frm.Bookmark = rst.Bookmark  
    End If

Open in new window

Question by:billcute
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 22711795
I tried to reproduce your error using just your code, but I couldn't.  It works fine using table and forms that I made.

One thing that can cause this error is if DLookup, DCount, etc is called with an inappropriate parameter:
for example: DLookup("x", "tblData","ID=4")  when "x" is NOT a field in tblData.  If you have such a function call in a control source (or other property of your form) that has an inappropriate parameter,
it would cause this error when frm.Bookmark is set equal to rst.Bookmark.

Author Comment

ID: 22716516
I do call some subs at the On Current Event handler of my data entry form - that seems to affect the Search function.
For example, using some "Ifs Statement", certain controls do "reset" the controls either by "setting the controls to nothing" or "by placing some data in the controls" depending on the "If Statement". If you need a sample db you can request it by clicking on my userid so you could drop me a note. Unfortunately I cannot place my sample db here permanently due to the sensitive nature of my db.

Accepted Solution

oldmanbim earned 500 total points
ID: 22747737
After looking over your code, I have a few suggestions.

1) The ClearFlow procedure sets many values to "".  When searching, it changes values in table, not just on the form, hence making the form "dirty".  What I would suggest is that the Control Source for each control affected by ClearFlow be removed.  Then whenever the user manually leaves the current record (I believe only through search or adding a new record), you can record the changes in the table; eg:
        Me!Flow = Me.txtFlow.Value
        Me!BldgPlan = Me.cboBPlan.Value
This will avoid unnecessary modifications to the data when only searching through records.

2) I might misunderstand but your code in frmW:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Not MandatoryFields Then
            Cancel = True
        End If
        Call UpdateEntryDate
        On Error GoTo 0
        Exit Sub
    End Sub
UpdateEntryDate is called even if Cancel is set to true.  Since the user is required to enter the data before they can leave the record, there is no reason to record the change until they do so.

3) If possible, you could open frmSearch as acDialog.  This way the user CAN'T modify the data and you don't have to check for a dirty form (assuming you use #1 above).  Of course, the user would have to close frmSearch to edit the data in frmS or frmW, and this might not be desireable.

4) Using "BeforeUpdate" can be a little tricky, since there are some functions/actions that are not allowed - for example Me.Refresh and some modifications to the current recordset.  I believe the changes suggested in #1 might make it possible not to use BeforeUpdate.  If there is some way around using Form_BeforeUpdate, I suspect your life might be a lot easier.

All that being said, I have written this type of search routine before and in most cases it turns out users are just as comfortable using the built-in Access search function.  In your case have the search look in Address. Another alternative is to simply add a "Search" Listbox to each frmW and frmS.  The attached snippet could be used to search:

Private LastSearch as String
Private Sub lbxSearch_Click()
    Dim booFound as Boolean
    If LastSearch <> Me.lbxSearch Then
        ' Find First
        booFound = SearchDAO("Address=""" & Me.lbxSearch & """, Me.Recordset, True)
        ' Find Next
        booFound = SearchDAO("Address=""" & Me.lbxSearch & """, Me.Recordset, False)
    End If

    If Not booFound Then
        ' This should never happen(!?) because all addresses in lbxSearch come from the recordset
        MsgBox "That address was not found"
    end if
End Sub

Since I just type this in by hand the code might be a bit buggy, but I have used similar code many times before.

' Method: SearchDAO
' Param:  FindString (String);
'   A SQL find string to be used for FindFirst/FindNext
' Param:  MeRst (ByRef DAO.Recordset);
'   A recordset - if from a form, then Me.Recordset
' Param:  FindFirst (Optional Boolean = True);
'   Use FindFirst or FindNext
' Param:  Follow (Optional Boolean = True);
'   If true, sets MeRst Bookmark to the found record
' Return: Boolean;
'   True if a matching record was found, false if not.
Public Function SearchDAO( _
        ByVal FindString As String, _
        ByRef MeRst As DAO.Recordset, _
        Optional ByVal FindFirst As Boolean = True, _
        Optional ByVal Follow As Boolean = True _
        ) As Boolean
    Dim rst As DAO.Recordset
    SearchDAO = False ' Default
    Set rst = MeRst.Clone
    If rst.EOF And rst.BOF Then Exit Function
    If FindFirst Then
        rst.FindFirst FindString
        rst.Bookmark = MeRst.Bookmark
        rst.FindNext FindString
    End If
    If rst.NoMatch Then Exit Function
    If Follow Then MeRst.Bookmark = rst.Bookmark
    SearchDAO = True
End Function

Open in new window


Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question