WHERE CLAUSE IN SELECT STATEMENT 
Author Message
 WHERE CLAUSE IN SELECT STATEMENT

I have code that sets up a WHERE clause as:

Criteria = "[DIN]= '" & DIN$ & "' AND [LASTNAME]='" &
LAST$ & "'"
strSQL = "SELECT * FROM [Inmates] WHERE " & Criteria
Set rst = dbs.OpenRecordset(strSQL)

The problem is that there are names like O'KELLY that come
up and SQL gives an error because the WHERE clause gives:

WHERE [LASTNAME]='O'KELLY'

How can I set it up so names with apostrophes do not
bomb?  Thanks.



Sat, 20 Aug 2005 22:49:55 GMT  
 WHERE CLAUSE IN SELECT STATEMENT
Quote:

> I have code that sets up a WHERE clause as:

> Criteria = "[DIN]= '" & DIN$ & "' AND [LASTNAME]='" &
> LAST$ & "'"
> strSQL = "SELECT * FROM [Inmates] WHERE " & Criteria
> Set rst = dbs.OpenRecordset(strSQL)

> The problem is that there are names like O'KELLY that come
> up and SQL gives an error because the WHERE clause gives:

> WHERE [LASTNAME]='O'KELLY'

> How can I set it up so names with apostrophes do not
> bomb?  Thanks.

there's several way, I use this one:

Criteria = "[DIN]= """ & DIN$ & """ AND [LASTNAME]=""" & LAST$ & """"
strSQL = "SELECT * FROM [Inmates] WHERE " & Criteria
Set rst = dbs.OpenRecordset(strSQL)

or you could use

Criteria = "[DIN]= " & chr(34) & DIN$ & chr(34) & " AND [LASTNAME]=" &
chr(34) & LAST$ & chr(34)
strSQL = "SELECT * FROM [Inmates] WHERE " & Criteria
Set rst = dbs.OpenRecordset(strSQL)

--

Tony Oakley (Microsoft Access MVP)
GSXR1300R Hayabusa
300hr 240GB TiVo



Sat, 20 Aug 2005 23:01:25 GMT  
 WHERE CLAUSE IN SELECT STATEMENT
Try:

Criteria = "[DIN] = " & Chr$(34) & DIN$ & Chr$(34) & _
    " AND [LASTNAME] = " & Chr$(34) & LAST$ & Chr$(34)
strSQL = "SELECT * FROM [Inmates] WHERE " & Criteria
Set rst = dbs.OpenRecordset(strSQL)

--
HTH
Van T. Dinh
MVP (Access)



Quote:
> I have code that sets up a WHERE clause as:

> Criteria = "[DIN]= '" & DIN$ & "' AND [LASTNAME]='" &
> LAST$ & "'"
> strSQL = "SELECT * FROM [Inmates] WHERE " & Criteria
> Set rst = dbs.OpenRecordset(strSQL)

> The problem is that there are names like O'KELLY that come
> up and SQL gives an error because the WHERE clause gives:

> WHERE [LASTNAME]='O'KELLY'

> How can I set it up so names with apostrophes do not
> bomb?  Thanks.



Sat, 20 Aug 2005 23:08:23 GMT  
 WHERE CLAUSE IN SELECT STATEMENT
I have implemented the chr(34) solution.  Thank you both.

Quote:
>-----Original Message-----
>I have code that sets up a WHERE clause as:

>Criteria = "[DIN]= '" & DIN$ & "' AND [LASTNAME]='" &
>LAST$ & "'"
>strSQL = "SELECT * FROM [Inmates] WHERE " & Criteria
>Set rst = dbs.OpenRecordset(strSQL)

>The problem is that there are names like O'KELLY that
come
>up and SQL gives an error because the WHERE clause gives:

>WHERE [LASTNAME]='O'KELLY'

>How can I set it up so names with apostrophes do not
>bomb?  Thanks.
>.



Sun, 21 Aug 2005 00:56:03 GMT  
 WHERE CLAUSE IN SELECT STATEMENT
You need to include something like this:

"WHERE tblContacts.ContShort like " & """" & Criteria
& "*" & """ ORDER BY....."

You need the wrap the criteria in a whole load of double-
quotes to avoid this problem.

Quote:
>-----Original Message-----
>I have code that sets up a WHERE clause as:

>Criteria = "[DIN]= '" & DIN$ & "' AND [LASTNAME]='" &
>LAST$ & "'"
>strSQL = "SELECT * FROM [Inmates] WHERE " & Criteria
>Set rst = dbs.OpenRecordset(strSQL)

>The problem is that there are names like O'KELLY that
come
>up and SQL gives an error because the WHERE clause gives:

>WHERE [LASTNAME]='O'KELLY'

>How can I set it up so names with apostrophes do not
>bomb?  Thanks.
>.



Sun, 21 Aug 2005 18:12:07 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Multiple WHERE Clauses In SELECT Statement

2. Multiple WHERE Clauses In SELECT Statement

3. Multiple WHERE Clauses In SELECT Statement

4. WHERE clause containing a variable in a SQL SELECT statement

5. Select statement as part of a where clause

6. How to select the current record only with SELECT statement

7. SQL statement - error in FROM clause, WHY ?

8. SQL: DELETE statement without WHERE clause?

9. Parameter in WHERE clause in VB SQL statement

10. SQL statement with GROUP BY clause not consistent

11. UPDATE statement with FROM clause in Access database?

12. SQL: DELETE statement without WHERE clause?

 

 
Powered by phpBB® Forum Software