Results 1 to 6 of 6

Thread: Reversing variable in textbox: HELP!!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    6

    Question

    HI There!!
    I'm having a problem with my program. I'm a beginner, so it's been a little confusing for me, using Visual Basic 5.
    My problem is this: In my program the user enters his or her name into Text1.Text like this: Jim Smith or Jim, Smith or whomever they are.
    Next, I have to reverse the name so that the last name comes first like this: Smith, Jim or Smith Jim, using Command1 to reverse it. I am having so much problems with this still, can anyone help me? It's just this one little thing to finish!
    It would be the greatest thing to get some coding that will work in Visual Basic 5!!
    Thanks

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'this will work if names are entered with spaces
    'Ie.  Jim Jones   Larry Smith  will not work without modifications if
    'you enter Jim,Jones or Larry-Smith
    'it is split based on a space between names and only using 2 names
    'first and last
    
    Public Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
        Dim sParts() As String
        Dim lParts As Long
        Dim lPos As Long
        
        lPos = InStr(sString, sSeparator)
        While lPos
        ReDim Preserve sParts(lParts)
        sParts(lParts) = Left(sString, lPos - 1)
        sString = Mid(sString, lPos + Len(sSeparator))
        lPos = InStr(sString, sSeparator)
        lParts = lParts + 1
        Wend
        If Len(sString) Then
        ReDim Preserve sParts(lParts)
        sParts(lParts) = sString
        End If
        Split2 = IIf(lParts, sParts, Array())
    End Function
    
    
    Private Sub Command1_Click()
        Dim sSplitMe As Variant
        Dim myString As String
        myString = Text1.Text
        
        sSplitMe = Split2(myString, " ")
        Text1 = sSplitMe(UBound(sSplitMe)) & " " & sSplitMe(LBound(sSplitMe))
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Fanatic Member Bonker Gudd's Avatar
    Join Date
    Mar 2000
    Location
    Saturn
    Posts
    748
    I'd use "InStr" to find the space and then use "Left", "Mid" and "Right" to split up the string.

    Use help, click F1 when the cursor is on the command you want help with.

  4. #4
    Member
    Join Date
    Jan 2001
    Location
    Dublin
    Posts
    35
    I am not sure whether or not this code works with V5, it probably does.

    Dim strFirstName As String, strSecondName As String

    Dim IsThereaComma As Boolean, lngCommaPos As Long

    Dim lngSpacePosition As Long

    lngCommaPos = InStr(1, Text1, ",")

    If lngCommaPos = 0 Then

    IsThereaComma = False

    lngSpacePosition = InStr(1, Text1, " ")

    End If

    If lngCommaPos <> 0 Then

    strFirstName = Left$(Text1, lngCommaPos - 1)

    strSecondName = Right$(Text1, Len(Text1) - lngCommaPos)

    Text2 = Trim$(strSecondName & ", " & strFirstName)

    Else

    strFirstName = Left$(Text1, lngSpacePosition - 1)

    strSecondName = Right$(Text1, Len(Text1) - lngSpacePosition)

    Text2 = Trim$(strSecondName & " " & strFirstName)

    End If

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    6

    Thanks you guys!!

    Hey, thanks for all the help from those that replied. I'll try it once I get to school. (I don't have VB5 at home). I am no longer stressed out about it, it was worrying me before. Thanks again

  6. #6
    Lively Member
    Join Date
    Aug 2000
    Location
    Dullawhere
    Posts
    100

    Question why not...

    why don't you make two or even three boxes for first name, last name, and middle intitals, that away when they hit command1...
    assuming you re-asemble their name into Text4 and Text1 is First Name, Text2 is Middle Name, and Text3 is Last name

    Code:
    Text4.text = Text3.Text & ", " & Text1.Text & " " & Text2.text
    That would be your easiest solution
    I am stupid..but I AM 15 !
    *a line from a song i wrote*
    """"""""""""""""""""""""""""""""""""""""
    so i came to say life bites away
    whats done is done the past is gone
    i cant believe the sh*t I know
    i always wish it would just go
    """"""""""""""""""""""""""""""""""""""""
    "remember you Ps And Qs Boy !"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width