Posted on 2000-04-15
Medium Priority
Last Modified: 2010-05-02
When i try to read some name with ' in the name it gives an error 3075, that is using SQL in VB6 e.g" the name" O'sullivan", how can I overcome this problem. The statement I use is <<<<  Set RsCheckIfMemExist = dbTo.OpenRecordset("select surname, firstname from tblmembers where surname like '" & rsFrom!surname & "' and firstname like '" & rsFrom!firstname & "'", 2)  >>>>
Question by:sml41
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
LVL 54

Expert Comment

ID: 2718850
Two options:
1) Switch quotes
<<<<  Set RsCheckIfMemExist = dbTo.OpenRecordset('select surname, firstname from tblmembers where surname like "' & rsFrom!surname & '" and firstname like "' & rsFrom!firstname & '"', 2)  >>>>
2) Replace all single quotes in the name by a double single quote. (Making a qoute duplication function would be the easiest way.


Expert Comment

ID: 2718922
say you have to enter this search creteria into a textbox and then build your select upon it.This will work.

surName = Replace(text1,"'","''")
then you can build your select
Select surname .......
LVL 70

Accepted Solution

Éric Moreau earned 200 total points
ID: 2718957
Your complete query should read like this:
Set RsCheckIfMemExist = dbTo.OpenRecordset("select surname, firstname from tblmembers where surname like '" & replace(rsFrom!surname, "'", "''") & "' and firstname like '" & replace(rsFrom!firstname, "'", "''") & "'", 2)  

Since you use the LIKE operator, you need to place the wildcard (or use the = operator instead):
Set RsCheckIfMemExist = dbTo.OpenRecordset("select surname, firstname from tblmembers where surname like '" & replace(rsFrom!surname, "'", "''") & "*' and firstname like '" & replace(rsFrom!firstname, "'", "''") & "*'", 2)  
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 2719489
nico5038 , i have tried that, it failed in the 1st quote.
LVL 54

Expert Comment

ID: 2719498
correct sml41,

I screwed up with COBOL. That's supporting the "quote-switch", sorry.

So just the replace single quote two singles quotes should do it ! (like marine suggests with REPLACE.

LVL 70

Expert Comment

by:Éric Moreau
ID: 2720512
Your query must definetly be surrounded by double quotes! Look at the query I gave you.

Author Comment

ID: 2720520
I have used your line, the replace works ok.

thanks to all
LVL 54

Expert Comment

ID: 2720547
I gave the basic solution,
Marine added the REPLACE
And emoreau typed it for you !

That's cooperation !

Expert Comment

ID: 2720579

I think this is what you are looking for:

Public Function RemoveApostrophe(text As String) As String
    Dim Temp As String
    Dim ASymbol As String
    Dim i As Integer
    Temp = ""
    For i = 1 To Len(text)
    ASymbol = Mid(text, i, 1)
    If ASymbol = Chr(34) Or ASymbol = "'" Then ASymbol = ASymbol + ASymbol
    Temp = Temp + ASymbol
    Next i
    RemoveApostrophe = Temp
End Function

In your SQL code do the folowing:

strSQL = "Insert Into ... Values( '" & RemoveApostrophe( Field1 ) & "')"

Your data will end up in the database as "O'sullivan". No conversion is required when retreiving data.


Author Comment

ID: 2720631
nico, sorry i did not realise, what do u suggest, everyone is great!!
LVL 54

Expert Comment

ID: 2720893

I normally give the direction(s) of a solution as finding the working code by yourself is often the most rewarding and gives the best learning experience.

If you know now the way to handle "embedded" quotes (as I assume you do), that's for me rewarding enough!
(It went wrong because your string started with a single qoute and the textfield contained one that terminated your first qoute, thus corrupting the SQL-string)

I learned using the Replace, as I normally use the 'traditional' INSTR and MID commands, so I also gained from this question !

Success with your program !

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month15 days, left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question