#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 138
Default ODBC Query

I am pulling data into Excel using ODBC. The returned data consists of
4 strings/numbers but, instead of being written to my defined
"results" range, the "system" is inserting 4 extra columns to the left
of my defined range for each and every iteration. Is there a way that
I can prevent this from happening such that the returned data
automatically gets written into the defined range?

Regards,
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default ODBC Query

On Jan 21, 7:15*am, Alan wrote:
I am pulling data into Excel using ODBC. The returned data consists of
4 strings/numbers but, instead of being written to my defined
"results" range, the "system" is inserting 4 extra columns to the left
of my defined range for each and every iteration. Is there a way that
I can prevent this from happening such that the returned data
automatically gets written into the defined range?

Regards,


hi
you didn't really give enough info to tell what's really wrong.
posting your code would be very helpful.
but from what you did post, it sounds like you are trying to recreate
the query each time you run it. that is really not necessary. once the
query has been created, excel assigns a name to it, a named range and
gives it properties. after that all you need to do is refresh the
query table. and 1 line of code would do it.
assuming query table is on sheet1 at cell A1.
Sub refre****()
Sheets("sheet1").Range("A2").QueryTable.Refresh BackgroundQuery:=False
MsgBox "done"
End Sub

you could also right click the query table and from the popup, click
refresh.

so if i have it wrong, post the code you are using.

regards
FSt1


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 138
Default ODBC Query

On Jan 22, 2:34*am, FSt1 wrote:
On Jan 21, 7:15*am, Alan wrote:

I am pulling data into Excel using ODBC. The returned data consists of
4 strings/numbers but, instead of being written to my defined
"results" range, the "system" is inserting 4 extra columns to the left
of my defined range for each and every iteration. Is there a way that
I can prevent this from happening such that the returned data
automatically gets written into the defined range?


Regards,


hi
you didn't really give enough info to tell what's really wrong.
posting your code would be very helpful.
but from what you did post, it sounds like you are trying to recreate
the query each time you run it. that is really not necessary. once the
query has been created, excel assigns a name to it, a named range and
gives it properties. after that all you need to do is refresh the
query table. and 1 line of code would do it.
assuming query table is on sheet1 at cell A1.
Sub refre****()
Sheets("sheet1").Range("A2").QueryTable.Refresh BackgroundQuery:=False
MsgBox "done"
End Sub

you could also right click the query table and from the popup, click
refresh.

so if i have it wrong, post the code you are using.

regards
FSt1


Hi,

Here is the guts of my query ... it is executed approximately 20 times
for each run.

Sub RunQuery(ByVal i As Integer, ByVal qDate As String)

Dim CmdText As String

qDate = Format(qDate, "dd-mmm-yy")
Application.DisplayAlerts = False

' Set the query command text
CmdText = _
"Get_Value('" & TagRefs.Cells(i, 1).Value & "', '" & qDate &
"')"

' Run the query
With QTable
.CommandText = CmdText
.Refresh BackgroundQuery:=False
End With

Application.DisplayAlerts = True

End Sub

Where the QTable is defined by:

Set QTable = Config.QueryTables.Add(Connection:="ODBC;Driver={A T
SQLplus};ADS=" & Server, _
Destination:=Config.Range("B1"))

Config is a worksheet and Get_Value is a named procedu the server
name is defined elsewhere. Originally the destination for the QTable
was a defined range but after much messing around I found that Excel
doesn't seem to like named ranges on the same row as a query table -
if such a range exists the returned query is returned into a new,
Excel generated, named range in inserted columns to the left of the
previous range. If no named range exists, Excel quite happily
overwrites the names that it generates for itself.

I now remove all named ranges from the query table rows before a new
query is run - this isn't what I had originally intended and it is a
bit of a pain but it does seem to work. Any advice would still be very
welcome.

Alan
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 236
Default ODBC Query

I'm not sure i follow why you need the named ranges in the query
result range - perhaps pulling it by some formulas elsewhere..
Anyway - this is what makes the columns shift:
Config.QueryTables.Add
as every time it adds a new query table.
As FST1 noted - you don't have to insert it anew every time - you can
use the existing one and just modify its .commandtext property and
refresh.
You can refer to the query table as FST1 shows you or
Config.Querytables(1).commandtext
or
Config.Querytables("QueryNameGoesHere").commandtex t



On Jan 24, 1:09*pm, Alan wrote:
On Jan 22, 2:34*am, FSt1 wrote:





On Jan 21, 7:15*am, Alan wrote:


I am pulling data into Excel using ODBC. The returned data consists of
4 strings/numbers but, instead of being written to my defined
"results" range, the "system" is inserting 4 extra columns to the left
of my defined range for each and every iteration. Is there a way that
I can prevent this from happening such that the returned data
automatically gets written into the defined range?


Regards,


