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

Instr

Status
Not open for further replies.

exodus300

Programmer
Mar 22, 2002
262
0
0
AU
Running Microsoft-IIS/5.1 on XP Pro

I'm trying to use Instr and Replace to replace each occurrence of a particular string from a message, like this:

Code:
Function FormatMessage(strSource)
	Set rs_format = Server.CreateObject("ADODB.RecordSet")
	rs_format.Open "SELECT * FROM Smileys", db
	Do While Not rs_format.EOF
		strSmileyCode = rs_format("SmileyCode")
		strFileName = rs_format("FileName")
		strAltText = rs_format("Description")
		Do While Instr(0, strSource, strSmileyCode, 1) > 0
			' Replace the smiley code with the <img> tag
			strSource = Replace(strSource, strSmileyCode, "<img src=""images/smileys/""" & strFileName & """ alt=""" & strAltText & """>", , , 1)
		Loop
		rs_format.MoveNext
	Loop
	rs_format.Close
	Set rs_format = Nothing
	
	' Return the formatted message
	FormatMessage = strSource
End Function

However, I get the following message:

Microsoft VBScript runtime (0x800A0005)
Invalid procedure call or argument: 'Instr'
/forum/formatmessage.asp, line 10


Please help :)

[Thanks in advance|Hope I helped you]
Exodus300
[pc3]
 
Hello exodus300,

It's 1-base for the start position.
Code:
Do While Instr(1, strSource, strSmileyCode, 1) > 0
regards - tsuji
 
Add some response.write code for debugging to see what strSource and strSmileyCode contain.
It may one of those is an empty string.

Chris.

Indifference will be the downfall of mankind, but who cares?
 
I just thought... does Replace replace every instance of something in a string, or just the first one? cuz if it does all then I won't need the Instr loop.

But I'll try what you said and get back to you.

[Thanks in advance|Hope I helped you]
Exodus300
[pc3]
 
Replace will do with every instance in one pass so as you say the instr loop would be pretty much redundant.

and yes I missed the 0 instead of 1

rearrange "wood can't the see trees for the" into a well known phrase or saying. [ponder]




Chris.

Indifference will be the downfall of mankind, but who cares?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top