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

InStr Function Does Not Work Per Documentation 2

Status
Not open for further replies.

GGleason

Technical User
Mar 22, 2001
321
US
I am running A97 SR-2.

The InStr function is not working per my understanding of the documentation, so I am wondering if I am doing something incorrect or if there is another issue lurking.
[tt]
strCurRec = "BOBS */BOB-###-.75 20 ATT PREF /BARNEYEE /CATE=BARNEY =0 =0 =0 $* */PA11112 01324"
intPos = InStr(61, strCurRec, " $* ")
[/tt]
I would expect [tt]intPos[/tt] to equal 77, but it is 137. Which means that the 61 indicated as the first arguement is not relavent to the function.

Has anyone seen this before? Can anyone help?

tia,
GGleason
 
yes... it is relavent. It tells the InStr function to skip any character before that position.

If there was a match before the 61st character, it would have been the number you would have gotten.

The InStr function returns the location of the first character of the match, starting the count from the first character of the string as 1, not starting from the character that you want to check from.

the reason (as I see it) that you can determine where to start looking is so that you can skip those matches that you already know about... as if you were looping through a string trying to find how many matches are in it.

GComyn
 
You misunderstood what "61" means. What it means is that Instr should begin searching for the character string starting at position 61. Therefore, if $* appeared, say twice, one starting at char. position 3 and one starting at char. position 137, Instr(61,...) would return 137, not 3.
 
I am posting what I have in the MS help for the InStr function example:

This example uses the InStr function to return the position of the first occurrence of one string within another.

Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".

' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(4, SearchString, SearchChar, 1)

 
Hi GGleason,

That's rather a confusing example. It returns 6 because there is a character "p" (lower case) in position 6 within the string. It is not returning 6 because there is a character "P" (Upper case) in the sixth position starting from and including position 4 which seeems to be how you have interpreted it.

"Textual" comparison does not distinguish between lower case and upper case. If you want to make that distinction then you must use binary comparison.

Enjoy,
Tony
 
.. or explicitly state vbBinaryCompare (or 0) as the fourth parameter on the Instr to override the default Compare Option.

Enjoy,
Tony
 
Thanks to Tony and Daniel for their comments and stars to you both! It has been helpful in the elucidation and clarification of the InStr function. My hope is that others may be helped from this posting.

Gratefully,
GGleason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top