Solved

sql

Posted on 2000-04-15
11
166 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

730 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