Results 1 to 6 of 6

Thread: ANOTHER string problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    8
    I need to get a first name and last name from a textbox such as:

    Whole$ = Text1.Text
    Pos% = InStr (Whole$, " ")
    FstName$ = Mid$(Whole$, Pos% + 1, InStr(Whole$, " ") - 1)
    LstName$ = Mid$(Whole$, Len(FstName$) + 2, Len(Whole$) - 1)

    I have to enter: -new johnnyboy nighty
    -new is the command
    johnnyboy is the first name
    nighty is the last name

    and I get:
    FstName$ -- john
    LstName$ -- johnnyboy nighty

    Whats wrong?

    P.S. I am sorry for posting so many messages in one day, but whenever a problem arises I can't seem to fix, there is no one I know that can help me.
    (sic) in the hed.

  2. #2
    Guest
    Put it into an Array and search it that way.

    Code:
    Private Sub Command1_Click()
    
        ArrayName = Split(Text1.Text, " ")
        For i = 0 To UBound(ArrayName)
            ArrName = ArrName & ArrayName(i) & vbCrLf
        Next i
        MsgBox ArrName
    
    End Sub

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

    <?>


    Code:
    'better coding practice to declare your variables
    'if you have VB6 you can do it this way.
    'same idea as Matthew..just a little more explanatory
    
    Option Explicit
    
    Private Sub Command1_Click()
    
        Dim sWhole As String, FstName As String
        Dim LstName As String
        Dim mySplit As Variant  'must always be a varian to hold a split
        
        sWhole = Text1.Text
        mySplit = Split(sWhole, " ")
        
    'the 3 elements of the split
        'mySplit(0) = new
        
        FstName = mySplit(1)
        LstName = mySplit(2)
        
        MsgBox "First Name is " & FstName
        MsgBox "Last Name is " & LstName
    
    End Sub
    
    Private Sub Form_Load()
        Text1 = "new johnnyboy nightly"
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    8
    Thank you all. My program is working now. It was easier to split into a vector (array).
    (sic) in the hed.

  5. #5
    Guest
    Did I forget to declare? Oops..

    Code:
    Dim ArrayName As Variant
    Dim ArrName As String
    Dim i As Integer
    ...I feel so much better now .

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

    <?>


    Code:
    'this is your way
    
    Private Sub Command1_Click()
        whole$ = Text1.Text
        Pos% = InStr(whole$, " ")
        whole = Right(whole, Len(whole) - Pos)
        Pos% = InStr(whole$, " ")
        LstName$ = Right(whole, Len(whole) - Pos)
        whole = Left(whole, Pos)
        FstName$ = whole
        
        MsgBox FstName
        MsgBox LstName
    
    End Sub
    
    Private Sub Form_Load()
      Text1 = "new johnnyboy nightly"
    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

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