Marouane Gazanayi
Marouane Gazanayi

Reputation: 5193

VBA problem with String concatenation

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

Upvotes: 1

Views: 4449

Answers (3)

Marc Thibault
Marc Thibault

Reputation: 1728

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 "

Upvotes: 0

Fionnuala
Fionnuala

Reputation: 91376

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 "

Upvotes: 2

Marouane Gazanayi
Marouane Gazanayi

Reputation: 5193

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

SQL = SQL + "Libelle = " + Chr(34) + Me.Libelle + Chr(34) + " AND "

Upvotes: 0

Related Questions