Runtime Error 2001 you canceled the previous operation

C

cambodian

Dear all Eperts

I have write VBA code like this

Private Sub calenstart_Click()
Me.Filter = "startdate='" & calenstart.Value & "'"
Me.FilterOn = True
End Sub

when i click on my calendar it show message error
Runtime Error 2001
you canceled the previous operation
I can't fix it, what something wrong.
Help me

Thanks
 
A

Allen Browne

The message means that the filter string is not valid.
Consequently, the next operation (applying the filter) fails.

Presumably startdate is the name of a date/time field.
Here are some reasons why it could fail:
a) you used ' instead of # as the delimiter in the filter string.
b) calenstart is Null.
c) calenstart contains an invalid date value (e.g. if it's unbound.)
d) the form is dirty with a record that cannot be saved (e.g. required field
missing.)
e) calenstart is not the right name (and you are not using Option Explicit.)
f) the startdate field is not in this form's source table/query.

Try something like this:

Private Sub calenstart_Click()
Dim strWhere As String
If IsDate(Me.calenstart) then
If Me.Dirty Then Me.Dirty = False
strWhere = "startDate = " & Format(Me.calenstart,
"\#mm\/dd\/yyyy\#")
'Debug.Print strWhere
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub

For an explanation of the date formatting, see:
http://allenbrowne.com/ser-36.html
 
A

Akim

cambodian said:
Dear all Eperts

I have write VBA code like this

Private Sub calenstart_Click()
Me.Filter = "startdate='" & calenstart.Value & "'"
Me.FilterOn = True
End Sub

when i click on my calendar it show message error
Runtime Error 2001
you canceled the previous operation
I can't fix it, what something wrong.
Help me

Thanks
 
R

Rastro

Hi.
I had the same error and It seems to be a bug of VBA and Access because the
error 2001 appears in several events with no visible relation. Anyway I
solved this by capturing it and then retry the line who made the error. For
example (my case):

the error appears in this line:
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , _
acMenuVer70

so I put this lines in the error routine:
Err_Save:
If Err.Number = 2001 Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
Resume Next
End If

It works fine.
 
A

Allen Browne

I would strongly recommend against the practice of repeating the code that
failed again in the error handler, where you could end up with an unhandled
error.
 
P

Paul Shapiro

I agree with Allen. As a developer, your really don't want your application
shutting down Access, which is what happens if you get an unhandled error.
Also, your error handler would perform that save operation if anything else
raised a 2001 error, not just the line you're looking at now. While that
might be ok with your current code, it's definitely fragile and subject to
unexpected results as you modify code in the future.

You could modify your code to retry the save a fixed number of times and
then fail gracefully. Something like this (not tested):

Const clngSaveRetryLimit as long = 5 'Number of times to try saving the
changes
Dim lngRetryCount as long 'Number of times we tried to
save changes
Dim lngError as long 'Current error number

<snip>
'Initialize retry counter
lngRetryCount = 0
'Disable error handling so we can deal with it here
On Error Resume Next
RetrySave:
'Next line could be replace by Me.Dirty = False
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
'Must save the error number as the very next statement, or it will be
reset
lngError = err.Number
'Any error?
Select Case lngError
Case 0 'No error, nothing to be done
Case 2001 'This is the Cancel error we're going to retry
'Have we hit our retry limit yet?
If lngRetryCount < clngSaveRetryLimit Then
lngRetryCount = lngRetryCount + 1
'See if trying again will get it to work
GoTo RetrySave
Else
'Display a nice message to the user
MsgBox "The changes cannot be saved at this time. Please try
again, or contact ..."
End If
Case Else 'Re-throw the error since it's not the one we're trapping
err.raise lngError
End Select
'Resume normal error handling
On Error GoTo ErrorHandler
<snip>

ErrorHandler:
'Your standard error handler here
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top