|
|
SynopsisThe following function removes any illegal characters from the passed string and optionally replaces them with a user entered character. Add (or remove) any other characters you want to check for to strIllegalChars. Related Functions: GetAlphaOnly and GetDigitsOnly CODE ' Strip Illegal Characters
' http://www.utteraccess.com/wiki/index.php/Strip_Illegal_Characters ' Code courtesy of UtterAccess Wiki ' Licensed under Creative Commons License ' http://creativecommons.org/licenses/by-sa/3.0/ ' ' You are free to use this code in any application, ' provided this notice is left unchanged. ' ' rev date brief descripton ' 1.0 2010-10-23 Writing files to disk that contain illegal file characters can cause sometimes obscure error message(s) ' Public Function fStripIllegal(strCheck As String, Optional strReplaceWith As String = "") As String On Error GoTo StripIllErr 'illegal file name characters included in default string are ? [ ] / = + < > :; * " , ' Dim intI As Integer Dim intPassedString As Integer Dim intCheckString As Integer Dim strChar As String Dim strIllegalChars As String Dim intReplaceLen As Integer If IsNull(strCheck) Then Exit Function strIllegalChars = "?[]/=+<>:;,*" & Chr(34) & Chr(39) 'add/remove characters you need removed to this string intPassedString = Len(strCheck) intCheckString = Len(strIllegalChars) intReplaceLen = Len(strReplaceWith) If intReplaceLen > 0 Then 'a character has been entered to use as the replacement character If intReplaceLen = 1 Then 'check the character itself isn't an illegal character If InStr(strIllegalChars, strReplaceWith) > 0 Then MsgBox "You can't replace an illegal character with another illegal character", _ vbOKOnly + vbExclamation, "Invalid Character" fStripIllegal = strCheck Exit Function End If Else 'only one replacement character allowed MsgBox "Only one character is allowed as a replacement character", _ vbOKOnly + vbExclamation, "Invalid Replacement String" fStripIllegal = strCheck Exit Function End If End If If intPassedString < intCheckString Then For intI = 1 To intCheckString strChar = Mid(strIllegalChars, intI, 1) If InStr(strCheck, strChar) > 0 Then strCheck = Replace(strCheck, strChar, strReplaceWith) End If Next intI Else For intI = 1 To intPassedString strChar = Mid(strIllegalChars, intI, 1) If InStr(strCheck, strChar) > 0 Then strCheck = Replace(strCheck, strChar, strReplaceWith) End If Next intI End If fStripIllegal = Trim(strCheck) StripIllErrExit: Exit Function StripIllErr: MsgBox "The following error occured: " & Err.Number & vbCrLf _ & Err.Description, vbOKOnly + vbExclamation, "Unexpected Error" fStripIllegal = strCheck Resume StripIllErrExit End Function
|
| This page was last modified 09:08, 6 April 2011. This page has been accessed 1,178 times. Disclaimers |