Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3675
  • Last Modified:

Chr(34) v/s double quotes

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
Sheils
Asked:
Sheils
  • 3
  • 3
  • 3
  • +2
2 Solutions
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
No pros or cons per se. No best.
Using Chr(34) is just plain easier to read. It's that simple.
mx
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
 
SheilsAuthor Commented:
Thanks Guys
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
Dale FyeCommented:
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
 
SheilsAuthor Commented:
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
 
Dale FyeCommented:
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
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
 
Dale FyeCommented:
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
 
Martin LissRetired ProgrammerCommented:
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
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
 
SheilsAuthor Commented:
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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now