8

In my current VBA code, I have a query in which I am using Chr(34) to put quotation marks between some of my variables.

I was wondering what alternatives exist to this. This is a simple question, i know, but I haven't had any success with repeating quotation marks like this

" & variable string here & "

My code is messy for one and not understandable for people who are not familiar with VBA:

comboService = Chr(34) & Me.Combo8.Value & Chr(34)

Also, this hasn't worked:

comboService = """" & Me.Combo8.Value & """"

Can you perhaps tell me why?

Thanks in advance.

6
  • 4
    This: """" & Me.Combo8.Value & """" works in VBA. What error or problem are you getting? Nov 28, 2012 at 0:54
  • Can you pls add a more detailed example. It isn't clear how many quotation marks you want to add.
    – brettdj
    Nov 28, 2012 at 0:54
  • Sorry, i just corrected my mistake. I am only looking to add 1 Quotation mark. I'm using the value of the variable comboService in a Query. Nov 28, 2012 at 0:56
  • 1
    As RBarryYound posted your prior code was fine along the lines of MsgBox """" & strIn & """" is fine. If quote marks prove difficult then the quick workaround is to use the macro recorder to record the string you need in a formula
    – brettdj
    Nov 28, 2012 at 1:00
  • Keep in mind, if you are using this in a Query, you can use single quotes instead. SQL reads single and double quotes as the same thing. Example: comboService = "'" & Combo8 & "'" (I dropped the Me. and the .Value as the Me. is assumed and the .value is the default return of a combo box)
    – Mike
    Sep 5, 2013 at 15:30

3 Answers 3

12

This:

comboService = """ & Me.Combo8.Value & """

is what you posted, but you need to add an extra quotation mark in order to add a literal quotation mark:

comboService = """" & Me.Combo8.Value & """"

Double-quotes within a string are what you are looking for.

aVar = "This: "" is a literal quotation mark"
7

I took a page from MS (the old vbCRLF) a while back, and just define any "tricky" characters I'll need as a string at the top of my code ...

Dim vbDblQuote As String
vbDblQuote = Chr(34)

Now you can just use that pseudo-constant as you build strings ...

strMyString = "Just another string " & vbDblQuote & "with quotes" & vbDblQuote & "!"

This makes code more readable, and also helps avoid "miscounted quote errors"

0
0

I have found the same behavior if I output to a file using the Write command. Any double quotes get repeated.

However, if you construct the string and then output to file using the Print command, this does not happen and all works as expected.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.