Microsoft Knowledge Base Email Alertz

KBAlertz.com: (150716) - This article describes how to attach and create Querydefs on external ODBC tables. The method for opening external ODBC tables is to attach the tables to an .mdb file. Jet does not support named QueryDefs on a non-attached ODBC database. A non-...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 2.0 Web Hosting with SQL 2005: Click Here!
Discount ASP.NET Hosting


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




How To DAO: Attach to and Create QueryDefs on ODBC Tables

Article ID:150716
Last Review:March 14, 2005
Revision:2.3
This article was previously published under Q150716
On This Page

SUMMARY

This article describes how to attach and create Querydefs on external ODBC tables. The method for opening external ODBC tables is to attach the tables to an .mdb file.

Jet does not support named QueryDefs on a non-attached ODBC database. A non- attached ODBC database is one that is opened directly with the OpenDatabase method of the WorkSpace object without the use of an .mdb file.

If it is not appropriate for the application to attach the ODBC tables, it is possible to create Querydefs with no name to accomplish the procedure.

For additional information, please see the following article in the Microsoft Knowledge Base:
149055 (http://kbalertz.com/Feedback.aspx?kbNumber=149055/EN-US/) : Jet Doesn't Support QueryDefs on a Non-Attached ODBC Table

Back to the top

MORE INFORMATION

The following is information from "Guide To Data Access Objects," Chapter 7, Data Access Choices, that explains this procedure:

In many cases, attaching a table to access external data is faster than opening a table directly, especially if the data is located in an ODBC database. In Visual Basic version 4.0, SQL Passthrough is used to query attached ODBC databases directly. Consider attaching external tables rather than opening them directly. Using external data in an ODBC database requires opening the external tables directly so performance is significantly slower when using the data.

Back to the top

Sample Program

The following example describes how to attach to and create a Querydef on an ODBC table using a "DSN-less" ODBC connection. With this procedure, it is not necessary to set up a DSN with the ODBC Admin utility.
1.Start a new project in Visual Basic. Form1 is created by default.
2.Add three Command buttons to Form1: Command1, Command2, Command3 by default.
3.Paste the following code in the General Declarations section of Form1:
      Dim db As Database
      Dim cn As String
      Private Sub Form_Load()
        cn = "odbc;driver={SQL Server};server=myserver;" & _
        "database=pubs;uid=myuid;pwd=mypwd"
        If Dir("mydb.mdb") <> "" Then
          ' database exists, so just open it.
          Set db = OpenDatabase(Name:="mydb", Exclusive:=False, _
            ReadOnly:=False, Connect:="")
        Else
          'database does not exist, create it and attach authors table.
          Set db = CreateDatabase(Name:="mydb", Connect:=dbLangGeneral, _
            Option:=dbVersion30)
          Dim td As TableDef
          Set td = db.CreateTableDef()
          td.Name = "Authors"
          td.SourceTableName = "Authors"
          td.Connect = cn
        End If
      End Sub
      Private Sub Command1_Click()
        Dim qd As QueryDef
        On Error Resume Next
        Set qd = db.QueryDefs("abc")  ' test for existence of querydef.
        If Error > 0 Then
          Set qd = db.CreateQueryDef(Name:="abc")
          qd.Connect = cn
          qd.SQL = "Select @@Version" 'native SQL Server
        End If
        Set qd = db.QueryDefs("xyz")  ' test for existence of querydef.
        If Error > 0 Then
          Set qd = db.CreateQueryDef(Name:="xyz")
          qd.Connect = cn
          qd.SQL = "Select * from titles" ' generic SQL.
        End If
        On Error GoTo 0
      End Sub
      Private Sub Command2_Click()
        Dim rs As Recordset
        Dim qd As QueryDef
        Set qd = db.QueryDefs("abc")
        Set rs = qd.OpenRecordset()
        Call displayResults(rs)
      End Sub
      Private Sub Command3_Click()
        Dim rs As Recordset
        Dim qd As QueryDef
        Set qd = db.QueryDefs("xyz")
        Set rs = qd.OpenRecordset()
        Call displayResults(rs)
      End Sub
      Sub displayResults(rs As Recordset)
        Dim f As Field, s As String, i As Integer
        For Each f In rs.Fields
          s = s & f.Name
        Next f
        Debug.Print s              ' print column headers.
        While Not rs.EOF And i < 5
          s = ""
          For Each f In rs.Fields
            s = s & f.Value
          Next f
          Debug.Print s            ' print first 5 rows.
          rs.MoveNext
          i = i + 1
        Wend
      End Sub

						
NOTE: You need to change the DRIVER, SERVER, DATABASE, UID, and PWD parameters in the OpenConnection method. Also you must modify the SQL statements contained in the Command1_Click event to match your SQL data source.
1.Press the F5 key to start the program.
2.Click the Command1 button to create the Querydefs. Click the Command2 and Command3 buttons to execute the Querydefs. Note that the first five rows of data appear in the Debug window.

Back to the top

REFERENCES

"Jet Database Engine Programmer's Guide", page 323, published by Microsoft Press.

Back to the top


APPLIES TO
Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
Microsoft Data Access Components 2.5

Back to the top

Keywords: 
kbhowto KB150716

Back to the top

       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please