我試圖從SQL數據庫中使用日期參數將數據導入到Excel中。以下VB查詢工作。而不是手動更改VB查詢的TS中的日期值,我希望查詢使用電子表格中的單元格值。單元格A1具有用於> = TS的時間,和單元格A2具有用於< TS與日期參數鏈接到單元格值的VBA Excel SQL查詢
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
我收到一條錯誤消息運行時錯誤'-2147217908(80040e0c)':命令文本未設置爲命令對象。 – Gary 2013-05-11 06:46:58
感謝您的幫助。得到它與以下工作: – Gary 2013-05-17 05:23:17