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

How Can I Assign An HTML Tag to a VB Constant?

Status
Not open for further replies.

MiniMe2

Programmer
May 19, 2002
15
US
I wrote the the following line of code:

Const c_Image1 = &quot;<img src=&quot; width=8 height=8 border=0>&quot;

NOTE: the &quot;;&quot; following the .gif string was placed by tek-tips message software, not by me.

When I attempted to compile the code, I received the following message:

&quot;Compiler error: Expected: end of statement&quot;

The blue highlight ends up on the word http. I imagine that the problem is the extra double quote. Is there a special character that I can use or a function that will allow to to turn this HTML tag into a constant?

I would rather not have to chop the tag up, if I can avoid it.
 
You used the &quot; character *inside* the constant. Try with the single quote (HTML can use both, single and double):

Const c_Image1 = &quot;<img src=' width=8 height=8 border=0>&quot;
 
The reason you are getting that error is because when the interpreter sees the quote before 'http' it thinks that is the end of your literal string.

Could you use the tic <'> instead of the quote <&quot;>

If not you would have to do something like this:
Code:
Const c_Image1 = &quot;<img src=&quot; & Chr(34) & &quot;[URL unfurl="true"]http://xyz.com/image1.gif&quot;[/URL] & Chr(34) & &quot; width=8 height=8 border=0>&quot;
 
If you really need a double quote (maybe for somewhere else since the single quote solution is easier here) you can replace the &quot; with Chr(34) as in:
Const c_Image1 = &quot;<img src=&quot; & Chr(34) &
Code:
&quot;[URL unfurl="true"]http://xyz.com/image1.gif&quot;[/URL]
& Chr(34) & &quot; width=8 height=8 border=0>&quot;
 
Wow, guys! What prompt feedback. Thanks so much.

The Chr(34) solution made perfect sense to me. However, when I tried it I received the following error:

&quot;Constant expression required.&quot;

The problem lies in attempting to initialize a constant with a the return value of a function call, a move that is illegal in Visual Basic 6.

One must initialize constants with literals, previously declared constants, or literals and constants joined by operators.

So I decided to try to declare the double quote as a constant.

I found a claim that if a double quote is a part of a string, it is necessary to put another double quote together with it.

I tried it and it worked.

These are the statements that I ended up with:

Const c_strDoubleQuote = &quot;&quot;&quot;&quot;

Const c_Image1 = &quot;<img src=&quot; & c_strDoubleQuote & &quot; & c_strDoubleQuote & &quot; width=8 height=8 border=0>&quot;

NOTE: Again, the &quot;;&quot; following the .gif string was placed by tek-tip's message software, not by me.

MiniMe2
 
Hi guys,

Just to let you know, I couldn't use the single quote or &quot;tic&quot; because I am actually parsing an HTML file that I could not change. Otherwise, the single quote solution would have been another solution to my problem.

Thanks again for putting those thinking caps on!
 
I had no problem adding the chr(34) function to my const declaration. I think the rule doesn't apply to instrinsic functions.

I can do this:
Code:
Const myQuotes = Chr(34)

and it works fine too... did that not work on your computer?
 
It didn't work under MS VB6, SP5. My attempt to compile Const c_strDoubleQuote = Chr(34) gave me the compiler error that I described in my previous post.

What's your configuration?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top