Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Chr(34) v/s double quotes

Posted on 2014-02-15
11
Medium Priority
?
3,439 Views
Last Modified: 2014-02-17
Hi Experts, This may be a very trivial question but I and a self taught developer who may be lacking in some basic theories.

When I write codes relating to user inputs I use chr(34) or """" to cater for quotes in their inputs.I personally perfect the chr(34) option because it is easier to read but I am wondering about the pros and cons of the two approaches. The codes below illustrates my question. Which on is the best?

strSomeVar = "Some Text" & """" & strUserInput & """" & "Some more Text"
strSomeVar = "Some Text" & chr(34) & strUserInput & Chr(34) & "Some more Text"
0
Comment
Question by:Sheils
  • 3
  • 3
  • 3
  • +2
11 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1000 total points
ID: 39862298
No pros or cons per se. No best.
Using Chr(34) is just plain easier to read. It's that simple.
mx
0
 
LVL 96

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 1000 total points
ID: 39862360
I would say this depends on your editor.  I use NotePad++ for vbX programming most of the time and it makes things clear to me.  I would also say your first option contains unnecessary code:

strSomeVar = "Some Text" & """" & strUserInput & """" & "Some more Text"

I would shorten that to:
strSomeVar = "Some Text""" & strUserInput & """Some more Text"

(In an editor that highlights appropriately, the above is very clear to me).
0
 
LVL 16

Author Closing Comment

by:Sheils
ID: 39862365
Thanks Guys
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:Dale Fye
ID: 39862558
I prefer to use a function I wrote a long time ago, which involves less typing and ensures that quotes embedded in the string are doubled up as well.
Public Function Wrap(WrapWhat as Variant, _
                     Optional WrapWith as string = """") as Variant

    'you may or may not want to include this line.  I use it so that if I need 
    'to write a NULL value to a SQL Insert statement, it will accept a NULL 
    'value and return "NULL"
    if IsNULL(WrapWhat) then = "NULL"

    'This line replaces characters embedded in WrapWhat with two of them
    'It then wraps the entire value with the WrapWhat characters
    Wrap = WrapWith _
              & Replace(WrapWhat, WrapWith, WrapWith & WrapWith) & WrapWith

End Function

Open in new window

In your example above, you would use this like:

strSomeVar = "Some Text " & Wrap(strUserInput) & " Some more Text"

I originally excluded the WrapWith and just hard coded in the chr(34) values, but then, as I started using SQL Server and realized that I need to wrap text in single quotes, and that this would work for date values as well, I added in the WrapWith argument, so now I can wrap values in quotes (the default), single quotes: Wrap(somevalue, "'"), or even pound signs: Wrap(DateValue, "#").
0
 
LVL 16

Author Comment

by:Sheils
ID: 39863250
Hi Dale,

Thanks for that very smart option. I would have given some point for that but unfortunately I have already closed the question

Cheers

SB9
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 39863423
I was just adding on.  I knew points had already been awarded, but wanted to impart that little trick that I use.  It makes reading the code so much easier, and takes less key strokes, and I'm all for saving work.
0
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 39864047
While Wrap as written is more versatile, it gives me the thought that you might just create a function Quote -

Function Quote(varString)
   Quote = """" & varString & """"
End Function

Open in new window


Then you could actually read what you've coded in more natural language:
strSomeVar = "Some Text" & Quote(strUserInput) & "Some more Text"
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 39864451
Lee,

That is actually what I started out with, in my first version.  The problem I found was that it didn't account for the possibility of embedded quotes in strUserInput.  

You know how hard it is to control user input in a text or memo field.  ;-)

That is why I added the Replace( ) function call, and then eventually modified it again to accept alternate characters as the WrapWith argument.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39865287
I'm too late for the party but I always do this.

Const QUOTE = """"

and then something like

Msgbox QUOTE & "This will appear as a string surrounded by quotes" & QUOTE
0
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 39865370
Actually, I do something similar (and would have eventually caught that) - in ASP pages, I use a function I call "SQLize" that replaces ' with ''  - SQLize(Request.Form("variable"))
0
 
LVL 16

Author Comment

by:Sheils
ID: 39865989
Hi Guys

Since that this post is still progressing I thought that I'd run a check on the various methods suggested. They all work to various extent. However, some of them add extra quotation marks within the string. The string I used to test is:

The experts said "There"s more then one way to skin this cat.  I'm now checking their methods. All in all I say "Thanks guys for your contributions"".

I bit of a nightmare enter but you get that from users.

I attach a copy of the test db just for participation sake.
dnquotetest.accdb
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question