Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How do I use variable dates with a SQL Query

I recorded a macro in Excel that will import data from a SQL Table . The
data imprted will be between two dates. Does anyone know how I can set the
two dates in two seperate cells on a worksheet and then run the macro. I
have tried several things and had no luck. I included the code with this
message;

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/18/2006
'

With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=sqlserver;UID=sa;PWD=sa;APP=Microsof t Office
XP;WSID=SGRENELL-XP;DATABASE=Coal" _
, Destination:=Range("A1"))
.CommandText = Array( _
"SELECT coal_data.date, coal_data.truck_number,
coal_data.delivery_no, coal_data.Tare, coal_data.Gross, coal_data.Netlbs,
coal_data.NetTons" & Chr(13) & "" & Chr(10) & "FROM coal.dbo.coal_data
coal_data" & Chr(13) & "" & Chr(10) & "WHERE (coal_data.date={t" _
, _
"s '2006-05-01 00:00:00'} And coal_data.date<={ts'2006-05-18
00:00:00'})" & Chr(13) & "" & Chr(10) & "ORDER BY coal_data.date,
coal_data.delivery_no" _
)


.Name = "Query from sqlserver"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How do I use variable dates with a SQL Query

Dim s as String, s1 as String
With Worksheets("Sheet1")
s = Format(.Range("A1").Value,"yyyy-mm-dd hh:mm:ss")
s2 = Format(.Range("B1").Value,"yyyy-mm-dd hh:mm:ss")
End With


then
.. . .
"WHERE (coal_data.date={ts'" & s & _
"'} And coal_data.date<={ts'" & s1 & "'})"
.. . .

--
Regards,
Tom Ogilvy


"MegaWatt" wrote in message
...
I recorded a macro in Excel that will import data from a SQL Table . The
data imprted will be between two dates. Does anyone know how I can set

the
two dates in two seperate cells on a worksheet and then run the macro. I
have tried several things and had no luck. I included the code with this
message;

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/18/2006
'

With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=sqlserver;UID=sa;PWD=sa;APP=Microsof t Office
XP;WSID=SGRENELL-XP;DATABASE=Coal" _
, Destination:=Range("A1"))
.CommandText = Array( _
"SELECT coal_data.date, coal_data.truck_number,
coal_data.delivery_no, coal_data.Tare, coal_data.Gross, coal_data.Netlbs,
coal_data.NetTons" & Chr(13) & "" & Chr(10) & "FROM coal.dbo.coal_data
coal_data" & Chr(13) & "" & Chr(10) & "WHERE (coal_data.date={t" _
, _
"s '2006-05-01 00:00:00'} And coal_data.date<={ts'2006-05-18
00:00:00'})" & Chr(13) & "" & Chr(10) & "ORDER BY coal_data.date,
coal_data.delivery_no" _
)


.Name = "Query from sqlserver"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How do I use variable dates with a SQL Query

Tom

Thanks for the help. It works great.

"Tom Ogilvy" wrote:

Dim s as String, s1 as String
With Worksheets("Sheet1")
s = Format(.Range("A1").Value,"yyyy-mm-dd hh:mm:ss")
s2 = Format(.Range("B1").Value,"yyyy-mm-dd hh:mm:ss")
End With


then
.. . .
"WHERE (coal_data.date={ts'" & s & _
"'} And coal_data.date<={ts'" & s1 & "'})"
.. . .

--
Regards,
Tom Ogilvy


"MegaWatt" wrote in message
...
I recorded a macro in Excel that will import data from a SQL Table . The
data imprted will be between two dates. Does anyone know how I can set

the
two dates in two seperate cells on a worksheet and then run the macro. I
have tried several things and had no luck. I included the code with this
message;

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/18/2006
'

With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=sqlserver;UID=sa;PWD=sa;APP=Microsof t Office
XP;WSID=SGRENELL-XP;DATABASE=Coal" _
, Destination:=Range("A1"))
.CommandText = Array( _
"SELECT coal_data.date, coal_data.truck_number,
coal_data.delivery_no, coal_data.Tare, coal_data.Gross, coal_data.Netlbs,
coal_data.NetTons" & Chr(13) & "" & Chr(10) & "FROM coal.dbo.coal_data
coal_data" & Chr(13) & "" & Chr(10) & "WHERE (coal_data.date={t" _
, _
"s '2006-05-01 00:00:00'} And coal_data.date<={ts'2006-05-18
00:00:00'})" & Chr(13) & "" & Chr(10) & "ORDER BY coal_data.date,
coal_data.delivery_no" _
)


.Name = "Query from sqlserver"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
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
Variable passed to database query Vijay Kotian Excel Discussion (Misc queries) 1 August 29th 08 05:00 PM
Deleting variable web query name wayliff[_13_] Excel Programming 1 January 17th 06 08:57 PM
With QUERY how to input a variable Guillaume Excel Discussion (Misc queries) 1 December 8th 05 02:28 PM
put query result into variable andy[_6_] Excel Programming 1 October 24th 05 07:39 AM
Query that returns a value to variable mas Excel Programming 0 March 7th 05 09:22 PM


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