• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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

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

  • 2
1 Solution
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.
billcuteAuthor Commented:
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.
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now