Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Procedure to Split String

Status
Not open for further replies.

MrTBC

Technical User
Joined
Nov 19, 2003
Messages
610
Location
US
Access 2000

I need to write some VBA code to split one string (strName) into two (strFirst and strLast). The code needs to check if the string contains a space, and if it does split the string into two strings at the point of the first space found.

e.g.
strName strFirst strLast
"John" "John"
"John Smith" "John" "Smith"
"John Smith Childs" "John" "Smith Childs"

Many Thanks.
 
Look at the Split function

Something like:

arrNameParts = Split(strName, " ", 2)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
The above would seperate Smith & Childs. Something like I've outlined below should work, but are you 100% sure you could never have an Anne Marie for example?

strFirst=IIf(Instr(strName," "),Left(strName,Instr(strName," ")), strName)

strLast = IIf(Instr(strName," "),Right(strName,Len(strName)-Instr(strName," ")), "")


"Your rock is eroding wrong." -Dogbert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top