LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Add QueryTable in code

In Excel VBA code, Runquery works, but Runquery2 does not.

Runquery2 gives a "Application-defined or object-defined error" on the Set
oQuery line

I would rather use the Runquery2 syntax as it is much shorter!

Any ideas why it is failing?


Public Sub runquery()
' required Tools | Reference to Microsoft Activex Data Objects Library
On Error GoTo e
Dim oQuery As QueryTable

Dim connstring As String
connstring = "Provider=sqloledb;Data Source=htivm;Initial
Catalog=pubs;Integrated Security=SSPI;"
Dim sql As String
sql = "SELECT * FROM AUTHORS"

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection
conn.ConnectionString = connstring
conn.Open
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open sql, conn, adOpenDynamic, adLockReadOnly
Set oQuery = Sheet1.QueryTables.Add(rs, Sheet1.Range("A1"))
oQuery.Refresh
rs.Close
conn.Close
Exit Sub
e:
Debug.Print Err.Description
End Sub

Public Sub runquery2()
On Error GoTo e
Dim oQuery As QueryTable

Dim connstring As String
connstring = "Provider=sqloledb;Data Source=htivm;Initial
Catalog=pubs;Integrated Security=SSPI;"
Dim sql As String
sql = "SELECT * FROM AUTHORS"

Set oQuery = Sheet1.QueryTables.Add(connstring, Sheet1.Range("A1"), sql)
oQuery.Refresh
Exit Sub
e:
Debug.Print Err.Description
End Sub




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
querytable connection TxRaistlin Excel Programming 2 February 9th 05 01:49 PM
QueryTable OLE DB with Parameter Rob van Gelder[_4_] Excel Programming 0 December 12th 04 03:15 AM
Querytable range name Ray[_12_] Excel Programming 0 September 13th 04 08:29 PM
How to use QueryTable with insertion sql ou Excel Programming 1 August 18th 04 12:20 PM
Querytable Ben.c Excel Programming 4 December 3rd 03 09:11 AM


All times are GMT +1. The time now is 08:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"