Filtering records with question mark at the end | Newbie | | Join Date: Sep 2006
Posts: 16
| |
Can anyone help with filtering records ended with "?".
here is the code I have tried: - strWhere = strWhere & "([stock_site] not like ""*" & "'?'"") AND "
-
strWhere = strWhere & "([goods_status] not Like ""*" & "scrap" & "*"") AND "
Not working at all. Any help is appreciated.
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,218
| | | re: Filtering records with question mark at the end Quote:
Originally Posted by Jiwei06xie Can anyone help with filtering records ended with "?".
here is the code I have tried: - strWhere = strWhere & "([stock_site] not like ""*" & "'?'"") AND "
-
strWhere = strWhere & "([goods_status] not Like ""*" & "scrap" & "*"") AND "
Not working at all. Any help is appreciated. Here is one of several Methods: - strWhere & "Where Not Right(TableName.[stock_site], 1) = '?' AND "
| | Moderator | | Join Date: Feb 2008 Location: Beauly, near Inverness, Scotland
Posts: 1,576
| | | re: Filtering records with question mark at the end
Hi, and Welcome to Bytes!
As you have found, the question mark character is a bit of a problem to use within a Where clause, as it is a wildcard character with a special meaning. There is a means to overcome it, however, using the InStr function, which returns the position of a character within the string if it exists, or 0 if it doesn't.
Your Where clause can be recast as - strWhere = "WHERE Instr([Stock_Site], " & Chr(63) & ") > 0"
(Chr(63) is just the questionmark character referred to by converting its ASCII code.)
This will match if the questionmark occurs anywhere in the string. If you need it to be the end of the string only - and I would caution that this would preclude strings matching if it is anywhere else - you could use - strWhere = "WHERE Instr([Stock_Site], " & Chr(63) & ") = Len([Stock_Site])"
-Stewart
| | Newbie | | Join Date: Sep 2006
Posts: 16
| | | re: Filtering records with question mark at the end
Thank you very much Stewart for your reply.
I will have a try later today.
Kindest regards, Jiwei Quote:
Originally Posted by ADezii Here is one of several Methods: - strWhere & "Where Not Right(TableName.[stock_site], 1) = '?' AND "
| | Newbie | | Join Date: Sep 2006
Posts: 16
| | | re: Filtering records with question mark at the end
It's a magic. Working perfect with my case.
Thank you very much Dezii.
Regards, Jiwei Quote:
Originally Posted by ADezii Here is one of several Methods: - strWhere & "Where Not Right(TableName.[stock_site], 1) = '?' AND "
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,218
| | | re: Filtering records with question mark at the end Quote:
Originally Posted by Stewart Ross Inverness Hi, and Welcome to Bytes!
As you have found, the question mark character is a bit of a problem to use within a Where clause, as it is a wildcard character with a special meaning. There is a means to overcome it, however, using the InStr function, which returns the position of a character within the string if it exists, or 0 if it doesn't.
Your Where clause can be recast as - strWhere = "WHERE Instr([Stock_Site], " & Chr(63) & ") > 0"
(Chr(63) is just the questionmark character referred to by converting its ASCII code.)
This will match if the questionmark occurs anywhere in the string. If you need it to be the end of the string only - and I would caution that this would preclude strings matching if it is anywhere else - you could use - strWhere = "WHERE Instr([Stock_Site], " & Chr(63) & ") = Len([Stock_Site])"
-Stewart Hello Stewart, I think the OP was specifically looking for values where the '?' appeared at the end of the String. In this case the Instr() Function would not work where there were > 1 '?', the '?' did not appear as the last character, or the '?' appeared in a position other than the last. Kindly correct me if I am wrong.
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,218
| | | re: Filtering records with question mark at the end Quote:
Originally Posted by Jiwei06xie It's a magic. Working perfect with my case.
Thank you very much Dezii.
Regards, Jiwei We do try to work magic whenever we can! (LOL).
| | Moderator | | Join Date: Feb 2008 Location: Beauly, near Inverness, Scotland
Posts: 1,576
| | | re: Filtering records with question mark at the end
Hi ADezii. The OP did indeed say that the question mark was at the end of the string, in which case using InStr and matching against Len (my second suggestion) would indeed fail if there is more than one questionmark (as InStr would return the position of the first occurrence only). I had pointed out that it would not work if the question mark was anywhere else, and it was implicit in my reply (but not spelt out clearly) that this would include use of more than one question mark.
Personally, I am not keen on use of user-entered marker characters that rely on the user placing the character in a specific position. Users are only human and will make mistakes, and such mistakes should not stop other routines from working.
For example, it is easy to type questionmark-space by mistake at the end of a string - and nearly impossible for the user concerned to spot. Even using RTrim would not guarantee a match, as the user could by mistake type something invalid that did not end in a space, like "?." - there are so many possibilities. A solution matching the last character will always fail in these circumstances, which is why my first suggestion - and it was first because it was the one I preferred - was to match for the questionmark anywhere in the string. This would not be dependent on whether or not there was just one question mark, or where it occurred. The OP has not said whether or not there is just one question mark, and whether or not end of text is the only place it could be, hence the provision of two solutions in my post...
-Stewart
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,218
| | | re: Filtering records with question mark at the end Quote:
Originally Posted by Stewart Ross Inverness Hi ADezii. The OP did indeed say that the question mark was at the end of the string, in which case using InStr and matching against Len (my second suggestion) would indeed fail if there is more than one questionmark (as InStr would return the position of the first occurrence only). I had pointed out that it would not work if the question mark was anywhere else, and it was implicit in my reply (but not spelt out clearly) that this would include use of more than one question mark.
Personally, I am not keen on use of user-entered marker characters that rely on the user placing the character in a specific position. Users are only human and will make mistakes, and such mistakes should not stop other routines from working.
For example, it is easy to type questionmark-space by mistake at the end of a string - and nearly impossible for the user concerned to spot. Even using RTrim would not guarantee a match, as the user could by mistake type something invalid that did not end in a space, like "?." - there are so many possibilities. A solution matching the last character will always fail in these circumstances, which is why my first suggestion - and it was first because it was the one I preferred - was to match for the questionmark anywhere in the string. This would not be dependent on whether or not there was just one question mark, or where it occurred. The OP has not said whether or not there is just one question mark, and whether or not end of text is the only place it could be, hence the provision of two solutions in my post...
-Stewart Makes perfect sense to me Stewart, thanks for the explanation.
|  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|