Solved

sql

Posted on 2000-04-15
11
167 Views
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)  >>>>
0
Comment
Question by:sml41
[X]
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
11 Comments
 
LVL 54

Expert Comment

by:nico5038
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.

0
 
LVL 6

Expert Comment

by:Marine
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 .......
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 50 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)  
0
Industry Leaders: 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

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

Expert Comment

by:nico5038
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.

Success!
0
 
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.
0
 

Author Comment

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

thanks to all
0
 
LVL 54

Expert Comment

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

That's cooperation !
 
0
 
LVL 2

Expert Comment

by:johnny6
ID: 2720579
sml41:

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.

John
0
 

Author Comment

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

Expert Comment

by:nico5038
ID: 2720893
sml41,

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 !
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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