Solved

ADO help on querying error.

Posted on 2000-04-20
12
156 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
 
LVL 69

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help in WHSCRIPT 9 47
Paint/Redraw window while dragging 16 68
Saving history changes to sub form 4 36
Create Files based on Cell Values in a Range in Excel 12 17
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 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…

910 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

16 Experts available now in Live!

Get 1:1 Help Now