Solved

ADO help on querying error.

Posted on 2000-04-20
12
155 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys but is there another way to do it without stipping the '?

 
Rev
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
Comment Utility
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
Comment Utility
Adjusted points from 20 to 30
0
 

Author Comment

by:Rev032400
Comment Utility
Adjusted points from 30 to 50
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Rev032400
Comment Utility
It does work thanks
0
 
LVL 3

Expert Comment

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

Author Comment

by:Rev032400
Comment Utility
what kind of loop mygLG?
Replace didnt work all that great
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
myqIG,

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

Expert Comment

by:myqlG
Comment Utility
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
Comment Utility
sage your function works great should give you points somehow
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

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…
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 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…

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now