hi
you didn't really give enough info to tell what's really wrong.
posting your code would be very helpful.
but from what you did post, it sounds like you are trying to recreate
the query each time you run it. that is really not necessary. once the
query has been created, excel assigns a name to it, a named range and
gives it properties. after that all you need to do is refresh the
query table. and 1 line of code would do it.
assuming query table is on sheet1 at cell A1.
Sub refre****()
Sheets("sheet1").Range("A2").QueryTable.Refresh BackgroundQuery:=False
MsgBox "done"
End Sub


you could also right click the query table and from the popup, click
refresh.


so if i have it wrong, post the code you are using.


regards
FSt1


Hi,

Here is the guts of my query ... it is executed approximately 20 times
for each run.

Sub RunQuery(ByVal i As Integer, ByVal qDate As String)

* * Dim CmdText As String

* * qDate = Format(qDate, "dd-mmm-yy")
* * Application.DisplayAlerts = False

' Set the query command text
* * CmdText = _
* * * * "Get_Value('" & TagRefs.Cells(i, 1).Value & "', '" & qDate &
"')"

' Run the query
* * With QTable
* * * .CommandText = CmdText
* * * .Refresh BackgroundQuery:=False
* * End With

* * Application.DisplayAlerts = True

End Sub

Where the QTable is defined by:

Set QTable = Config.QueryTables.Add(Connection:="ODBC;Driver={A T
SQLplus};ADS=" & Server, _
* * * * Destination:=Config.Range("B1"))

Config is a worksheet and Get_Value is a named procedu the server
name is defined elsewhere. Originally the destination for the QTable
was a defined range but after much messing around I found that Excel
doesn't seem to like named ranges on the same row as a query table -
if such a range exists the returned query is returned into a new,
Excel generated, named range in inserted columns to the left of the
previous range. If no named range exists, Excel quite happily
overwrites the names that it generates for itself.

I now remove all named ranges from the query table rows before a new
query is run - this isn't what I had originally intended and it is a
bit of a pain but it does seem to work. Any advice would still be very
welcome.

Alan- Hide quoted text -

- Show quoted text -


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 138
Default ODBC Query

On Jan 24, 4:30*pm, AB wrote:
I'm not sure i follow why you need the named ranges in the query
result range - perhaps pulling it by some formulas elsewhere..
Anyway - this is what makes the columns shift:
Config.QueryTables.Add
as every time it adds a new query table.
As FST1 noted - you don't have to insert it anew every time - you can
use the existing one and just modify its .commandtext property and
refresh.
You can refer to the query table as FST1 shows you or
Config.Querytables(1).commandtext
or
Config.Querytables("QueryNameGoesHere").commandtex t

On Jan 24, 1:09*pm, Alan wrote:



On Jan 22, 2:34*am, FSt1 wrote:


On Jan 21, 7:15*am, Alan wrote:


I am pulling data into Excel using ODBC. The returned data consists of
4 strings/numbers but, instead of being written to my defined
"results" range, the "system" is inserting 4 extra columns to the left
of my defined range for each and every iteration. Is there a way that
I can prevent this from happening such that the returned data
automatically gets written into the defined range?


Regards,


hi
you didn't really give enough info to tell what's really wrong.
posting your code would be very helpful.
but from what you did post, it sounds like you are trying to recreate
the query each time you run it. that is really not necessary. once the
query has been created, excel assigns a name to it, a named range and
gives it properties. after that all you need to do is refresh the
query table. and 1 line of code would do it.
assuming query table is on sheet1 at cell A1.
Sub refre****()
Sheets("sheet1").Range("A2").QueryTable.Refresh BackgroundQuery:=False
MsgBox "done"
End Sub


you could also right click the query table and from the popup, click
refresh.


so if i have it wrong, post the code you are using.


regards
FSt1


Hi,


Here is the guts of my query ... it is executed approximately 20 times
for each run.


Sub RunQuery(ByVal i As Integer, ByVal qDate As String)


* * Dim CmdText As String


* * qDate = Format(qDate, "dd-mmm-yy")
* * Application.DisplayAlerts = False


' Set the query command text
* * CmdText = _
* * * * "Get_Value('" & TagRefs.Cells(i, 1).Value & "', '" & qDate &
"')"


' Run the query
* * With QTable
* * * .CommandText = CmdText
* * * .Refresh BackgroundQuery:=False
* * End With


* * Application.DisplayAlerts = True


End Sub


Where the QTable is defined by:


Set QTable = Config.QueryTables.Add(Connection:="ODBC;Driver={A T
SQLplus};ADS=" & Server, _
* * * * Destination:=Config.Range("B1"))


Config is a worksheet and Get_Value is a named procedu the server
name is defined elsewhere. Originally the destination for the QTable
was a defined range but after much messing around I found that Excel
doesn't seem to like named ranges on the same row as a query table -
if such a range exists the returned query is returned into a new,
Excel generated, named range in inserted columns to the left of the
previous range. If no named range exists, Excel quite happily
overwrites the names that it generates for itself.


