Convert string to special format

Posted on 2009-02-16
Last Modified: 2012-05-06

I'm trying to convert the value of a textbox to something like this;


Where the value of the textbox can be numbers or letters, upper or lower case.


I enter 7m0343834ewrw and I want the result to be 7M0 343 834 EWRW.

How do I format MyTextBox?


Question by:jppinto
    LVL 25

    Expert Comment

    LEFT(MyTextBox.Text, 3) & " " & MID(MyTextBox.Text,4, 3) & " " & MID(MyTextBox.Text,7, 3) & " "  & MID(MyTextBox.Text,10,50)
    LVL 92

    Expert Comment

    by:Patrick Matthews
    In VB6...

    Dim strInput As String

    strInput = UCase(Replace(SomeForm.MyTextBox, " ", ""))
    strInput = Trim(Left(strInput, 3) & " " & Mid(strInput, 4, 3) & " " & Mid(strInput, 7, 3) & " " & Right(strInput, 4))

    MsgBox strInput
    LVL 92

    Expert Comment

    by:Patrick Matthews
    Need more coffee :)
    LVL 33

    Author Comment


    Public Property Left() As Integer' has no parameters and its return type cannot be indexed.

    LVL 48

    Accepted Solution

    If you want to use that way you need to change the Left() function to Microsoft.VisualBasic.Left() because otherwise you're using a Left() function from another namespace. This is a function that comes from older versions and you have to indicate the correct namespace for compatibility.
    Here's a .NET way:

        ''' <summary>
        ''' Converts to a specific format
        ''' </summary>
        ''' <returns></returns>
        Function formatString(ByVal str As String) As String
                Dim part1 As String = str.Substring(0, 3).ToUpper
                Dim part2 As String = str.Substring(3, 3).ToUpper
                Dim part3 As String = str.Substring(6, 3).ToUpper
                Dim part4 As String = str.Substring(9).ToUpper
                Return String.Format("{0} {1} {2} {3}", part1, part2, part3, part4)
            Catch ex As Exception
                Return String.Empty
            End Try
        End Function
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String = "7m0343834ewrw"
        End Sub

    Open in new window

    LVL 33

    Author Closing Comment

    Thanks jpaulino. Now it's working!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    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

    17 Experts available now in Live!

    Get 1:1 Help Now