how to find the first spca eon a srting (really eassy i just need urgent)

Posted on 2007-10-18
Last Modified: 2010-04-30
i have a string like this: Jhon Doe
how do i get to read that string and the until the space so in order words i need to get the part that display Jhon how do i do this?

i dont need a fix lenght of string because i have different records and all of them varies.
Question by:jsctechy
    LVL 8

    Expert Comment

    name=split("Jhon Doe"," ")(0)
    surname=split("Jhon Doe"," ")(1)
    LVL 35

    Expert Comment

    Hi jsctechy,

    You can also use InStr() to determine the position of the first space, then use Left() to extract what comes before it:

     Dim oldStr As String, newStr As String, i As Long
     oldStr = "Jhon Doe"
     i = InStr(1, oldStr & " ", " ") 'append space after oldstr in case there is no space
     newStr = Left(oldStr, i - 1)

    LVL 8

    Expert Comment

    Or you can do like this a (bit more complicated):

    fullname="Jhon Doe"
    name=left(fullname,instr(fullname," "))
    surname=right(fullname,len(fullname)-instr(fullname," "))
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Or you could just check the value returned from Instr() and act accordingly:

        Dim str As String
        Dim spcIndex As Integer
        Dim value As String
        str = "Jhon Doe"
        spcIndex = InStr(str, " ")
        If spcIndex > 0 Then
            value = Left(str, spcIndex - 1)
            value = str ' return the whole string since no space was found
        End If
        Debug.Print value
    LVL 1

    Author Comment

    i need something simple get the string and output it to message box
    thats all i need

    string = Jhon doe

    msgbox =>Jhon
    thats all i need
    LVL 8

    Accepted Solution

    string1 = "Jhon doe"
    msgbox(split(string1," ")(0))

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    734 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

    25 Experts available now in Live!

    Get 1:1 Help Now