Is Putting Double Quotes in a String possible?

Argonak

Registered User.
Local time
Today, 14:13
Joined
Apr 15, 2005
Messages
28
In C i could use \" as I recall to put a double quote in a string. is this possible for VBA?
 
The Chr(34) function should help you with that.

Code:
MsgBox "testing " & Chr(34) & "123" & Chr(34)
 
Ahh, thankyou very much. While there was ussually a better way to do what I wanted, not knowing if that was possible was bugging me.
 
You can also just double up within the string.

i.e.

Code:
MsgBox "This is an ""example"" of what I mean!", vbInformation
 
I realize this is an outdated post...from 5 years ago!

I was searching through the boards to find an how to put a single double quote in a string (such as "8" Par").

Would doubling up the quote work?
ex: "8"" Par"

I've been searching around the 'net and can't seem to find a clear answer!

Thanks.
 
You would have to use 3 double quotes. So, I prefer to use Chr(34) as it is much simpler to understand and read (in my opinion).
 
Thanks for getting back to me so quickly.

So "8" & Chr(34) & " Par" should be the way it looks then?

Thanks again, I'll go try it out!
 
How do I enter 2 Double Quotes ("") in a String next to each other?

I want VBA to run a SQL statement to Update my number-field to "empty".

Here's my exsample:

RemoveNumber = "UPDATE MyTable SET MyTable.[MyCheckbox] = No, MyTable.NumberField = "" WHERE (((MyTable.[MyCheckbox])=Yes));"

CurrentDb.Execute RemoveNumber, dbFailOnError​
 
this might work for a null

RemoveNumber = "UPDATE MyTable SET MyTable.[MyCheckbox] = No, MyTable.NumberField = " & null & " WHERE (((MyTable.[MyCheckbox])=Yes));"


or this for a zero length string
RemoveNumber = "UPDATE MyTable SET MyTable.[MyCheckbox] = No, MyTable.NumberField = " & vbnullstring & " WHERE (((MyTable.[MyCheckbox])=Yes));"


again trying to avoid interminable concatentations of " characters.
 
Thank You Gemma!!!

Thanks to your reply I got the solution:

I just had to write Null without any quote or & sign.


RemoveNumber = "UPDATE MyTable SET MyTable.[MyCheckbox] = No, MyTable.NumberField = null WHERE (((MyTable.[MyCheckbox])=Yes));"
 
try this and let me know....

Sheet2.Cells(1, 1) = """"""

the above code will insert double quotes in a cell.....

if u want to add double quotes along with string use this code:
Sheet2.Cells(i, 6) = "Sathish" & """" & tc_id & """" & "Kumar"
here tc_id mentions the variable... if the value of tc_id is 2 then the result will be as follows:

Sathish"2"Kumar

Have a good day..:cool:
 
try this and let me know....

Sheet2.Cells(1, 1) = """"""

the above code will insert double quotes in a cell.....

if u want to add double quotes along with string use this code:
Sheet2.Cells(i, 6) = "Sathish" & """" & tc_id & """" & "Kumar"
here tc_id mentions the variable... if the value of tc_id is 2 then the result will be as follows:

Sathish"2"Kumar

Have a good day..:cool:
I still say that using Chr(34) is easier; easier to read and easier to type.
 
I still say that using Chr(34) is easier; easier to read and easier to type.

I agree! Use Chr(34) but I do get headaches when trying to code out a long SQL in VBA that involves DLookups that need quotes.
 
In some cases it is easier to use two quotes (which of these is easier to read?)
Code:
MsgBox "This is an ""example"" of what I mean!" 
MsgBox "This is an " & chr(34) & "example" & chr(34) & " of what I mean!"

If I was using it a lot, I would probably declare it as a Private Const at the module level for readability

Code:
Private Const dQuote = """"
...
MsgBox "This is an " & dQuote & "example" & dQuote & " of what I mean!"
 
(which of these is easier to read?)
I think the one with the Chr(34) is easier to read and know what is going to be happening. It isn't by nature normal for people to see "" surrounding something. And again I will state it that using CHR(34) will be very explicit and easy to reproduce for users but when you start getting into some very complex strings which have a lot of double quotes to begin with it can be less confusing to use:
Code:
strSQL = "Select Field1, Field2, Field3, 'Query1' As SourceQ " & _
             "FROM [" & strTableName & "] " & _
             "WHERE Field1 = " & Chr(34) & strData1 & Chr(34) & " And Field2=" & Chr(34) & strData2 & Chr(34) & " UNION " & _
"Select Field6, Field9, Field2, 'Query2' As SourceQ " & _
"FROM [" & strTableName2 & "] " & _
"WHERE Field6 = " & Chr(34) & strData1 & Chr(34) & " And Field9 =" & Chr(34) & strData2 & Chr(34)

and using triple quotes, especially for a newbie, can be daunting.
 
I agree. I would only use "" in a simple command like my example. In a complex command, I would use a Const or chr(34).

I just don't like having to remember that 34 is double quote, 39 is single quote (when I am using TSQL), Alt-156 when I need a £ but have a US keyboard!

You would have thought that M$ would have added it as a constant by now like vbCrLf etc.

Do you know whether the compiler optimizes "123" & chr(34) & "456" to a single string assignment?
 

Users who are viewing this thread

Back
Top Bottom