Solved

remove spaces and commas in fields when a button is clicked

Posted on 2011-09-15
7
282 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 500 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 92

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculation in Access 5 26
Create report using crosstab query 11 29
Access Changing Number to Date with Seperator 5 21
Cascading Combo boxes between 2 sub navigation forms 1 15
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

809 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