Help with a select statement (1 Viewer)

keving

Registered User.
Local time
Today, 00:39
Joined
Feb 28, 2003
Messages
23
Hello. I am having trouble with a select statement in my code. The select has actually been working for months and then all of a sudden I got a surprise. One of the pieces of data has a single quote imbedded in it and it causes the lookup to fail. Here is the problem.

SELECT * FROM [Training Request Table] Where [Student Last Name]= 'Morel-à-l'Huissier' and [Student First Name]= 'Patrick' ;


As you can see the student last name has a single quote in it which messes up the select. Any ideas?????

Kevin ....
 
Last edited:

ByteMyzer

AWF VIP
Local time
Yesterday, 16:39
Joined
May 3, 2004
Messages
1,409
Change the SQL statement to:

SELECT * FROM [Training Request Table] Where [Student Last Name]= "Morel-à-l'Huissier" and [Student First Name]= "Patrick" ;


To implement this statement in Visual Basic (ex. enumerating a recordset):
Code:
'rs - RecordSet Object
Set rs = CurrentDb.OpenRecordset("SELECT * FROM [Training Request Table] " _
    & "Where [Student Last Name]= ""Morel-à-l'Huissier"" " _
    & "and [Student First Name]= ""Patrick"";")
 

keving

Registered User.
Local time
Today, 00:39
Joined
Feb 28, 2003
Messages
23
ByteMyzer, your suggestion appears to stop my code from failing but it also appears that I am doing my lookup using a variable name rather than the student name? The following shows how I built the SQL string in VBA. The two commented lines are my original code replaced by your suggestion.

stSql = "SELECT * FROM [Training Request Table]"
stSql = stSql & " Where [Student Last Name]= "" & [InStudentLastName] & "" and"
stSql = stSql & " [Student First Name]= "" & [InStudentFirstName] & "" ;"
' stSql = stSql & " Where [Student Last Name]= '" & [InStudentLastName] & "' and"
' stSql = stSql & " [Student First Name]= '" & [InStudentFirstName] & "' ;"

rstTrainingRequestTable.Open stSql, cnn, adOpenDynamic, adLockOptimistic

I appreciate the help.

Kevin ....
 

ByteMyzer

AWF VIP
Local time
Yesterday, 16:39
Joined
May 3, 2004
Messages
1,409
Try this:
Code:
stSql = "SELECT * FROM [Training Request Table]"
stSql = stSql & " Where [Student Last Name]= """ & [InStudentLastName] & """ and"
stSql = stSql & " [Student First Name]= """ & [InStudentFirstName] & """ ;"
 

keving

Registered User.
Local time
Today, 00:39
Joined
Feb 28, 2003
Messages
23
That solves the problem. Thanks so much for your help.

Keving
 

Mile-O

Back once again...
Local time
Today, 00:39
Joined
Dec 10, 2002
Messages
11,316
For each ' substitute ""
 

Users who are viewing this thread

Top Bottom