Article ID: 149055
Article Last Modified on 3/14/2005
150716 : 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
Keywords: kbprb KB149055