replacing " function

1. Senior Member
Join Date
Dec 1969
Posts
181

## replacing " function

Hi i have a problem replacing " or chr(34) , i have written a function to replace each occurense with a double "" because i want to update information in a database... otherwise it wont work...<BR><BR><BR>the strange thing is that , if i replace the parm like this<BR><BR>iChar = Chr(34)<BR>camp_guideCompanyPres = Replace(camp_guideCompanyPres,iChar ,iChar & iChar )<BR><BR>but if i use my function it doesnt, whats wrong ?<BR><BR>personaly i think that the replace function dosnt find the char(34) and therefore arent able to replace it.. but why ?<BR><BR><BR>Function CampTrim(strInput)<BR><BR>Dim iChar<BR>iChar = Chr(34)<BR><BR> IF instr(strInput,"&#039;") Then<BR> strInput = Replace(strInput, "&#039;","&#039;&#039;")<BR> ELSEIF instr(strInput,"%") Then<BR> strInput = Replace(strInput, "%",CHR(37))<BR> ELSEIF instr(strInput,"&") Then<BR> strInput = Replace(strInput, "&",CHR(38))<BR> ELSEIF instr(strInput,iChar) Then<BR> strInput = Replace(strInput, iChar,iChar & iChar) <BR> END IF <BR><BR> CampTrim = Cstr(strInput)<BR> <BR>End Function<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## It should work...

But the code you show won&#039;t!<BR><BR>Do you understand what ELSEIF means?<BR><BR>It means *IF* you did *NOT* do the think above the ELSEIF (that is, the THEN clause) *THEN* you will make the check!<BR><BR>So if your string had, for example, an & in it, then you would *NEVER* see the " character to replace!<BR><BR>But you INSTR tests are bogus, anyway. What are they there for??? It&#039;s not an error to do a REPLACE the doesn&#039;t find anything. It takes just about as long to do the INSTR as it does to do the REPLACE, so why do both?<BR><BR>So you want simply:<BR><BR>Function CampTrim(strInput)<BR>&nbsp; &nbsp; Dim iChar<BR>&nbsp; &nbsp; iChar = Chr(34)<BR>&nbsp; &nbsp; strInput = Replace(strInput, "&#039;","&#039;&#039;")<BR>&nbsp; &nbsp; strInput = Replace(strInput, "%",CHR(37))<BR>&nbsp; &nbsp; strInput = Replace(strInput, "&",CHR(38))<BR>&nbsp; &nbsp; CampTrim = Replace(strInput, iChar,iChar & iChar) <BR>END Function<BR><BR>*******************<BR><BR>However ... THIS IS A MISTAKE!<BR><BR>If you are going to use this string in a SQL statement, you should double up *EITHER* the &#039; characters *OR* the " characters but never both!<BR><BR>SQL needs and wants *only* the characters you use as the string delimiters doubled.<BR><BR>So, for example, if your string is:<BR><BR>He said, "Don&#039;t replace both," emphatically.<BR><BR>Then you could use it in SQL *either* of these two ways:<BR><BR>INSERT INTO table ( someTextField )<BR>Values( &#039;He said, "Don&#039;&#039;t replace both," emphatically.&#039; )<BR><BR>*or*<BR><BR>INSERT INTO table ( someTextField )<BR>Values( "He said, ""Don&#039;t replace both,"" emphatically." )<BR><BR>But if you did this:<BR><BR>INSERT INTO table ( someTextField )<BR>Values( "He said, ""Don&#039;&#039;t replace both,"" emphatically." )<BR><BR>you would end up with &#039;&#039; in your db table! And if you did this:<BR><BR>INSERT INTO table ( someTextField )<BR>Values( &#039;He said, ""Don&#039;&#039;t replace both,"" emphatically.&#039; )<BR><BR>you would end up with two occurrences of "" in your table.<BR><BR>So...One or the other...to match the delimiters you use in the SQL...but never both.<BR><BR>Okay?<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
181

## RE: It should work...

thanx for the exstensiv answer!<BR><BR>much appritiated

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•