Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

remove spaces and commas in fields when a button is clicked

Posted on 2011-09-15
7
Medium Priority
?
289 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:andybrooke
7 Comments
 

Author Comment

by:andybrooke
ID: 36544092
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?
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 36544095

try this codes

private sub btn_click()

me.text0= cleanText(me.text0)

end sub

Function cleantext(s)
Dim j, vChar
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
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 36544098
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:

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Closing Comment

by:andybrooke
ID: 36544114
Hi Capricorn
Thank you that worked PERFECT
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36544118

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
0
 
LVL 7

Expert Comment

by:kemi67
ID: 36544129
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

0
 
LVL 21
ID: 36544153
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





0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question