Solved

SQL Query String

Posted on 2001-06-27
3
259 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

808 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