Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Trying to make Date Range variable in VBA SQL Query

I have the following code that pulls information from my time and billing
database and drops it into a pivot table to summarize everything by Partner
(CppLname). What I want to do is to make the date range variable but, due to
the format of the date field in the SQL database (date plus time combined)
I'm running into problems.

Here's the code:

With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExter nal)
.Connection = Array(Array( _
"ODBC;Description=Margvpm SQL;DRIVER=SQL
Server;SERVER=MARGFPS01;UID=;PWD=;APP=Microsoft® Access;WSID=;D" _
), Array("ATABASE=margvpm"))
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT Clients.CPPLname, WIP.Wdate, WIP.Whours, WIP.Wfee,
WIP.Wrate" & Chr(13) & "" & Chr(10) & "FROM margvpm.dbo.Clients Clients,
margvpm.dbo.WIP WIP" & Chr(13) & "" & Chr(10) & "WHERE WIP.WCltID =
Clients.ID AND ((WIP.Wdate{ts '2005-09-30 00:00:00'} And" _
, _
" WIP.Wdate<={ts '2006-09-30 00:00:00'}))" & Chr(13) & "" & Chr(10)
& "ORDER BY Clients.CPPLname, WIP.Wdate" _
)
.CreatePivotTable TableDestination:="[Book2]Sheet1!R3C1",
TableName:= _
"PivotTable2", DefaultVersion:=xlPivotTableVersion10
End With

The WHERE statement where it loks at the WIP.wdate field is the problem.
How can I drop a variable into this? I've tried a couple of solutions I
found here but nothing has worked (the .CreatePivotTable section crashes,
presumably due to incorrect or no data returned). HELP!!!!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Trying to make Date Range variable in VBA SQL Query

s1 = "2005-09-30 00:00:00"
s2 = "2006-09-30 00:00:00"

. . .
WIP.WCltID =
Clients.ID AND ((WIP.Wdate{ts '" & s1 & "'} And" _
, _
" WIP.Wdate<={ts '" & s2 & "'}))" & Chr(13) & "" & Chr(10)

.. . .

--
Regards,
Tom Ogilvy


"Tim French" wrote:

I have the following code that pulls information from my time and billing
database and drops it into a pivot table to summarize everything by Partner
(CppLname). What I want to do is to make the date range variable but, due to
the format of the date field in the SQL database (date plus time combined)
I'm running into problems.

Here's the code:

With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExter nal)
.Connection = Array(Array( _
"ODBC;Description=Margvpm SQL;DRIVER=SQL
Server;SERVER=MARGFPS01;UID=;PWD=;APP=Microsoft® Access;WSID=;D" _
), Array("ATABASE=margvpm"))
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT Clients.CPPLname, WIP.Wdate, WIP.Whours, WIP.Wfee,
WIP.Wrate" & Chr(13) & "" & Chr(10) & "FROM margvpm.dbo.Clients Clients,
margvpm.dbo.WIP WIP" & Chr(13) & "" & Chr(10) & "WHERE WIP.WCltID =
Clients.ID AND ((WIP.Wdate{ts '2005-09-30 00:00:00'} And" _
, _
" WIP.Wdate<={ts '2006-09-30 00:00:00'}))" & Chr(13) & "" & Chr(10)
& "ORDER BY Clients.CPPLname, WIP.Wdate" _
)
.CreatePivotTable TableDestination:="[Book2]Sheet1!R3C1",
TableName:= _
"PivotTable2", DefaultVersion:=xlPivotTableVersion10
End With

The WHERE statement where it loks at the WIP.wdate field is the problem.
How can I drop a variable into this? I've tried a couple of solutions I
found here but nothing has worked (the .CreatePivotTable section crashes,
presumably due to incorrect or no data returned). HELP!!!!

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
remote query & date range? Linn Kubler Excel Programming 0 March 7th 06 03:19 PM
How do I enter a date range ie -7 from current date in MS QUERY notsmartenough Excel Discussion (Misc queries) 1 November 11th 05 10:17 PM
Query on Date range does not qualify the Year Malcolm Makin[_2_] Excel Programming 5 June 23rd 05 07:00 PM
'ActiveWorkbook.Names.Add Name:' how to make range variable? TeeSea Excel Programming 1 June 9th 05 03:42 PM
Changing fixed date with a variable in a ODBC Query Hande & Tolga Excel Programming 1 September 22nd 03 11:00 AM


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