double quo

  • Thread starter Thread starter 00KobeBrian
  • Start date Start date
0

00KobeBrian

I tried to write the following code in a module:

If temp_word <> """ Then

End If

But given syntax error. I want to check if the temp_word is equal to a
double quotation mark. How can I achieve this? Thanks.
 
Each double quote delimited by other double quotes must be doubled:

If temp_word <> """" Then ...

You could also use Chr(34):

If temp_word <> Chr(34) Then ...
 
I'm curious as to how Chr(34), which is as I understand it a double quote,
could substitute for a pair of doubled quotes. I have seen this suggestion
before, but I don't understand it.
 
In code, """" (four double quotes in a row) results in a one double quote.
Chr$(34) also results in a one double quote.

From the Immediate window (Ctrl-G)

?"""" = Chr$(34)
True
 
I guess I'll just have to accept that it works, and forget about
understanding how. My single biggest difficulty with VBA is understanding
quotes. I have read all kinds of explanations, but somehow I just can't
seem to get it. If I enter ?Chr(34) or ?Chr$(34) in the immediate window I
get a double quote, yet ?Asc(""") returns an error, even though Asc("'")
returns 39. In all cases except the double quote (all cases of printing
characters, at least, as far as I can tell), the Asc function returns the
literal value betweeen the double quotes, except when the literal value is a
double quote, in which case all bets are off.
 
Quotes must always be balanced (i.e.: there must always be an even number of
them in a string). In your first example ?Asc("""), you've got an odd number
of quotes.

Now, you're essentially trying to create a string that contains a double
quote. Since you're trying to create a string, you have to have double
quotes at the beginning and end of the string. To represent a double quote
inside the string, you need to have two double quotes (since quotes always
have to be balanced).

Try ?Asc(""""). You should get 34.
 
You must understand the difference between sparsing and storing the internal
representation.

When the compiler parse a string that you have wrote, it must be able to
make a distinction between an embedded quote to must be stored along with
the other caracters (for example « shouldn"t » and a quote that is used to
indicate the end of the string. When you write something like "shouldn"t"
, the first and last " are not stored with the other caracters and are only
there to indicate to the compiler the beginning and the end of the string.
However, when the compiler see the embedded " followed by other alphanumeric
caracters, it doesn't know if this is the end of the string or an error that
you have made.

If you write two "", you are effectively telling the computer that this " is
not there to indicate the end of the string but is a caractere that must be
stored like every other caracters in the string. Howerver, even if you have
wrote two embedded double quotes, the compiler will store a single one (and
won't store the first and the last quotes that are only there to indicate
the beginning and the end of the string).

It's must clearer when you use another symbol like \ to separate any
embedded quote from the delimiters, like in C, C++ and C# :

... "shouldn\"t" ...

With SQL, you cant also use the single quote ' as the delimiter:

... 'shouldn"t' ...

Of course, you must then double any embedded single quote to have two single
quotes:

... 'shouldn''t' ...

With Chr(34), there is no need to tell the compiler when does it begin and
when does it end. The important thing to remember is that in all the above
cases, there is only one double quote (or single quote for the last example)
that got stored and that the other quotes that are used as delimiters are
not stored internally.
 
Thanks for the reply. ?Asc("""") was indeed 34. I just need to remember
that double quotes in a string need to be in pairs, and that they follow
different rules than any other character. If I want single quotes around a
title or something in a message box I can use:
"The title is " & "'" & [BookTitle] & "'"
but for double quotes I need to use a different format:
"The title is " & """" & [BookTitle] & """"
In the first case I could use Chr(39), and in the second Chr(34)
I could also use (in the second case):
"The title is """ [BookTitle] & """"
and in the first case:
"The title is '" [BookTitle] & "'"
I'm not disputing the truth of any of this, I'm just saying it is rather
bewildering. I need to remember to use the immediate window more often.
 
Thanks for the details, and especially for pointing out the difference
between parsing and storing (or representing, I suppose, if it is in a
message box or something). As I mentioned in a reply to Douglas, I just
need to remember that double quotes are a special case, and need to be
thought about differently than other characters.

I don't want even to think about using single quotes, unless I find that
they offer functionality that can't be found elsewhere. There are too many
way to do the same thing as it is.

Thanks again for taking the time to explain. This concept will eventually
lodge in my brain, and then something else can come along to puzzle me in
its stead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top