Results 1 to 5 of 5
  1. #1
    sarcher is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2019
    Posts
    9

    Grab text of string until space

    I'd like a VBA code to grab the characters of a string until it gets to the first space character?

    example

    "Down in the valley"

    will return "Down"



    TIA

    Stephen

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Left(yourTextField,Instr(yourTextField," ") )

    or as a function

    Code:
    Function LeftOfSpace(txtIn As String) As String
    LeftOfSpace = Left(txtIn, InStr(txtIn, " "))
    End Function
    and a test routine

    Code:
    Sub sep10()
        Dim a As String: a = "down in the valley"
        Debug.Print LeftOfSpace(a)
    End Sub
    down

  3. #3
    sarcher is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2019
    Posts
    9
    perfect - thank you
    Stephen

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    another way is

    split(yourTextField," ") (0)

    but will only work in VBA, not in a query

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    shouldn't this be moved out of Knowledge Base / Code Repository?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 02-20-2018, 07:25 PM
  2. Grab letters after a particular string
    By Rustin788 in forum Queries
    Replies: 3
    Last Post: 08-02-2017, 11:55 AM
  3. Replies: 6
    Last Post: 05-29-2015, 10:21 AM
  4. Replies: 1
    Last Post: 07-26-2013, 12:20 PM
  5. Space between two text boxes
    By ashbear in forum Reports
    Replies: 3
    Last Post: 08-17-2011, 05:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums