Solved

sql

Posted on 2000-04-15
11
162 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
  • 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 69

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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 69

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now