Jet Doesn't Support QueryDefs on a Non-Attached ODBC TableID: Q149055
|
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.
This behavior is by design because Jet does not have a place to store the QueryDef object on the ODBC source.
The preferred method for opening an external ODBC table is to attach it to
an .mdb file. For additional information, please see the following
article(s) in the Microsoft Knowledge Base:
Q150716 : DAO: How To Attach to and Create QueryDefs on ODBC Tables
Set qd = db.CreateQueryDef("")
qd.SQL = "Select * from authors"
Dim db As Database
Dim cn As String
Dim qd As QueryDef
Private Sub Form_Load()
'open db directly, without attaching
cn = "odbc;driver={SQL Server};server=myserver;" & _
"database=pubs;uid=myuid;pwd=mypwd"
Set db = OpenDatabase(Name:=pubs, Exclusive:=False, _
ReadOnly:=False, Connect:=cn)
End Sub
Private Sub Command1_Click()
'If we try to name it, we get the following error:
'Error 3251 "Operation is not supported for this type of object."
Set qd = db.CreateQueryDef("")
'If we try to name it like this we get error 3219 "Invalid operation."
'qd.Name = "abc"
qd.Connect = cn
qd.SQL = "Select * from titles"
End Sub
Private Sub Command2_Click()
Dim rs As Recordset
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
Jet Database Engine Programmer's Guide, Microsoft Press, page 323
Additional query words: kbVBp400 kbVBp600 kbdse kbDSupport kbVBp kbODBC
Keywords :
Version :
Platform : NT WINDOWS
Issue type :
Last Reviewed: June 22, 1999