I now remove all named ranges from the query table rows before a new
query is run - this isn't what I had originally intended and it is a
bit of a pain but it does seem to work. Any advice would still be very
welcome.


Alan- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -


Thanks AB but the Config.QueryTables.Add line is only executed once
when the whole process is initialised so that is not a cause of the
issue ... or are you saying that once the query table exists in the
workbook it is not necessary to ADD again even if the workbook is
opened up and restarted from scratch? I use QTables so infrequently
that I can't honestly remember what I did last time.

I was trying to add the query table to a named range because I need to
use the returned data elsewhere and I don't like hard-coding cell
addresses into my code just in case the worksheet is later changed.
Perhaps I should create the QTable and then add the range name?

You have certainly given me a few pointers to experiment with.

Alan


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 138
Default ODBC Query

On Jan 24, 4:30*pm, AB wrote:
I'm not sure i follow why you need the named ranges in the query
result range - perhaps pulling it by some formulas elsewhere..
Anyway - this is what makes the columns shift:
Config.QueryTables.Add
as every time it adds a new query table.
As FST1 noted - you don't have to insert it anew every time - you can
use the existing one and just modify its .commandtext property and
refresh.
You can refer to the query table as FST1 shows you or
Config.Querytables(1).commandtext
or
Config.Querytables("QueryNameGoesHere").commandtex t

On Jan 24, 1:09*pm, Alan wrote:



On Jan 22, 2:34*am, FSt1 wrote:


On Jan 21, 7:15*am, Alan wrote:


I am pulling data into Excel using ODBC. The returned data consists of
4 strings/numbers but, instead of being written to my defined
"results" range, the "system" is inserting 4 extra columns to the left
of my defined range for each and every iteration. Is there a way that
I can prevent this from happening such that the returned data
automatically gets written into the defined range?


Regards,


hi
you didn't really give enough info to tell what's really wrong.
posting your code would be very helpful.
but from what you did post, it sounds like you are trying to recreate
the query each time you run it. that is really not necessary. once the
query has been created, excel assigns a name to it, a named range and
gives it properties. after that all you need to do is refresh the
query table. and 1 line of code would do it.
assuming query table is on sheet1 at cell A1.
Sub refre****()
Sheets("sheet1").Range("A2").QueryTable.Refresh BackgroundQuery:=False
MsgBox "done"
End Sub


you could also right click the query table and from the popup, click
refresh.


so if i have it wrong, post the code you are using.


regards
FSt1


Hi,


Here is the guts of my query ... it is executed approximately 20 times
for each run.


Sub RunQuery(ByVal i As Integer, ByVal qDate As String)


* * Dim CmdText As String


* * qDate = Format(qDate, "dd-mmm-yy")
* * Application.DisplayAlerts = False


' Set the query command text
* * CmdText = _
* * * * "Get_Value('" & TagRefs.Cells(i, 1).Value & "', '" & qDate &
"')"


' Run the query
* * With QTable
* * * .CommandText = CmdText
* * * .Refresh BackgroundQuery:=False
* * End With


* * Application.DisplayAlerts = True


End Sub


Where the QTable is defined by:


Set QTable = Config.QueryTables.Add(Connection:="ODBC;Driver={A T
SQLplus};ADS=" & Server, _
* * * * Destination:=Config.Range("B1"))


Config is a worksheet and Get_Value is a named procedu the server
name is defined elsewhere. Originally the destination for the QTable
was a defined range but after much messing around I found that Excel
doesn't seem to like named ranges on the same row as a query table -
if such a range exists the returned query is returned into a new,
Excel generated, named range in inserted columns to the left of the
previous range. If no named range exists, Excel quite happily
overwrites the names that it generates for itself.


I now remove all named ranges from the query table rows before a new
query is run - this isn't what I had originally intended and it is a
bit of a pain but it does seem to work. Any advice would still be very
welcome.


Alan- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -


Gents,

Thanks for your assistance - you were both right, it just took a while
for the penny to drop.

Once a query table exists in a workbook, don't recreate it.
Add any named ranges to the area within a query table once the query
table has been created - not before!

Simple!!

Alan
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
ODBC Query Laddie Excel Worksheet Functions 0 April 23rd 10 03:35 PM
ODBC database query gneissgirl Excel Discussion (Misc queries) 0 November 10th 05 06:18 PM
MS Query/ODBC/SQL problem Jen Excel Discussion (Misc queries) 0 December 3rd 04 03:11 PM
Problem with .Background Query option of ODBC Query Shilps Excel Programming 0 April 19th 04 06:41 AM
SQL query to ODBC gkelle[_5_] Excel Programming 2 February 12th 04 01:34 PM


All times are GMT +1. The time now is 12:02 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"