Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default Not able to declare variable in MS Query.

Hi All,

I am seriously in a problem.

I have recorded a macro which pull's out data from a database with the
help of MS query.

The user of this macro will pull the data from a specific date to a
specific date and for that I have applied filter in the query itself.
But my problem comes when I try to fire the query by declaring a
variable.

The error which I get is mentioned below.
Run-time error '1004':
General ODBC Error

I have declared the date as variable. Please help, here is my code.

Sub Macro5()
'
' Macro5 Macro
'

Dim Ddate1 as Date
Dim Ddate2 as Date

Ddate1 = ThisWorkbook.Sheets("Report Manager").Range("C3").Value
Ddate2 = ThisWorkbook.Sheets("Report Manager").Range("C4").Value



With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array
( _
"ODBC;DSN=Excel Files;DBQ=\\10.253.23.5\TPS\TPS_COLLECTIONS
\MIS Report\Data Base\Consolidated Database Of Call
Audits.xlsm;DefaultDir" _
), Array( _
"=\\10.253.23.5\TPS\TPS_COLLECTIONS\MIS Report\Data
Base;DriverId=1046;MaxBufferSize=2048;PageTimeout= 5;" _
)), Destination:=Range("$A$9")).QueryTable
.CommandText = Array( _
"SELECT `Sheet1$`.`Audit Date`, `Sheet1$`.`Agent Name`,
`Sheet1$`.`F ID`, `Sheet1$`.Evaluator" & Chr(13) & "" & Chr(10) &
"FROM `Sheet1$` `Sheet1$`" & Chr(13) & "" & Chr(10) & "WHERE
(`Sheet1$`.`Audit Date`={ts Ddate1} And `Sheet1$`.`Audit D" _
, "ate`<={ts Ddate2})" & Chr(13) & "" & Chr(10) & "ORDER BY
`Sheet1$`.`Audit Date`")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_Excel_Files"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.ListObjects("Table_Query_from_Excel_Fi les").TableStyle
= ""
End Sub

Regards
Heera
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Not able to declare variable in MS Query.

Your problem is that you are using the text Ddate1 in your query and not the
values associated with the variable Ddate1. Try this to see what I mean. Add
a message box...

msgbox "Audit Date`={ts Ddate1} And `Sheet1$"
and another message box
msgbox "Audit Date`={ts " & Ddate1 & "} And `Sheet1$"
--
HTH...

Jim Thomlinson


"Heera" wrote:

Hi All,

I am seriously in a problem.

I have recorded a macro which pull's out data from a database with the
help of MS query.

The user of this macro will pull the data from a specific date to a
specific date and for that I have applied filter in the query itself.
But my problem comes when I try to fire the query by declaring a
variable.

The error which I get is mentioned below.
Run-time error '1004':
General ODBC Error

I have declared the date as variable. Please help, here is my code.

Sub Macro5()
'
' Macro5 Macro
'

Dim Ddate1 as Date
Dim Ddate2 as Date

Ddate1 = ThisWorkbook.Sheets("Report Manager").Range("C3").Value
Ddate2 = ThisWorkbook.Sheets("Report Manager").Range("C4").Value



With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array
( _
"ODBC;DSN=Excel Files;DBQ=\\10.253.23.5\TPS\TPS_COLLECTIONS
\MIS Report\Data Base\Consolidated Database Of Call
Audits.xlsm;DefaultDir" _
), Array( _
"=\\10.253.23.5\TPS\TPS_COLLECTIONS\MIS Report\Data
Base;DriverId=1046;MaxBufferSize=2048;PageTimeout= 5;" _
)), Destination:=Range("$A$9")).QueryTable
.CommandText = Array( _
"SELECT `Sheet1$`.`Audit Date`, `Sheet1$`.`Agent Name`,
`Sheet1$`.`F ID`, `Sheet1$`.Evaluator" & Chr(13) & "" & Chr(10) &
"FROM `Sheet1$` `Sheet1$`" & Chr(13) & "" & Chr(10) & "WHERE
(`Sheet1$`.`Audit Date`={ts Ddate1} And `Sheet1$`.`Audit D" _
, "ate`<={ts Ddate2})" & Chr(13) & "" & Chr(10) & "ORDER BY
`Sheet1$`.`Audit Date`")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_Excel_Files"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.ListObjects("Table_Query_from_Excel_Fi les").TableStyle
= ""
End Sub

Regards
Heera

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Not able to declare variable in MS Query.

Jim i did not understood what you are trining to explane me.

And how do i add Message box.

It will be great help for me if modify my query and show me an example.
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
Declare variable as what? N E Body Excel Programming 6 October 28th 04 01:06 AM
Declare Variable zapatista66[_14_] Excel Programming 0 October 7th 04 05:02 PM
Declare Variable zapatista66[_12_] Excel Programming 2 October 7th 04 04:55 PM
Declare Variable zapatista66[_13_] Excel Programming 0 October 7th 04 04:13 PM
Declare Variable zapatista66[_11_] Excel Programming 1 October 7th 04 03:23 PM


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