ACC2000: How to Strip Specific Characters from a String
ID: Q210227
|
The information in this article applies to:
Moderate: Requires basic macro, coding, and interoperability skills.
This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).
SUMMARY
This article shows you how to create a custom function to strip invalid
characters or characters that you do not want from a string. You can use
this function in queries and in text input boxes.
Microsoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the
following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
MORE INFORMATION
To strip invalid characters or characters that you do not want from a
string, follow these steps:
- Create a module and type the following line in the Declarations
section if it is not already there:
Option Explicit
- Type the following procedure:
' --------------------------------------------------
' Function StripString()
'
' Returns a string minus a set of specified chars.
' --------------------------------------------------
Function StripString(MyStr As Variant) As Variant
On Error GoTo StripStringError
Dim strChar As String, strHoldString As String
Dim i As Integer
' Exit if the passed value is null.
If IsNull(MyStr) Then Exit Function
' Exit if the passed value is not a string.
If VarType(MyStr) <> 8 Then Exit Function
' Check each value for invalid characters.
For i = 1 To Len(MyStr)
strChar = Mid$(MyStr, i, 1)
Select Case strChar
Case ".", "#", ",", "-"
' Do nothing
Case Else
strHoldString = strHoldString & strChar
End Select
Next i
' Pass back corrected string.
StripString = strHoldString
StripStringEnd:
Exit Function
StripStringError:
MsgBox Error$
Resume StripStringEnd
End Function
- To test this function, type the following line in the Immediate window, and then press ENTER:
? StripString("Test.#,-")
Note that the function prints the word "Test" in the Immediate window. The invalid characters (".#,-") are successfully stripped from the test string.
REFERENCES
For more information about information about string manipulation, click Microsoft Access Help on the
Help menu, type "Len" in the Office Assistant or the Answer Wizard,
and then click Search to view the topics returned.
Additional query words:
remove removing removes
Keywords : kbprg kbdta AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbinfo