Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 15th, 2003, 02:18 PM
Registered User
 
Join Date: Jul 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Replacing Text with a String in a Form

I have users enter information into a vba form in word. Once the user hits ok on the form, I want the code to check the string that they entered in that field (which is a phone number) and replace any "-" with "." and then check for "(" or ")" and replace with "." Once the text is replaced, I want it to enter the "revised" string into the document. For example text entered as 999-999-999 or (999) 999-999 should be 999.999.999

Thanks in advance

 
Old July 15th, 2003, 02:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

check out the REPLACE command.
 
Old August 14th, 2003, 12:37 PM
Authorized User
 
Join Date: Aug 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

newgeek,

I find functions to be a fast and efficient way to go and would simply use the following code:

Public Sub Main()
strPhone = txtPhoneNum.Text
strPhone = ReplaceWithin(strPhone, "-", ".")
strPhone = ReplaceWithin(strPhone, "(", ".")
strPhone = ReplaceWithin(strPhone, ")", ".")

Selection.TypeText strPhone
End Sub

' ************************************************** ***************************
' Function to Replace Within with NumberToReplace as Optional Last Parameter
' ************************************************** ***************************
Public Function ReplaceWithin(ByVal strString As String, ByVal strFind As String, ByVal strReplace _
                                                        As String, Optional ByVal NumberToReplace As Integer) As String

' Purpose: To replace all occurrences of one string in another
Dim intPos As Integer
Dim intLP As Integer
Dim intLen As Integer
Dim strTemp As String
Dim intCount As Integer
intCount = 0

' find each search string and replace with target
intLen = Len(strFind)
intPos = InStr(strString, strFind)
While intPos <> 0
    If NumberToReplace > 0 And NumberToReplace = intCount Then
        GoTo LOOP_ESCAPE
    End If
    strTemp = strTemp & Left$(strString, intPos - 1)
    strTemp = strTemp & strReplace
    strString = Right$(strString, Len(strString) - intPos - intLen + 1)
    intPos = InStr(strString, strFind)
    intCount = intCount + 1
Wend

' append remainder upon failure of last InStr search
LOOP_ESCAPE:
strTemp = strTemp & strString
ReplaceWithin = strTemp
End Function


For more info recommend http://www.vba-programmer.com

CarlR








Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing multiple characters in a string philboparker BOOK: XSLT Programmer's Reference, 2nd Edition 0 May 20th, 2008 04:38 PM
Replacing text in text box inside table Larry Landis Word VBA 0 December 27th, 2007 12:03 PM
replacing numbers in a string cole SQL Server 2000 4 April 2nd, 2007 04:24 PM
Replacing characters in a string semilemon C# 2005 2 June 16th, 2006 11:31 PM
Replacing a character from string itHighway Classic ASP Basics 5 March 14th, 2005 11:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.