Solved

Chr(34) v/s double quotes

Posted on 2014-02-15
11
1,611 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 - Access MVP) earned 250 total points
Comment Utility
No pros or cons per se. No best.
Using Chr(34) is just plain easier to read. It's that simple.
mx
0
 
LVL 95

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 250 total points
Comment Utility
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
Comment Utility
Thanks Guys
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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 95

Expert Comment

by:Lee W, MVP
Comment Utility
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 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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 95

Expert Comment

by:Lee W, MVP
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now