473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

VBA to Filter for exact match

loonette
I need help with some code. I am using this code to filter for a catalog number([SEitem#]) in a form. I would like it to filter for the exact match. When it filters right now if you put a "1" in the [filtercat] text box it filters for everything that starts with "1", how do I change the code to filter for the exact match?

Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckFilter()
  2.  
  3.   Dim strFilter As String, strOldFilter As String
  4.  
  5.     strOldFilter = Me.Filter
  6.  If Me!filtercat > "" Then _
  7.         strFilter = strFilter & _
  8.                     " AND ([SEitem#] Like '" & _
  9.                     Me!filtercat & "*')"
  10.     'Debug.Print ".Filter = '" & strOldFilter & "' - ";
  11.     'Debug.Print "strFilter = '" & strFilter & " '"
  12.     'Tidy up results and apply IF NECESSARY
  13.     If strFilter > "" Then strFilter = Mid(strFilter, 6)
  14.     If strFilter <> strOldFilter Then
  15.         Me.Filter = strFilter
  16.         Me.FilterOn = (strFilter > "")
  17.     End If
  18. End Sub
  19.  
Thank you,
Lori
Jan 13 '12 #1
12 4991
Seth Schrock
2,965 Expert 2GB
The reason that it is returning everything that starts with a 1 is because of the word like in line 8 and the "*" in line 9. So a 1 in SEitem# is like 1, 10, 100, 155, etc. You want it to equal Me!filtercat. I believe that the fix would be the following for lines 7 through 9.

Expand|Select|Wrap|Line Numbers
  1. strFilter = [SEitem#] = Me!filtercat
I don't code very many filters so my syntax could be wrong, but that is where your problem lies.
Jan 13 '12 #2
NeoPa
32,556 Expert Mod 16PB
Lines #6 to #9 in your code should be :
Expand|Select|Wrap|Line Numbers
  1.     If Me.FilterCat > "" Then _
  2.         strFilter = strFilter & " AND ([SEitem#] = '" & Me.FilterCat & "')"
Jan 13 '12 #3
NeoPa,

Thank you for your help. I put the code in that you gave me and I am now getting an error on Line #15 & #16 of my code. Any thoughts?

Thanks!
Lori
Jan 14 '12 #4
Seth Schrock
2,965 Expert 2GB
What does the error message say?
Jan 14 '12 #5
It doesn't say anything, it just goes into debug and highlights those two lines.

Lori
Jan 14 '12 #6
NeoPa
32,556 Expert Mod 16PB
That's strange Lori. The two lines are separate as you've shown them here and when code stops it always stops on a single VBA command (IE. a single line or multiple lines if a single command is spread over them). Not only is the code on your lines #15 and #16 two separate commands, but it's also good, bug-free, code.

I have come across situations where the debugger gets confused and thinks it needs to stop simply to debug. This may be one of those. Try recompiling and saving the database then closing Access completely and opening it up again and see what happens.
Jan 14 '12 #7
NeoPa,

I tried compact & repair the database and it didn't fix the error. I apologize, it is giving me an error message it says "Runtime Error 2001, You canceled the previous operation" Any more thoughts?

Lori
Jan 14 '12 #8
NeoPa
32,556 Expert Mod 16PB
Lori, I can't see how that can make sense. First you say there is no error message, then you say you tried C&R and the error's still there - now with an error message. Perhaps you could explain so that I can understand.
Jan 15 '12 #9
NeoPa,

I apologized in my previous post. It WAS giving me an error message originally, I just wasn't looking in the correct place for the error message. I thought the error message would come up when it highlighted the code, not before I hit the debug button. Sorry for any confusion.

To clarify, when I try to filter for the exact match it is giving me an error which says "Runtime Error 2001, You canceled the previous operation". I then click on the debug button, the programs then highlights lines 15 & 16 of the code.

Thank you,
Lori

Lori
Jan 15 '12 #10
NeoPa
32,556 Expert Mod 16PB
That's much clearer. Unfortunately we still have problems. That error message is particularly uninformative. It only tells me there may be some form of corruption somewhere. We also have the issue I mentioned in post #7, where your explanation doesn't match your posted code. Even with a corruption it can still only select a single line (or command) of code. Is your code exactly as posted in post #1?
Jan 15 '12 #11
Ok, I think I have it narrowed down. When I received the error message the first time it highlighted both lines. Now when I do it is only highlighting line 16. This is so weird! Let me know if that helps....

Thanks!
Lori
Jan 16 '12 #12
NeoPa
32,556 Expert Mod 16PB
It certainly makes more sense Lori, but as I said earlier, this error message is largely unhelpful. When I get something like that I have to play it by ear to a certain extent. What I try to do (if I can attempt to put some ideas into a coherent set of steps to try) is to make a change to the code (Nothing that has any real effect, but enough for Access to realise that a save is required) then, assuring that no lines are breakpointed anywhere, compile then save the module again. Then I close and reopen the database and hope that the problem has disappeared. If it hasn't then I can't help you.

The problem is not with any of your code I'm confident to say, but probably something you've done earlier, or that Access has mistakenly tracked you as having done earlier. I've had similar situations where typing in the same code again exactly as it was before, but after deleting the original and offending line, has resulted in the problem disappearing and all being hunky-dory going forward. That's another thing to try if the other advice yields nothing.

The issue doesn't appear to be very logical, so I can't offer any logical or reliable solution I'm afraid. Good luck anyway :-)
Jan 16 '12 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andy | last post by:
Hi... i'm trying to understand the concept of function name overloading in c++. to understand the resolving system it's important to understand the diffrent levels of typecasting (exact match,...
2
by: Larry | last post by:
I have the following code attached to a search button on a form that runs a query. It works great, except that the search for Last Name only returns exact matches. It is even case sensitive....
3
by: baka | last post by:
Dear Sir/madam Here i am having some one sql which returns more rows than the required rows after comaparing tables simple sql statement will be lokk like SELECT t.empcd, s.daicd, s.chucd,...
2
by: Wayne Shu | last post by:
Hi everyone. In the following program, foo is an ambiguous call. #include <iostream> using namespace std; void foo(int *); void foo(int (&));
2
by: Slippy27 | last post by:
I'm trying to modify a find/replace script which iterates through a file A and makes replacements defined in a csv file B. My original goal was to change any line in file A containing a search string...
6
by: Mr.SpOOn | last post by:
Hi, I'd like to use regular expressions to parse a string and accept only valid strings. What I mean is the possibility to check if the whole string matches the regex. So if I have: I can...
0
by: Lie Ryan | last post by:
On Sun, 26 Oct 2008 17:51:29 +0100, Mr.SpOOn wrote: re.compile('a*b*$') $ matches the end of a string, or in MULTILINE mode, the end of a line (right before newline) Symmetrically, ^...
4
by: nk28 | last post by:
I am having problem in re module of python. when i do a call of match function it returns true if first of the expression is true. For Eg :- Expression like (+()*) to denote ...
2
by: Neil Gilbert | last post by:
Ok i am fudging a legacy CRM system and i need to hide a button on the script using client side javascript. I am trying to hide a button on the screen based on the URL containing the word...
23
by: tetsuo2030 | last post by:
Hi all, I have two fields in a query: and . is a list of comma-separated paragraph numbers (like 1.0, 1.1, 1.1.2, etc); is just "1.0" or "1.1.2" or the like. I wanted to create a third field...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.