Function InStr with Japanese Windows: strange return value

Dear all,

I have found a strange behavior of VBA InStr function in Japanese language in Japanese Windows.

(WinXP Pro SP2, Office 2003 Pro)

InStr function: InStr([start, ]string1,string2[,compare])

While a Japanese Katakana character with "tenten" or "maru" appears in a string1, InStr(string1, string2) returns a wrong result:

sTest="ヘルプabc"

InStr(sTest, "a") = 5 (should be 4)

(while: Len(sTest) = 6; Mid(sTest, 4) = "abc"; LenB(sTest) = 12 )

However, if I use it with vbBinaryCompare, it returns a right value:

InStr(1, sTest, "a", vbBinaryCompare) = 4.

Furthermore, it does not happen with Windows XP English version.

Do you have any idea? Is this a bug?

Thank you.

[864 byte] By [gcsman] at [2008-1-10]
# 1

I would advise you to use the binary comparison

If you are worried about the case sensitveness, change the case of your string by lcase and ucase functions

Cheers

Shasur

Shasur at 2007-10-3 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 2

Dear Shasur,

Thank you for your advice.

In fact, I had to change to binary comparison in every call to InStr in my application. However I am curious about the root cause. How VBA can return that strange value when it is so simple to return the right value for UCS-2, where each character consumes 2 bytes (PosInChar=PosInByte/2).

Regards.

Gcsman

gcsman at 2007-10-3 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...