Is Putting Double Quotes in a String possible? (1 Viewer)

Argonak

Registered User.
Local time
Today, 12:42
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?
 

ghudson

Registered User.
Local time
Today, 15:42
Joined
Jun 8, 2002
Messages
6,195
The Chr(34) function should help you with that.

Code:
MsgBox "testing " & Chr(34) & "123" & Chr(34)
 

Argonak

Registered User.
Local time
Today, 12:42
Joined
Apr 15, 2005
Messages
28
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.
 

Mile-O

Back once again...
Local time
Today, 20:42
Joined
Dec 10, 2002
Messages
11,316
You can also just double up within the string.

i.e.

Code:
MsgBox "This is an ""example"" of what I mean!", vbInformation
 

Cowboy

Registered User.
Local time
Today, 15:42
Joined
Jul 23, 2010
Messages
53
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.
 

boblarson

Smeghead
Local time
Today, 12:42
Joined
Jan 12, 2001
Messages
32,059
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).
 

Cowboy

Registered User.
Local time
Today, 15:42
Joined
Jul 23, 2010
Messages
53
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!
 

MyTech

Access VBA
Local time
Today, 15:42
Joined
Jun 10, 2010
Messages
108
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​
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:42
Joined
Sep 12, 2006
Messages
15,656
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.
 

MyTech

Access VBA
Local time
Today, 15:42
Joined
Jun 10, 2010
Messages
108
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));"
 

gsk

New member
Local time
Tomorrow, 01:12
Joined
Jul 18, 2012
Messages
1
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:
 

boblarson

Smeghead
Local time
Today, 12:42
Joined
Jan 12, 2001
Messages
32,059
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.
 

ghudson

Registered User.
Local time
Today, 15:42
Joined
Jun 8, 2002
Messages
6,195
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.
 

DrallocD

Registered User.
Local time
Today, 15:42
Joined
Jul 16, 2012
Messages
112
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!"
 

boblarson

Smeghead
Local time
Today, 12:42
Joined
Jan 12, 2001
Messages
32,059
(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.
 

DrallocD

Registered User.
Local time
Today, 15:42
Joined
Jul 16, 2012
Messages
112
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

Top Bottom