views:

142

answers:

3

I want to create a query like this :

SQL = SQL + "Libelle = \"" + Me.Libelle + "\" AND "

because if I have a problem with my data if I write like this :

SQL = SQL + "Libelle = '" + Me.Libelle + "' AND "

but there is a problem with the \" I'm used to use it in Java (I'm not a VBA developper :s) How to resolve it ? thnx

A: 

The solution is a concatenation with the Chr(34) :)

SQL = SQL + "Libelle = " + Chr(34) + Me.Libelle + Chr(34) + " AND "
taichimaro
You should not use + to concatenate strings.
Remou
David-W-Fenton
+2  A: 

In VBA use & to concatenate strings, + will lead to unexpected results. Jet SQL uses two single quotes to escape a single quote. It is normal to use Replace to get this.

 SQL = SQL & "Libelle = '" & Replace(Me.Libelle,"'","''") & "' AND "
Remou
David-W-Fenton
A: 

In VB you get a literal quote with two quotes. "abc""def" yields abc"def.

What you want is:

SQL = SQL & "Libelle = """ & Me.Libelle & """ AND "
Marc Thibault
Do not use plus (+) to concatenate strings in VBA unless you understand what happens if one of the strings is null.
Remou
Absolutely right. Fixed it.
Marc Thibault
The ANSI standard for SQL specifies single quotes (').
Remou
Well, use the + concatenation operator if you want to propagate Nulls. I use it frequently, but you have to be careful (as my extensive comment above explains).
David-W-Fenton
Why should I as an Access developer care particularly if the ANSI standard for SQL uses single quotes? Access doesn't, so isn't that more relevant than an outside standard that is not implemented in Access?
David-W-Fenton