rutime error '2001': you canceled the previous operation

C

cambo

Hello All expert

I am a beginner using vba
to do filter on form on open but when i do that It show message error like
this
( rutime error '2001':
you canceled the previous operation)

can you help me to solve this problem
thanks.
 
A

Allen Browne

The most likely case is that the filter string is not valid.

To see why, set up your VBA filtering code to assign the filter to a string
variable. Then print this variable to the immediate window, and see how it
looks.

example:
Dim strWhere as String
If Me.Dirty Then Me.Dirty = False
strWhere = "([Last Name] = """ & [txtWotName] & """)"
Debug.Print strWhere
Me.Filter = strWhere
Me.FilterOn = True

If it fails, open the Immediate Window (Ctrl+G) and see what came out. It
has to look just like the WHERE clause in a query. If the field name
contains spaces or odd characters, you must use the square brackets. If the
filter field is a Text field, you must use the quotes as delimiters around
the value.

Another possible reason is that the form is dirty with a record that cannot
be saved (e.g. required field missing), and so the filter cannot be applied.
 
C

cambo

No it is not filter on subform
I have form login and filter form.
So when I login I hide form login and open filter form
so it show message erro like my subject.
 
A

Allen Browne

How do you open the filter form?
Post the code that you use here.

If you are not passing any filter, you might be able to clear the immediate
problem by opening the filter form in design view, and deleting anything in
its Filter property.
 
C

cambo

LoginForm
' -----------------------------------------
Private Sub cmdchangeuser_Click()
If login(txtusername.Value, txtpassword.Value) = True Then
me.visible=false;
Form_frmdailymoney.visible=true
Else
MsgBox "Wrong Username or Password", vbOKOnly, gettitle
End If
End Sub
'-----------------------------------------------
FilterForm
'---------------------------------------------
Private Sub Form_Open(Cancel As Integer)
Dim strfilter As String
strfilter = "DateInserted='" & Date & "' and staffname='" & usernamelogin &
"'"
me.Filter=strfilter
me.Filteron=true
End Sub
'-------------------------------------------------------
 
A

Allen Browne

The filter string is bad.

Date values need to be delimited with #, and forced to the US format that
JET expects:

strfilter = "(DateInserted = #" & Format(Date, "mm\/dd\/yyyy") & _
"#) and (staffname = """ & usernamelogin & """)"
 
A

a a r o n _ k e m p f

do you have a variable named 'cancel'?

I believe that the form_open event has a cancel parameter.. and if you
were to inadvertantly use a VB varaible named 'Cancel' it might cause
this same problem
 

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