|
Question : Strip special character function for SQL statement
|
|
Access 2003 I need to Strip special characterfrom a field for SQL statement
SELECT tblData.fldDId, tblData.fldWwg, tblData.fldMfgname, tblData.fldMfgnameOrig, tblData.fldMfrnum, tblData.fldMfrnumOrig, tblData.fldDescription, tblData.fldDescriptionOrig, tblData.fldDelete, tblData.fldEXtype, tblCoreSkuInformation.ITEM, tblCoreSkuInformation.WWGMFRNUM, tblCoreSkuInformation.WWGMFRNAME, tblCoreSkuInformation.WWGDESC FROM tblData INNER JOIN tblCoreSkuInformation ON (tblData.fldMfgname = tblCoreSkuInformation.WWGMFRNAME ) AND (tblData.fldMfrnum = tblCoreSkuInformation.WWGMFRNUM);
While performing the search I need the special characters stripped from WWGMFRNUM , BUT leave the data intact in the field in the database Is this possible ? sometimes (tblData.fldMfrnum maybe HW1518 AND = tblCoreSkuInformation.WWGMFRNUM maybe HW-1518 I just need the special characters removed while performing the searches
Thanks fordraiders
|
|
Answer : Strip special character function for SQL statement
|
|
Here is a more general purpose UDF; I added a few lines to handle the "special characters" you want to suppress:
Function TranslateChar(StrIn As String) As String
' This function may be used in Excel or Access, or in any VB/VBA project. ' Function evaluates an ANSI string that may have special characters, identified ' in the collection populated below. If a special character is found, the function ' replaces that character with a designated replacement string (may be any number ' of characters). There is no support for Unicode. ' The function conserves case, so if the special character is uppercase, then the ' first character of the replacement string will be uppercase as well. ' While the intent of this function is to "replace" characters with diacritical ' marks with their Roman alphabet equivalents (you should feel free to change the ' mapping below if you do not think it's right or it does not suit your purposes; ' I am no linguist). However, you could use the code to replace any single ANSI ' character with whatever string you desire. Dim Counter As Long Dim coll As New Collection Dim Check As String Dim WasLower As Boolean Dim Letter As String ' Populate a Collection with the mapping. The Key is the special character, and the ' Item is the replacement. The key must always be a single character, but the item ' may be 1+ characters. Use lower case in this list, and continue the list as ' needed. coll.Add Item:="a", Key:="á" coll.Add Item:="a", Key:="à" coll.Add Item:="a", Key:="â" coll.Add Item:="a", Key:="ã" coll.Add Item:="a", Key:="å" coll.Add Item:="a", Key:="ä" coll.Add Item:="ae", Key:="æ" coll.Add Item:="c", Key:="ç" coll.Add Item:="e", Key:="è" coll.Add Item:="e", Key:="é" coll.Add Item:="e", Key:="ê" coll.Add Item:="e", Key:="ë" coll.Add Item:="i", Key:="ì" coll.Add Item:="i", Key:="í" coll.Add Item:="i", Key:="î" coll.Add Item:="i", Key:="ï" coll.Add Item:="n", Key:="ñ" coll.Add Item:="o", Key:="ó" coll.Add Item:="o", Key:="ò" coll.Add Item:="o", Key:="õ" coll.Add Item:="o", Key:="ö" coll.Add Item:="o", Key:="ø" coll.Add Item:="oe", Key:="" coll.Add Item:="ss", Key:="ß" ' German sharp s coll.Add Item:="th", Key:="ð" ' Old English eth coll.Add Item:="th", Key:="þ" ' Old English thorn coll.Add Item:="u", Key:="ù" coll.Add Item:="u", Key:="ú" coll.Add Item:="u", Key:="û" coll.Add Item:="u", Key:="ü" coll.Add Item:="y", Key:="ý" coll.Add Item:="y", Key:="ÿ" ' This entry is for illustration only! You should remove before using! 'coll.Add Item:="dollar", Key:="$" ' "special keyboard characters" coll.Add Item:="", Key:="~" coll.Add Item:="", Key:="!" coll.Add Item:="", Key:="@" coll.Add Item:="", Key:="#" coll.Add Item:="", Key:="$" coll.Add Item:="", Key:="%" coll.Add Item:="", Key:="^" coll.Add Item:="", Key:="&" coll.Add Item:="", Key:="*" coll.Add Item:="", Key:="(" coll.Add Item:="", Key:=")" coll.Add Item:="", Key:="_" coll.Add Item:="", Key:="+" coll.Add Item:="", Key:="-" coll.Add Item:="", Key:="=" coll.Add Item:="", Key:="`" coll.Add Item:="", Key:="{" coll.Add Item:="", Key:="}" coll.Add Item:="", Key:="|" coll.Add Item:="", Key:=":" coll.Add Item:="", Key:="""" coll.Add Item:="", Key:="<" coll.Add Item:="", Key:=">" coll.Add Item:="", Key:="?" coll.Add Item:="", Key:="," coll.Add Item:="", Key:="." coll.Add Item:="", Key:="/" coll.Add Item:="", Key:="\" coll.Add Item:="", Key:=";" coll.Add Item:="", Key:="'" coll.Add Item:="", Key:="[" coll.Add Item:="", Key:="]" ' Loop through string to look for special characters needing replacement For Counter = 1 To Len(StrIn) ' Look in collection to see if the current character being considered is a "special" ' character On Error Resume Next Letter = Mid(StrIn, Counter, 1) Check = coll(Letter) ' Check to see if original character was upper or lower case WasLower = (StrComp(Letter, LCase(Letter), vbBinaryCompare) = 0) ' If there was no error, that means character was in collection and thus is a ' special character needing replacement If Err <> 0 Then Err.Clear Check = Letter End If On Error GoTo 0 ' If character was lower case, return the translation in lower case. If upper case, ' return in proper case (first character capitalized) TranslateChar = TranslateChar & IIf(WasLower, LCase(Check), StrConv(Check, vbProperCase)) Next ' Release object variable Set coll = Nothing End Function
Patrick
|
|
|
|