Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #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




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default Add QueryTable in code

Jim

I think you have to have "OLEDB;" as the first field of connstring


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;"


connstring = "OLEDB;Provider=sqloledb;etc...."

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



--
Dick Kusleika
Excel MVP
Daily Dose of Excel
www.dicks-blog.com


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Add QueryTable in code

Thank you Dick!

That's the first time that www.ConnectionStrings.com let me down ;(

"Dick Kusleika" wrote in message
...
Jim

I think you have to have "OLEDB;" as the first field of connstring


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;"


connstring = "OLEDB;Provider=sqloledb;etc...."

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



--
Dick Kusleika
Excel MVP
Daily Dose of Excel
www.dicks-blog.com




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default Add QueryTable in code

Jim

You're welcome. I think it's a curiousity of MSQuery that it needs OLEDB or
ODBC at the start of its connection strings. I guess it needs to know if
you're using OLE or ODBC so you have to tell it. So don't be too hard on
ConnectionStrings.com. :)

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

Jim Hughes wrote:
Thank you Dick!

That's the first time that www.ConnectionStrings.com let me down ;(

"Dick Kusleika" wrote in message
...
Jim

I think you have to have "OLEDB;" as the first field of connstring


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;"


connstring = "OLEDB;Provider=sqloledb;etc...."

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



--
Dick Kusleika
Excel MVP
Daily Dose of Excel
www.dicks-blog.com



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Add QueryTable in code

Hard on them, no, I didn't mean to be :)

But I did forward a copy of this thread to them via email so that they can
include it if they so choose, least I could do after the help their site has
given me in the past! Thanks again!

"Dick Kusleika" wrote in message
...
Jim

You're welcome. I think it's a curiousity of MSQuery that it needs OLEDB
or ODBC at the start of its connection strings. I guess it needs to know
if you're using OLE or ODBC so you have to tell it. So don't be too hard
on ConnectionStrings.com. :)

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com





Reply
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 10:37 AM.

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"