?
Solved

remove spaces and commas in fields when a button is clicked

Posted on 2011-09-15
7
Medium Priority
?
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

719 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