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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find comma in string & split string 1

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hallo - I have a string with a set of coordinates in it, separated by a comma.
The x & y coordinate strings can be of varying length.
I need to be able to locate the comma in the string so that I can split the string & retrieve the numbers.
Can individual elements of a string be accessed?
& the length of a string, like in C++ (eg string str.length())?

All help, gratefully...

Cheers,
Douglas JL
Common sense is what tells you the world is flat.
 
Yes, you can do this. Let's assume your string will contain the following: 110,234, and that it is stored in a variable called strStringTest (from parent code or as a public variable, so not declared here)

You can use InStr to return the position of the comma, as follows:

Sub ExtractCoordinates ()

Dim intCommaPosition as Integer
Dim intStrLength as Integer
Dim strXCoord as String
Dim strYCoord as String

'InStr returns the position of a string within a string
intCommaPosition = InStr(strStringTest, ",")

'Len returns length of string
intStrLenght = Len(strStringTest)

'Left allows you to retrieve the leftmost characters
strXCoord = Left(strStringTest, intCommaPosition)

'Right returns rightmost characters
'Use length of string to find appropriate characters
strYcoord = Right(strStringTest, _
(intStrLength - intCommaPosition))

End Sub

This would extract the coordinates, regarless of the characters involved or the number of digits. You would have to do more work if there might be a space or other characters involved. You could convert these string values to integers if you needed them in that form for other purposes.

Hope this helps... (and hope I didn't make any errors! LOL)

Gerry
 
If you're using Access 2000 and above you could use the InStr function in your VBA. This takes the form:

Code:
InStr([Start], [String1], [String2], [ComparisonMethod])

Assuming you have a string called strCoords already, you could have:

Code:
Dim strX As String, strY As String
strX = Left$(strCoords, InStr(1, strCoords, ",", vbTextCompare)-1)
strY = Mid$(strCoords, InStr(1, strCoords, ",", vbTextCompare)+1)

Hope this helps. [pc2]
 
otherwise, in Access 2k, just look up "Split" in help and follow directions / examples. For Pre 2K, search for "basSplit" in these fora.

Either is a bit easier to use than the above approaches, although given the correct implementation, any should be useable.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Thanks alot, all - gpseymour's is the one I'll go with, as I'm working in Access 97, but thanks alot for all the help.
Cheers again,
Douglas JL Common sense is what tells you the world is flat.
 
gpseymour - thought you might like to know that the code you gave as an example works perfectly - thanks again!

DJL Common sense is what tells you the world is flat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top