0

I have an Access DB with a few tables, one of which has a column of address. These addresses all begin with a building name and are followed by city information. They are all separated by commas.

I am looking for a way in the VB code to take this field and separate it into two fields, with everything left of the first comma in one field and everything right of the first comma in the other.

I've tried variations on the following:

currentBuildingName = currentBuildingAddress.Substring(0, currentBuildingAddress.IndexOf(","))
currentCity = currentBuildingAddress.Substring(currentBuildingAddress.IndexOf(","), 1)

But with no success. I've also tried looking at the left() and right() functions, but they require you to specify the integer location of where the split should occur, which is variable.

Any advice folks? How can I split a string based on the first comma, with everything to the left in one field and everything to the right in another field, in MS Access 2010?

2 Answers 2

3

Use the InStr() function, InStr("expression_to_search", "search_string"). It returns the starting position of the string you're searching for or zero if it doesn't find it. Once you've got the position of the comma you can use Right() and Left() to parse the string. For more details see

InStr Function.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah this is the way to go. I'll mark this as accepted, but post my end result as a separate answer. Thanks
0

Thanks to @OTTA for the guidance. This was my final answer:

startingPosition = InStr(currentBuildingAddress, ",")

currentBuildingName = Left(currentBuildingAddress, (startingPosition))
currentCity = Right(currentBuildingAddress, Len(currentBuildingAddress) - startingPosition)

Nice and simple.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.