Link to home
Start Free TrialLog in
Avatar of bennyv
bennyv

asked on

Changing the querry of ADO recordset

Hi,
I have a small VB application with an access database as backend.  I amd using ADO connection.  It is working fine.  Now my requirement is very simple.  I have a text box in which if I type some thing, the records beginning with that should be displayed.  Initially I was using DAO and I was able to do this.  Now I'm trying to use ADO.  I don't know how to do this using ADO.  Basically I want to change the contents of the recordset at runtime depending upon a different querry.  Please help me in this with the command/syntax.

Thanks in advance,
Benny
Avatar of Marine
Marine

"Select * from TableName where FieldName like '" & Text1.Text & "*'"
One way to do this is to create a new Recordset object when you need to re-query. (The old one will be destroyed by VB).

Another way is to use a parameterized query. Use the ADO.Command object. (This may not work correctly with older versions of access, so be carefull.)

Good Luck

ASKER CERTIFIED SOLUTION
Avatar of anand_m
anand_m

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Benny:
Post your old code.. a picture is worth a thousand words.. <smile>.

altena:
you write: "One way to do this.. Another way.." How is this a definitive answer? Shame on you for locking this question down with an answer rather than allowing other comment.

The gyst of what the questioner is saying is "I don't know HOW to do this using ADO".. To that means.. your answer hardly suffices. By the way.. your parameterized ADO Command Object solution implies a Stored database procedure.. and I saw no mention of that anywhere in the question. To be honest, the questioner has not provided enough information.

Please remove your answer, so that Benny can benefit from other's comments.. and perhaps, benefit further by additional "comments" of your own making.. <smile>.
Avatar of bennyv

ASKER

I would like to accept the answer of anand m rather than altena's because altena didn't give me the code to use.  Anand's method was easy to use and I got the syntax from MSDN.  I tried Altena's answer also (by closing the current recordset and reopening it with the new query); but it did not work.  It is not displaying anything.  The absolute position is coming as -1.  But anand's method is working perfectly.  This is the code I used. :

Private Sub txtSearch_Change()
sqls = "NAMES LIKE " & "'" & txtSearch.Text & "*'"
adoPrimaryRS.MoveFirst
If txtSearch.Text <> "" Then
adoPrimaryRS.Find sqls
End If
End Sub

I would like to accept anand's answer.

Thanks altena.
Thanks to everybody for answering my question.

Benny
Avatar of bennyv

ASKER

One reason why I felt anand's answer is better is that, I feel it will be more efficient.  Because you are not reopening the record set.

Thanks anand