Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2008-10-14
Medium Priority
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
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

971 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