Solved

about the query in ADO

Posted on 1998-06-01
6
173 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
[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
  • 4
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
Sekans earned 50 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

751 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