Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Help with error 1004 in database query

I am writing a block of Visual Basic code for use with
Excel:

With ActiveSheet.QueryTables.Add
(Connection:=connectionString, _
Destination:=Range("A1"))
.CommandText = SQLText
.Name = DataSourceName & "Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

However, at the statement ".Refresh
BackgroundQuery:=False", I got a
run-time error 1004: Application-defined or object-
defined error.

The strange thing is that the same query worked well for
a few days before ...

Does anyone know what is the cause of the problem? How I
can solve this problem? Please help!

Thank you very much.
Alex.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 280
Default Help with error 1004 in database query

Confirm that you can connect to the data source outside of Excel using the
same info as in the connection string, and that you can execute the query
that is SQLText on that connection. Be sure the SQL that you run in your
test is the same as what is in your code. The code adds the querytable, but
the querytable doesn't execute (and you don't know if it is valid) until
Refresh is called. My guess is that it fails because it can't execute due to
some error in the definition.

Another tactic: Try creating a similar query against the same datasource
while recording a macro. If it succeeds, compare the recorded code to the
original code.


"Alex Zhang" wrote in message
...
I am writing a block of Visual Basic code for use with
Excel:

With ActiveSheet.QueryTables.Add
(Connection:=connectionString, _
Destination:=Range("A1"))
.CommandText = SQLText
.Name = DataSourceName & "Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With

However, at the statement ".Refresh
BackgroundQuery:=False", I got a
run-time error 1004: Application-defined or object-
defined error.

The strange thing is that the same query worked well for
a few days before ...

Does anyone know what is the cause of the problem? How I
can solve this problem? Please help!

Thank you very much.
Alex.




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
Import New Database Query (Union Query) in Spreadsheet klock Excel Discussion (Misc queries) 2 September 24th 09 01:30 AM
odbc error on Excel query into database flowry Excel Discussion (Misc queries) 2 November 6th 08 09:26 PM
database query not showing foxpro database How I import data mangat New Users to Excel 1 June 24th 07 03:31 PM
Anyone Else Use Database Query to Query Another Sheet in the Same Excel Workbook? jocke Excel Discussion (Misc queries) 1 November 29th 05 01:44 PM
How to use a Access Query that as a parameter into Excel database query Karen Middleton Excel Discussion (Misc queries) 1 December 13th 04 07:54 PM


All times are GMT +1. The time now is 04:36 AM.

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"