Runtime Error 2001,when setting filter.

L

Lars Pedersen

Hi there.
I am getting run-time error 2001 "You cancled the previus operation", when
my code reach
the frmFilterOn=true . I can not find out what operation it is talking
about and why, can anyone help me.
The code is on a popup search form, ID is autonumber that uniquely
identifies the record

Code:
Private Sub cmdUseAsFilter_Click()
Dim Criteria As String
Dim i As Variant
Dim frm As Form
Set frm = Forms(Me.OpenArgs)
' Build criteria string from selected items in list box.
Criteria = ""
For Each i In Me![lstShipInfo].ItemsSelected
If Criteria <> "" Then
Criteria = Criteria & " OR "
End If
Criteria = Criteria & " ID = '" & Me![lstShipInfo].ItemData(i) & "'"
Next i
' Filter the form using selected items in the list box.
frm.Filter = Criteria
frm.FilterOn = True
DoCmd.Close acForm, Me.Name
End Sub
 
A

Allen Browne

The message means that Access cannot complete the current action (turning
the FilterOn), because the previous action is now found to be invalid. The
previous operation is therefore assigning the string to the Filter, and the
message is telling you that the Filter string you assigned is malformed.

If ID is a Number field (not a Text field), lose the extra quotes, i.e.:
Criteria = Criteria & " ID = " & Me![lstShipInfo].ItemData(i)

That will still be invalid if ItemData(i) is null or a zero-length string.
 
L

Lars Pedersen

Thank you very much, now it works.(I found the code on internet, and was
making it work for me. obviously I haven't got the fundamentals right,
since I did not se that one)

/Lars

Allen Browne said:
The message means that Access cannot complete the current action (turning
the FilterOn), because the previous action is now found to be invalid. The
previous operation is therefore assigning the string to the Filter, and
the message is telling you that the Filter string you assigned is
malformed.

If ID is a Number field (not a Text field), lose the extra quotes, i.e.:
Criteria = Criteria & " ID = " & Me![lstShipInfo].ItemData(i)

That will still be invalid if ItemData(i) is null or a zero-length string.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Lars Pedersen said:
Hi there.
I am getting run-time error 2001 "You cancled the previus operation",
when my code reach
the frmFilterOn=true . I can not find out what operation it is talking
about and why, can anyone help me.
The code is on a popup search form, ID is autonumber that uniquely
identifies the record

Code:
Private Sub cmdUseAsFilter_Click()
Dim Criteria As String
Dim i As Variant
Dim frm As Form
Set frm = Forms(Me.OpenArgs)
' Build criteria string from selected items in list box.
Criteria = ""
For Each i In Me![lstShipInfo].ItemsSelected
If Criteria <> "" Then
Criteria = Criteria & " OR "
End If
Criteria = Criteria & " ID = '" & Me![lstShipInfo].ItemData(i) & "'"
Next i
' Filter the form using selected items in the list box.
frm.Filter = Criteria
frm.FilterOn = True
DoCmd.Close acForm, Me.Name
End Sub
 
L

Lars Pedersen

