Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Query text too long

The length of my query is well over 255 characters and I get an
automation error trying to run the query. Any suggestions?

Thank you for your time.

Steve
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Query text too long

I should have been more specific. I am querying an ODBC database and the
SQL query is too long.


John Bundy wrote:
There are a couple ways to do this if the first doesn't work but I use this
method, adapted from Microsoft.
Sub URL_Get_Query()

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;
http://www.moto-net.com/images/nouveautes2006/suzuki/bandit1200_static.jpg", _
Destination:=Range("a1"))

.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 196
Default Query text too long

Steve

Can you post your current code? I seem to remember that you can pass
longer SQL statements to a querytable object if you create the sql in
an array ie:

strSQL = Array("SELECT blah,blah, blah, blah")

Richard


steveh wrote:
I should have been more specific. I am querying an ODBC database and the
SQL query is too long.


John Bundy wrote:
There are a couple ways to do this if the first doesn't work but I use this
method, adapted from Microsoft.
Sub URL_Get_Query()

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;
http://www.moto-net.com/images/nouveautes2006/suzuki/bandit1200_static.jpg", _
Destination:=Range("a1"))

.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Query text too long

Richard, I saw that somewhere as well, but I'm not sure how to pass the
array. My code chokes if it is an array instead of a String.

Here's the pertinent code:

Dim conn As ADODB.Connection
Dim recset As ADODB.Recordset
Dim sqlArray() As String
Dim sqlText As String

Set recset = New ADODB.Recordset
With recset
.ActiveConnection = conn
.Open sqlText ' automation error; if sqlArray run-time error
Sheet1.Range("A2").CopyFromRecordset recset
.Close
End With

How would I pass an array?

Also, and maybe I should start another thread, but do you know how to
retrieve the field (column) names as part of the recordset?

Thanks,
Steve


RichardSchollar wrote:
Steve

Can you post your current code? I seem to remember that you can pass
longer SQL statements to a querytable object if you create the sql in
an array ie:

strSQL = Array("SELECT blah,blah, blah, blah")

Richard


steveh wrote:
I should have been more specific. I am querying an ODBC database and the
SQL query is too long.


John Bundy wrote:
There are a couple ways to do this if the first doesn't work but I use this
method, adapted from Microsoft.
Sub URL_Get_Query()

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;
http://www.moto-net.com/images/nouveautes2006/suzuki/bandit1200_static.jpg", _
Destination:=Range("a1"))

.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
End Sub


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 129
Default Query text too long

I fathom that the 255 limit depends on your database and your database
driver.

I use ODBC with DB2 and execute queries having over 3,000 characters.
All the time. No problems.

As to getting field names:

Dim rs As ADODB.Recordset
Set rs = command.Execute
Dim field As ADODB.field
Dim fieldIndex As Integer
fieldIndex = 1
For Each field In rs.Fields
sheet.Cells(1, fieldIndex).Value = field.Name
fieldIndex = fieldIndex + 1
Next field



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 196
Default Query text too long

Steve

It's not an area I have much experience with, but I think I was
incorrect with the array idea (it works with querytable objects, but
not seemingly with ADO recordset creation).

It is possible that you may get round the limit on the recordset SQL by
creating it from a Command Object. Give the following a try (this is
what I used for a sample Access Db on my harddrive, obviously you'll
have to amend to suit your specific circumstances):

Sub impo()
Dim com As ADODB.Command
Dim rst As ADODB.Recordset
Dim sqlText As String

sqlText = "SELECT * FROM Table1;"
Set com = New ADODB.Command
With com
.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=C:\Documents and
Settings\Richard Schollar\My Documents\" _
& "db1.mdb;User Id=admin;Password=;"
.CommandText = sqlText
.CommandType = adCmdText
End With
Set rst = com.Execute
With ActiveSheet
.Cells.ClearContents
For i = 1 To rst.Fields.Count
.Cells(1, i) = rst.Fields(i - 1).Name
Next
.Cells(2, 1).CopyFromRecordset rst
End With
End Sub

I hope this may be of some help!

Richard

How would I pass an array?

Also, and maybe I should start another thread, but do you know how to
retrieve the field (column) names as part of the recordset?

Thanks,
Steve


RichardSchollar wrote:
Steve

Can you post your current code? I seem to remember that you can pass
longer SQL statements to a querytable object if you create the sql in
an array ie:

strSQL = Array("SELECT blah,blah, blah, blah")

Richard


steveh wrote:
I should have been more specific. I am querying an ODBC database and the
SQL query is too long.


John Bundy wrote:
There are a couple ways to do this if the first doesn't work but I use this
method, adapted from Microsoft.
Sub URL_Get_Query()

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;
http://www.moto-net.com/images/nouveautes2006/suzuki/bandit1200_static.jpg", _
Destination:=Range("a1"))

.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
End Sub



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
How can I use a long URL in a web query? philnic Excel Discussion (Misc queries) 1 September 22nd 06 09:42 PM
Loading MS Query takes a long time TonyL Excel Worksheet Functions 0 August 14th 06 08:30 AM
How to enter a long address to new web query Pivotrend Excel Discussion (Misc queries) 5 September 2nd 05 11:36 AM
MyODBC & Excel VBA - Too long query ? Peter M.[_2_] Excel Programming 2 March 1st 05 02:30 PM
Excel Database Query String Too Long Karl Burrows[_2_] Excel Programming 4 January 27th 05 06:25 AM


All times are GMT +1. The time now is 11:51 PM.

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

About Us

"It's about Microsoft Excel"