Solved

ADO help on querying error.

Posted on 2000-04-20
12
158 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

831 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