Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ADO Filter

Posted on 1999-07-27
18
Medium Priority
?
324 Views
Last Modified: 2013-12-25
I need to how to filter an ADO recordset for this value -
Field X ="test'11111'"
0
Comment
Question by:omrir
[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
  • 8
  • 4
  • 4
  • +2
18 Comments
 
LVL 12

Expert Comment

by:mark2150
ID: 1507118
select from {table} where X='test''1111'

You have to double up on 's that appear as literals.

M

0
 

Author Comment

by:omrir
ID: 1507119
I know, but if I have more then one quotes then i does not work.

PS

I'm working with a local Recordset with the Filter mehtod.

0
 
LVL 1

Expert Comment

by:tkuppinen
ID: 1507120
Make sure that record set is of type dynaset and then execute the follwing loop.

rs.MoveFirst
    Do Until rs("fieldname") = var
        rs.Move (1)
            If rs.EOF = True Then
                               
            Exit Do
            Exit Sub
            End If
    Loop
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:omrir
ID: 1507121
This is not the solution...

Thanks any way

Omri
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 1507122
Use this:
    rs.Filter "x = '" & Replace("test'1111", "'", "''") & "'"
0
 

Author Comment

by:omrir
ID: 1507123
emoreau

This is very true, if you have only one quotes in your string, and does not work if you have more :)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 1507124
Do you have a function that is called Replace?

Here is what I get when running it from the Immediate Window.
? replace("1'2'3","'","''")
1''2''3

Maybe you can try to use VBA.Replace to be sure to use this function.
0
 

Author Comment

by:omrir
ID: 1507125
The problem is not the replace but the Filter method in ADO , it returns an error for this kind of string
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 1507126
Could you paste here your Filter line so that we can see!
0
 
LVL 12

Expert Comment

by:mark2150
ID: 1507127
You have to double up on 's in your string. If you have two, you need FOUR, etc.

M

0
 

Expert Comment

by:10point
ID: 1507128
Use this:
 rs.Filter="X="+Chr(34)+"test"+Chr(39)+"11111"+Chr(39)+Chr(34)


0
 
LVL 12

Expert Comment

by:mark2150
ID: 1507129
Code a function like this:

Public Function UnQuote(txt as String) as String
'
if len(txt) < 1 then exit function
'
dim work as string
dim achar as string
dim ndx as integer
'
work = ""
for ndx = 1 to len(txt)
  achar = mid(txt, ndx, 1)
  if achar = "'" then achar = achar & "'"
  work = work & achar
next ndx
'
UnQuote = work
'
End Function

Use this when building your SQL:

sql = "select from {table} where X='" & _
       unquote("test''1111") & _
       "'"

M

0
 

Author Comment

by:omrir
ID: 1507130
10Points

The problem is not VB syntax but ADO, ADO returns an error if you try to filter a string that has more then one quotes.


0
 

Author Comment

by:omrir
ID: 1507131
This is an ADO question!!!

0
 
LVL 12

Expert Comment

by:mark2150
ID: 1507132
Code given works for me.

M

0
 

Author Comment

by:omrir
ID: 1507133
what version of ado are you using?
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 600 total points
ID: 1507134
Mark answer will work very well for regular query string to build your recordset.

But what you want from the beginning is to use the Filter property.

After some tests, I finally got it. You are right, Filter can't accept more than one quote in the searched expression. So I wrote this small function:
Private Function ReplaceForFilter(ByVal pstrField As String, pstrFilter As String) As String
Dim intPos As Integer
Dim strTemp As String

    intPos = InStr(1, pstrFilter, "'")
    If intPos = 0 Then
        ReplaceForFilter = pstrField & " = '" & pstrFilter & "'"
    Else
        strTemp = Left$(pstrFilter, intPos) & Mid$(pstrFilter, intPos)
        intPos = InStr(intPos + 2, strTemp, "'")
        If intPos = 0 Then
            ReplaceForFilter = pstrField & " = '" & strTemp & "'"
        Else
            ReplaceForFilter = pstrField & " LIKE '" & Left$(strTemp, intPos - 1) & "%'"
        End If
    End If
End Function

The first parameter is the field you want to filter for. The second parameter is the text you are looking for.

Use it this way (copied from the Immediate Window):
? frmdataenv.ReplaceForFilter("X","Test123")
X = 'Test123'
? frmdataenv.ReplaceForFilter("X","Test1'23")
X = 'Test1''23'
? frmdataenv.ReplaceForFilter("X","Test1'2'3")
X LIKE 'Test1''2%'

This is the nearest you can have. Since you can't have more than one quote in your searched text, use the Like operator instead of the = operator in those cases!


0
 

Author Comment

by:omrir
ID: 1507135
emoreau

First of all I'm glade that someone got the problem.
Your answer, I already know, I thought maybe this was not a
Microsoft bug. I guess I underestimated myself.

I will give the points for the effort :)

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

722 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