我正在尝试使用日期参数将数据从 SQL 数据库中提取到 Excel 中。以下 VB 查询有效。我不想手动更改 VB 查询的 TS 中的日期值,而是让查询使用电子表格中的单元格值。单元格 A1 的日期为 >= TS,单元格 A2 的日期为 < TS
Sub vba_query_01()
Dim oCon As ADODB.Connection
Dim oRS As ADODB.Recordset
Set oCon = New ADODB.Connection
oCon.ConnectionString = "DRIVER=SQL Server;SERVER=GSEYBERTHNB7
\SQLEXPRESS;UID=gseyberth;Trusted_Connection=Yes;APP=2007 Microsoft Office
system;WSID=GSEYBERTHNB7;DATABASE=DATA_LOGGER"
oCon.Open
Set oRS = New ADODB.Recordset
oRS.ActiveConnection = oCon
oRS.Source = "Select * FROM DATA_LOGGER.dbo.LYLE LYLE WHERE (( [Date] >= {TS '2013-04-24
07:00:00'} )) AND (( [Date] < {TS '2013-04-24 15:00:00'} ))"
oRS.Open
Range("A10").CopyFromRecordset oRS
oRS.Close
oCon.Close
If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCon Is Nothing Then Set oCon = Nothing
End Sub