Solved

ADO help on querying error.

Posted on 2000-04-20
12
161 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

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

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…
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 …
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…
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…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

623 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