But I ran into another problem, the filter will to long to be applied :-(

/Lars

Lars Pedersen said:
Thank you very much, now it works.(I found the code on internet, and was
making it work for me. obviously I haven't got the fundamentals right,
since I did not se that one)

/Lars

Allen Browne said:
The message means that Access cannot complete the current action (turning
the FilterOn), because the previous action is now found to be invalid.
The previous operation is therefore assigning the string to the Filter,
and the message is telling you that the Filter string you assigned is
malformed.

If ID is a Number field (not a Text field), lose the extra quotes, i.e.:
Criteria = Criteria & " ID = " & Me![lstShipInfo].ItemData(i)

That will still be invalid if ItemData(i) is null or a zero-length
string.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Lars Pedersen said:
Hi there.
I am getting run-time error 2001 "You cancled the previus operation",
when my code reach
the frmFilterOn=true . I can not find out what operation it is talking
about and why, can anyone help me.
The code is on a popup search form, ID is autonumber that uniquely
identifies the record

Code:
Private Sub cmdUseAsFilter_Click()
Dim Criteria As String
Dim i As Variant
Dim frm As Form
Set frm = Forms(Me.OpenArgs)
' Build criteria string from selected items in list box.
Criteria = ""
For Each i In Me![lstShipInfo].ItemsSelected
If Criteria <> "" Then
Criteria = Criteria & " OR "
End If
Criteria = Criteria & " ID = '" & Me![lstShipInfo].ItemData(i) &
"'"
Next i
' Filter the form using selected items in the list box.
frm.Filter = Criteria
frm.FilterOn = True
DoCmd.Close acForm, Me.Name
End Sub
 
A

Allen Browne

How long is this filter?
You should be able to assign any practical filter in code.

If it's a multi-select list box, use IN () instead of a bunch of ORs.

If the worst comes to the worst, you could build the complete SQL statement
instead of just the Filter (which is the WHERE clause of an SQL statement),
and assign it to the form's RecordSource.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Lars Pedersen said:
But I ran into another problem, the filter will to long to be applied :-(

/Lars

Lars Pedersen said:
Thank you very much, now it works.(I found the code on internet, and was
making it work for me. obviously I haven't got the fundamentals right,
since I did not se that one)

/Lars

Allen Browne said:
The message means that Access cannot complete the current action
(turning the FilterOn), because the previous action is now found to be
invalid. The previous operation is therefore assigning the string to the
Filter, and the message is telling you that the Filter string you
assigned is malformed.

If ID is a Number field (not a Text field), lose the extra quotes, i.e.:
Criteria = Criteria & " ID = " & Me![lstShipInfo].ItemData(i)

That will still be invalid if ItemData(i) is null or a zero-length
string.


"Lars Pedersen" <no> wrote in message
Hi there.
I am getting run-time error 2001 "You cancled the previus operation",
when my code reach
the frmFilterOn=true . I can not find out what operation it is talking
about and why, can anyone help me.
The code is on a popup search form, ID is autonumber that uniquely
identifies the record

Code:
Private Sub cmdUseAsFilter_Click()
Dim Criteria As String
Dim i As Variant
Dim frm As Form
Set frm = Forms(Me.OpenArgs)
' Build criteria string from selected items in list box.
Criteria = ""
For Each i In Me![lstShipInfo].ItemsSelected
If Criteria <> "" Then
Criteria = Criteria & " OR "
End If
Criteria = Criteria & " ID = '" & Me![lstShipInfo].ItemData(i) &
"'"
Next i
' Filter the form using selected items in the list box.
frm.Filter = Criteria
frm.FilterOn = True
DoCmd.Close acForm, Me.Name
End Sub
 
L

Lars Pedersen

The filter may contain a bunch of OR's if the user was not very selective in
selecting criteria on the searchform, there could be up to 1500 of them, -
together with an longint.
I guess this is not a pratical way of doing this, maybe changing the
recordsource is better.
How do you make use of this IN() ?

/Lars



Allen Browne said:
How long is this filter?
You should be able to assign any practical filter in code.

If it's a multi-select list box, use IN () instead of a bunch of ORs.

If the worst comes to the worst, you could build the complete SQL
statement instead of just the Filter (which is the WHERE clause of an SQL
statement), and assign it to the form's RecordSource.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Lars Pedersen said:
But I ran into another problem, the filter will to long to be applied :-(

/Lars

Lars Pedersen said:
Thank you very much, now it works.(I found the code on internet, and was
making it work for me. obviously I haven't got the fundamentals right,
since I did not se that one)

/Lars

The message means that Access cannot complete the current action
(turning the FilterOn), because the previous action is now found to be
invalid. The previous operation is therefore assigning the string to
the Filter, and the message is telling you that the Filter string you
assigned is malformed.

If ID is a Number field (not a Text field), lose the extra quotes,
i.e.:
Criteria = Criteria & " ID = " & Me![lstShipInfo].ItemData(i)

That will still be invalid if ItemData(i) is null or a zero-length
string.


"Lars Pedersen" <no> wrote in message
Hi there.
I am getting run-time error 2001 "You cancled the previus operation",
when my code reach
the frmFilterOn=true . I can not find out what operation it is
talking about and why, can anyone help me.
The code is on a popup search form, ID is autonumber that uniquely
identifies the record

Code:
Private Sub cmdUseAsFilter_Click()
Dim Criteria As String
Dim i As Variant
Dim frm As Form
Set frm = Forms(Me.OpenArgs)
' Build criteria string from selected items in list box.
Criteria = ""
For Each i In Me![lstShipInfo].ItemsSelected
If Criteria <> "" Then
Criteria = Criteria & " OR "
End If
Criteria = Criteria & " ID = '" & Me![lstShipInfo].ItemData(i) &
"'"
Next i
' Filter the form using selected items in the list box.
frm.Filter = Criteria
frm.FilterOn = True
DoCmd.Close acForm, Me.Name
End Sub
 
A

Allen Browne

WHERE ID IN (23,5,7,99,...)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Lars Pedersen said:
The filter may contain a bunch of OR's if the user was not very selective
in selecting criteria on the searchform, there could be up to 1500 of
them, - together with an longint.
I guess this is not a pratical way of doing this, maybe changing the
recordsource is better.
How do you make use of this IN() ?

/Lars



Allen Browne said:
How long is this filter?
You should be able to assign any practical filter in code.

If it's a multi-select list box, use IN () instead of a bunch of ORs.

If the worst comes to the worst, you could build the complete SQL
statement instead of just the Filter (which is the WHERE clause of an SQL
statement), and assign it to the form's RecordSource.


Lars Pedersen said:
But I ran into another problem, the filter will to long to be applied
:-(

/Lars

"Lars Pedersen" <no> wrote in message
Thank you very much, now it works.(I found the code on internet, and
was making it work for me. obviously I haven't got the fundamentals
right, since I did not se that one)

/Lars

The message means that Access cannot complete the current action
(turning the FilterOn), because the previous action is now found to be
invalid. The previous operation is therefore assigning the string to
the Filter, and the message is telling you that the Filter string you
assigned is malformed.

If ID is a Number field (not a Text field), lose the extra quotes,
i.e.:
Criteria = Criteria & " ID = " & Me![lstShipInfo].ItemData(i)

That will still be invalid if ItemData(i) is null or a zero-length
string.


"Lars Pedersen" <no> wrote in message
Hi there.
I am getting run-time error 2001 "You cancled the previus operation",
when my code reach
the frmFilterOn=true . I can not find out what operation it is
talking about and why, can anyone help me.
The code is on a popup search form, ID is autonumber that uniquely
identifies the record

Code:
Private Sub cmdUseAsFilter_Click()
Dim Criteria As String
Dim i As Variant
Dim frm As Form
Set frm = Forms(Me.OpenArgs)
' Build criteria string from selected items in list box.
Criteria = ""
For Each i In Me![lstShipInfo].ItemsSelected
If Criteria <> "" Then
Criteria = Criteria & " OR "
End If
Criteria = Criteria & " ID = '" & Me![lstShipInfo].ItemData(i) &
"'"
Next i
' Filter the form using selected items in the list box.
frm.Filter = Criteria
frm.FilterOn = True
DoCmd.Close acForm, Me.Name
End Sub
 
L

Lars Pedersen

I found out, thanks again.

/Lars

Allen Browne said:
WHERE ID IN (23,5,7,99,...)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Lars Pedersen said:
The filter may contain a bunch of OR's if the user was not very selective
in selecting criteria on the searchform, there could be up to 1500 of
them, - together with an longint.
I guess this is not a pratical way of doing this, maybe changing the
recordsource is better.
How do you make use of this IN() ?

/Lars



Allen Browne said:
How long is this filter?
You should be able to assign any practical filter in code.

If it's a multi-select list box, use IN () instead of a bunch of ORs.

If the worst comes to the worst, you could build the complete SQL
statement instead of just the Filter (which is the WHERE clause of an
SQL statement), and assign it to the form's RecordSource.


"Lars Pedersen" <no> wrote in message
But I ran into another problem, the filter will to long to be applied
:-(

/Lars

"Lars Pedersen" <no> wrote in message
Thank you very much, now it works.(I found the code on internet, and
was making it work for me. obviously I haven't got the fundamentals
right, since I did not se that one)

/Lars

The message means that Access cannot complete the current action
(turning the FilterOn), because the previous action is now found to
be invalid. The previous operation is therefore assigning the string
to the Filter, and the message is telling you that the Filter string
you assigned is malformed.

If ID is a Number field (not a Text field), lose the extra quotes,
i.e.:
Criteria = Criteria & " ID = " & Me![lstShipInfo].ItemData(i)

That will still be invalid if ItemData(i) is null or a zero-length
string.


"Lars Pedersen" <no> wrote in message
Hi there.
I am getting run-time error 2001 "You cancled the previus
operation", when my code reach
the frmFilterOn=true . I can not find out what operation it is
talking about and why, can anyone help me.
The code is on a popup search form, ID is autonumber that uniquely
identifies the record

Code:
Private Sub cmdUseAsFilter_Click()
Dim Criteria As String
Dim i As Variant
Dim frm As Form
Set frm = Forms(Me.OpenArgs)
' Build criteria string from selected items in list box.
Criteria = ""
For Each i In Me![lstShipInfo].ItemsSelected
If Criteria <> "" Then
Criteria = Criteria & " OR "
End If
Criteria = Criteria & " ID = '" & Me![lstShipInfo].ItemData(i)
& "'"
Next i
' Filter the form using selected items in the list box.
frm.Filter = Criteria
frm.FilterOn = True
DoCmd.Close acForm, Me.Name
End Sub
 

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