Solved

ADO help on querying error.

Posted on 2000-04-20
12
159 Views
Last Modified: 2010-05-02

Hi just a slight problem,  the following code works great except when I encounter a word with an apostrophy

ex.  text16 = "horton's"
I get a syntax error in query expression
'SITE_NAME = 'horton's"

I've put " around "Chr(39) & Text16 & Chr(39)" then I get a Too few paramaters expected1 error at the adodc1.refresh line.

Any Idea's



sSQL5 = "SELECT *"
sSQL5 = sSQL5 & " From OasisSiteRes"
sSQL5 = sSQL5 & " Where SITE_NAME = "
sSQL5 = sSQL5 & Chr(39) & Text16 & Chr(39)

Adodc1.RecordSource = sSQL5
Adodc1.Refresh
0
Comment
Question by:Rev032400
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 2

Expert Comment

by:Sage020999
ID: 2736332
See if this function helps you.

Function FixQueryString(OldString As String, Optional IsLike As Boolean)
 
  Dim ctr As Integer
  Dim NewString As String

  For ctr = 1 To Len(OldString)
      If Mid$(OldString, ctr, 1) = "'" Then
         NewString = NewString & "''"
      ElseIf IsLike Then
         If Mid$(OldString, ctr, 1) = "#" Or Mid$(OldString, ctr, 1) = "?" Or Mid$(OldString, ctr, 1) = "!" Then
            NewString = NewString & "[" & Mid$(OldString, ctr, 1) & "]"
         Else
            NewString = NewString & Mid$(OldString, ctr, 1)
            End If
      Else
         NewString = NewString & Mid$(OldString, ctr, 1)
         End If
      Next ctr

  FixQueryString = NewString

End Function
0
 
LVL 3

Expert Comment

by:myqlG
ID: 2736356
ouch..it seems a little like overkill to remove an apostraphe.
for i=1 to len(mystring$)
if mid$(mystring$,i)<>"'" then
mynewstring$=mynewstring$ & mid$(mystring,i)
next i
NOW my new string will NOT have an apostraphe. I like this meathod because it strips out ALL the apostraphes from
O'Shea's and the like.

0
 

Author Comment

by:Rev032400
ID: 2736607
Thanks guys but is there another way to do it without stipping the '?

 
Rev
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 70

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 2736609
You can use the VB6 Replace function:

sSQL5 = "SELECT * From OasisSiteRes Where SITE_NAME = '" & replace(Text16, "'","''") & "'"

Adodc1.RecordSource = sSQL5
Adodc1.Refresh
0
 

Author Comment

by:Rev032400
ID: 2736612
Adjusted points from 20 to 30
0
 

Author Comment

by:Rev032400
ID: 2736618
Adjusted points from 30 to 50
0
 

Author Comment

by:Rev032400
ID: 2736789
It does work thanks
0
 
LVL 3

Expert Comment

by:myqlG
ID: 2737070
I believe that running a loop is faster then replace().
0
 

Author Comment

by:Rev032400
ID: 2742748
what kind of loop mygLG?
Replace didnt work all that great
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2743409
myqIG,

Benchmark it. You will be surprise that Replace is faster!
0
 
LVL 3

Expert Comment

by:myqlG
ID: 2743719
dim newstring as string
for i = 1 to len(myString$)
if mid$(mystring$,i,1)="'" then
mystring=mystring & "''"
else
mystring=mystring & mid$(mystring,i,1)
end if
And use mystring as the mysql5$

A have not benchmarked this.. I usually use the mid$,left$,right$ etc to parse out strings because split() returns varaints..and replace (well.. I just dont use it much ;)  I would be interested in some benchmark statistics
0
 

Author Comment

by:Rev032400
ID: 2813130
sage your function works great should give you points somehow
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

828 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