Using InStr Function to Parce a string

L

Lisab

In inStr function and using it has always caused me a headache.

I did the following to get the [personName] from the datafile path. I would
like to know if there was an easier way to accomplish the same.
------------------------------------------------------------
'Get the Person name from the file path
'example C:\ProjectName_ContractCode_\DatabaseName_PersonName.mdb

Dim CPos, LastPos, DPos, UPos, nameLength As Integer

'Get the position of the .dot
DPos = InStr(strPath, ".")

'There are several underscores in the path
'I know the name comes after the last underscore
'Get the position of the last underscore
CPos = InStr(strPath, "_")
Do While CPos > 0
LastPos = CPos
CPos = InStr(CPos + 1, strPath, "_")

Loop

'the length of the name is the distance between the last underscore and the
dot
nameLength = DPos - LastPos - 1

strName = Right$(strPath, (nameLength + 4))
strName = Left$(strName, nameLength)
 
K

Ken Snell \(MVP\)

Perhaps this will be easier, based on the example that has underscore before
person's name:

Left(Mid(strPath, InStrRev(strPath, "_") + 1), Len(Mid(strPath,
InStrRev(strPath, "_") + 1)) - 4)
 
L

Lisab

THANKS.

OMG, I have to be fresh and headach free in order to deal with mixing the
Left,Right, Mid, & InStr functions :)

InStrRev --> that a new one I didn't know about

Thanks again.

Ken Snell (MVP) said:
Perhaps this will be easier, based on the example that has underscore before
person's name:

Left(Mid(strPath, InStrRev(strPath, "_") + 1), Len(Mid(strPath,
InStrRev(strPath, "_") + 1)) - 4)

--

Ken Snell
<MS ACCESS MVP>



Lisab said:
In inStr function and using it has always caused me a headache.

I did the following to get the [personName] from the datafile path. I
would
like to know if there was an easier way to accomplish the same.
------------------------------------------------------------
'Get the Person name from the file path
'example C:\ProjectName_ContractCode_\DatabaseName_PersonName.mdb

Dim CPos, LastPos, DPos, UPos, nameLength As Integer

'Get the position of the .dot
DPos = InStr(strPath, ".")

'There are several underscores in the path
'I know the name comes after the last underscore
'Get the position of the last underscore
CPos = InStr(strPath, "_")
Do While CPos > 0
LastPos = CPos
CPos = InStr(CPos + 1, strPath, "_")

Loop

'the length of the name is the distance between the last underscore and
the
dot
nameLength = DPos - LastPos - 1

strName = Right$(strPath, (nameLength + 4))
strName = Left$(strName, nameLength)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top