Desu Nota from Columbus
Well-known Member
- Joined
- Mar 17, 2011
- Messages
- 556
I got the following code to run (it pulls data from a SQL Server based on the SQL Code line)
I tried to add another facet to this working macro. I want to input two parameters in A1 and A2 (start time, end time) and apply these to the SQL Command line with a where statement. I tried the following (changes are in blue):
I get the error code,
Run-Time Error '-2147217913
[Microsoft][ODBC SQL Server Driver][SQL Server] Conversion failed when converting datetime from character string
The line: rs.Open SQLStr, Cn, adOpenStatic is highlighted.
The format of the datetime for SQL is the following string 'yyyy-mm-dd hh:mm:ss.sss' and I matched the contents of A1 and A2 to follow that requirement exactly (formatted as Text).
When I use the following in the code, the macro works perfectly:
So this leads me to believe that the error is coming from using the RangeA1/A2.
Once the data is imported into excel (using a manual import) excel sees the datetime as m/d/yyyy h:mm
I think this is all the information I can provide.
I am not familiar with userforms, but if you were asked to input the start time and end time manually that might work.
Any ideas or work arounds?
Rich (BB code):
Sub PullData()
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Server_Name = "GGVORNE\SQLEXPRESS" ' Enter your server name here
Database_Name = "VorneDB" ' Enter your database name here
User_ID = "vorne" ' enter your user ID here
Password = "VData!!!" ' Enter your password here
SQLStr = "Select * From [VorneDB].[dbo].[interval_stream]" ' Enter your SQL here
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("sheet1").Range("a1:z50000") ' Enter your sheet name and range here
.ClearContents
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
Rich (BB code):
Sub PullData()
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim LResultA As String
Dim LResultB As String
LResultA = Replace("Variable1", "Variable1", Range("A1"))
LResultB = Replace("Variable2", "Variable2", Range("A2"))
Server_Name = "GGVORNE\SQLEXPRESS" ' Enter your server name here
Database_Name = "VorneDB" ' Enter your database name here
User_ID = "vorne" ' enter your user ID here
Password = "VData!!!" ' Enter your password here
SQLStr = "Select * From [VorneDB].[dbo].[interval_stream] Where [start_time] >= 'Variable1' and [end_time] < 'Variable2'" ' Enter your SQL here
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("sheet1").Range("a1:z50000") ' Enter your sheet name and range here
.ClearContents
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
Run-Time Error '-2147217913
[Microsoft][ODBC SQL Server Driver][SQL Server] Conversion failed when converting datetime from character string
The line: rs.Open SQLStr, Cn, adOpenStatic is highlighted.
The format of the datetime for SQL is the following string 'yyyy-mm-dd hh:mm:ss.sss' and I matched the contents of A1 and A2 to follow that requirement exactly (formatted as Text).
When I use the following in the code, the macro works perfectly:
Rich (BB code):
SQLStr = "Select * From [VorneDB].[dbo].[interval_stream] Where [start_time] >= '2011-06-03 07:00:00.000' and [end_time] < '2011-06-06 07:00:00.000'" ' Enter your SQL here
So this leads me to believe that the error is coming from using the RangeA1/A2.
Once the data is imported into excel (using a manual import) excel sees the datetime as m/d/yyyy h:mm
I think this is all the information I can provide.
I am not familiar with userforms, but if you were asked to input the start time and end time manually that might work.
Any ideas or work arounds?
Last edited: