Link to home
Start Free TrialLog in
Avatar of andybrooke
andybrooke

asked on

remove spaces and commas in fields when a button is clicked

Hi EE
I want to remove  spaces, commas and brackets  ect from a field on the click of a button,

Example:  if i was to write  WA13 ,DL(4)   in a field, when i click a button it will look up this field and remove all unwanted charicters.
so it would read      WA13DL4        after the button is pressed.    Any idea on how to do this?
Avatar of andybrooke
andybrooke

ASKER

I have tried the follow:
Me.sample = Replace(Me.sample, "," , "")

And this works but when i wrote this:
Me.sample = Replace(Me.sample, "," Or "." Or "(" Or ")" Or "/" Or "?" Or "\" Or "*" Or " ", "")

I got debug.
Is there a way i can re-write above?
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to remove all non-letters and non-digits, I would use Regular Expressions.

1) Add this function to a regular VBA module:

Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True, _
    Optional MultiLine As Boolean = False)
    
    ' Function written by Patrick G. Matthews.  You may use and distribute this code freely,
    ' as long as you properly credit and attribute authorship and the URL of where you
    ' found the code
    
    ' For more info, please see:
    ' http://www.experts-exchange.com/articles/Programming/Languages/Visual_Basic/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
    
    ' This function relies on the VBScript version of Regular Expressions, and thus some of
    ' the functionality available in Perl and/or .Net may not be available.  The full extent
    ' of what functionality will be available on any given computer is based on which version
    ' of the VBScript runtime is installed on that computer
    
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll
    ' controls whether all instances of the matched string are replaced (True) or just the first
    ' instance (False)
    
    ' If you need to replace the Nth match, or a range of matches, then use RegExpReplaceRange
    ' instead
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    ' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
    ' where a large number of calls to this function are made, making RegX a static variable that
    ' preserves its state in between calls significantly improves performance
    
    Static RegX As Object
    
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
        .MultiLine = MultiLine
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    
End Function

Open in new window


2) In your form, add code like this to your button click event code:

If Nz(Me!MyTextbox, "") <> "" Then

    Me!MyTextbox = RegExpReplace(CStr(Me!MyTextbox), "[^A-Za-z0-9]")

End If

Open in new window


For more on Regular Expressions, please see:

https://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
Hi Capricorn
Thank you that worked PERFECT

to handle the error if nothing was entered in the textbox, use this revised code for the function


Function cleantext(s)
Dim j, vChar
If s & "" = "" Then cleantext = "": Exit Function
vChar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
For j = 1 To Len(s)
    If InStr(vChar, Mid(s, j, 1)) Then
        cleantext = cleantext & Mid(s, j, 1)
    End If
Next
End Function
You can fill a String with the characters to remove
Then you can remove such characters from the field, using Replace.
Supposing that you put in your form a button named cmdReplace and that your Field name is dex, I mean something like this
:
Private Sub cmdReplace_Click()
    Dim strRemoveChars As String
    strRemoveChars = " (),"
    Dim i As Integer
    
    For i = 1 To Len(strRemoveChars)
      dex = Replace(dex, Mid(strRemoveChars, i, 1), "")
    Next
    
    
End Sub

Open in new window

A simple way is to use VBA (not VB Script)

One method is to use the Replace() function

' remove spaces
Me.txtControlname = Replace(Me.txtControlname," ", "")

' remove bracket
Me.txtControlname = Replace(Me.txtControlname,"(", "")

etc.

 or use a User Defined Function:

Public Function strOnlyAlphaNumeric(strInData As String) As String

' keep  asc codes  48 to 57 and 65 to 122
Dim strOut As String
Dim i As Long
   
   strOut = ""
   For i = 1 To Len(strInData)
    
     If (Asc(Mid(strInData, i, 1)) >= 48 And Asc(Mid(strInData, i, 1)) <= 57) Or (Asc(Mid(strInData, i, 1)) >= 65 And Asc(Mid(strInData, i, 1)) <= 122) Then
       strOut = strOut + Mid(strInData, i, 1)
     End If
     
   Next i

  strOnlyAlphaNumeric = strOut
  

End Function

Open in new window


Example:

? strOnlyAlphaNumeric("WA13 ,DL(4)")
WA13DL4

Open in new window