
InStrRev not working as expected - bug?
The relevant code snippet
Function TidyUpName(Name)
Dim strFirstName, strLastName
Dim intFirstNameMark, intLastNameMark
Dim arrNames
Name = Trim(Name)
Wscript.Echo Name
If Instr(Name, ",") Then
intLastNameMark = Instr(Name, ",")
strLastName = Left(Name, intLastNameMark - 1)
intFirstNameMark = InstrRev(Name, ",")
strFirstName = Right(Name, intFirstNameMark - 1)
strFirstName = Trim(strFirstName)
TidyUpName = strFirstName & " " & strLastName
Else
arrNames = Split(Name)
strLastName = arrNames(0)
strFirstName = arrNames(1)
TidyUpName = Trim(strFirstName & " " & strLastName)
End If
End Function
The string parameter Name in the data source I am parsing is of the
form DAY, ROBERT or sometimes DAY ROBERT (no comma - hence the Else).
The bug I am finding is that Instr AND InstrRev are returning the same
value on all records and this cannot be right (except on records with
equal numbers of characters either side of the comma)
I even ended up echoing the results of each line of
intLastNameMark = Instr(Name, ",")
strLastName = Left(Name, intLastNameMark - 1)
intFirstNameMark = InstrRev(Name, ",")
strFirstName = Right(Name, intFirstNameMark - 1)
to the console and this confirmed my findings above.
I'm baffled. Anyone else experienced this?
Robert
ps I finally sidestepped the issue by swapping
intFirstNameMark = InstrRev(Name, ",")
with
intFirstNameMark = (len(Name) - intLastNameMark)
pps I can post full script if necessary.