Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

about the query in ADO

Posted on 1998-06-01
6
Medium Priority
?
181 Views
Last Modified: 2010-05-03
After creating a query, something like

Set OldDb = OpenDatabase("c;\file.mdb")
Set NewQry = OldDb.CreateQueryDef("Qry1")
NewQry.SQL = "select * from table1 where field1 like '*1*'"

How to access the selected records? Say debug.print first record.

Moreover, why not use

Set OldDb = OpenDatabase("c;\file.mdb")
Set NewSet = OldDb.OpenRecordset("select * from table1 where field1 like '*1*'")

which looks much easy and one may directely assess the selected records by the recordset.

0
Comment
Question by:victorlong
  • 4
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
Sekans earned 200 total points
ID: 1462254
victorlong,
To access the selected records, you would simply need to create a recordset, like this:

Set NewSet=OldDb.OpenRecordset("Qry1")

Then just move through the recordset:

NewSet.MoveFirst
Do Until NewSet.EOF = True
     'Do something
NewSet.MoveNext

Second question,
    I would indeed use the openrecordset with a SQL statement.

Regards,
Sekans
0
 
LVL 1

Expert Comment

by:Sekans
ID: 1462255
Sorry, I left the Loop off of the Do...Loop, it should look like this.

Do Until NewSet.EOF = True
     'Do something
      NewSet.MoveNext
Loop

Sekans
0
 

Author Comment

by:victorlong
ID: 1462256
Thank you Sekans. I want to make sure that

1. We can do:

   Set NewQry = OldDb.CreateQueryDef("Qry1")
   NewQry.SQL = "select * from table1 where field1 like '*1*'"
   Set NewSet = OldDb.OpenRecordset("Qry1")

2. The above is the same as:

   Qry2 = "select * from table1 where field1 like '*1*'"
   Set NewSet = OldDb.OpenRecordset(Qry2)

3. You like the second method.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:Sekans
ID: 1462257
1. Yes, this will create a new query object in the specified database, then return the records based on that query.

2. The two methods are almost the same.  The difference is, the first method actually creates a new query object in your database, while the second, only returns the desired records.  

3. Other experts may disagree, but I definately prefer the second method.  I don't see why you should junk up your database with queries, when you don't need to.  As you can see, the difference in code is minimal.

Hope this helps,
Sekans
0
 

Author Comment

by:victorlong
ID: 1462258
Hi Sekans

Thank you for your help.
0
 
LVL 1

Expert Comment

by:Sekans
ID: 1462259
Your very welcome,
Sekans
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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.
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…
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…

824 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