I have the VBA script as below to pull the SQL (stored procedure) and fetching to Excel. I allow the excel cell "B1" to enter the date, but I am facing the error message
Error converting varchar to datetime
I have tried to change the Excel column "B1" to date format but the error still remains. Please help. Thanks
Sub puldata()
Dim oConn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Dim mssql As String
Dim row As Integer
Dim col As Integer
Dim DateTo As Date
Dim ws As ThisWorkbook
Set ws = ThisWorkbook
Application.ScreenUpdating = False
Set oConn = New ADODB.Connection
Set rs = New ADODB.Recordset
'oConn.Parameters.Append oConn.CreateParameter("DateTo", adDate, adParamInput, 10, Range("D1").Text)
mssql = "exec dbo.HP_Receivable @DateTo = '" & Cells(1, 2) & "';"
oConn.ConnectionString = "Driver={SQL Server Native Client 11.0}; Server=10.88.71.32; Database=HCMY; UID=sa; PWD=Hcmy@sap"
'oConn.ConnectionTimeout = 30
oConn.Open
rs.Open mssql, oConn
If rs.EOF Then
MsgBox "No Matching recrods found"
rs.Close
oConn.Close
Exit Sub
End If
row = 3
col = 1
For Each fld In rs.Fields
Sheet1.Cells(row, col).Value = fld.Name
col = col + 1
Next
rs.MoveFirst
row = row + 1
Do While Not rs.EOF
col = 1
For Each fld In rs.Fields
Sheet1.Cells(row, col).Value = fld
col = col + 1
Next
row = row + 1
rs.MoveNext
Loop
rs.Close
oConn.Close
End Sub