Solved

remove spaces and commas in fields when a button is clicked

Posted on 2011-09-15
7
272 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
Comment Utility
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 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility

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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Closing Comment

by:andybrooke
Comment Utility
Hi Capricorn
Thank you that worked PERFECT
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility

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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now