Solved

SQL Query String

Posted on 2001-06-27
3
250 Views
Last Modified: 2010-05-02
Hi,

I have this problem.

when i made a SQL string query example:

set rst = new recordset
rst.open "select * from table where field = 'ABC'", ConnStr, adOpenStatic, adLockReadOnly

Note that the criteria "ABC" is retrieved from user input. The qns is, if the user input "AB'C", my query string will have error as the string will end till "AB" and the other statements will be invalid.

How am i going to resolve this?
Is there any other special characters which will caused this problem?

Pls help!
0
Comment
Question by:edonasan
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 6231066
You just need to double the ' character:

MyString = "AB'C"
rst.Open "Select * From Table Where Field = '" & Replace(MyString,"'","''") & "'",ConnStr,adOpenStatic,adLockReadOnly

0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6231123
Hi edonasan, TimCottee is correct.

Here is an alternative sample , use it as you use VB6 lower:

______________________________________________

Private Function Convert(ByVal Keyword As String) As String
   EmptyM = Keyword
   Do While InStr(EmptyM, "'") <> 0
       i = InStr(EmptyM, "'")
       EmptyM = Right$(Keyword, Len(Keyword) - i)
       Keyword = Left(Keyword, i) & "'" & EmptyM
   Loop
   Convert = Keyword
End Function

Use like this:
MyString = "AB'C"
rst.Open "Select * From Table Where Field = '" & Convert(MyString) & "'",ConnStr,adOpenStatic,adLockReadOnly
0
 

Author Comment

by:edonasan
ID: 6231138
yes!
thanks TimCotte!

Thanks ryancys too, i hope to allocate some points to u too.. thanks again :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

832 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