Hi, everybody
I am not sure this is the correct forum to ask my question. But any help can orient me towards the solution.
I am developing a VB.NET Sub which can get data from SQL Server to my Excel Sheet at lightning speed (using ADODB connection).
My Sub is written in VB.NET, not VBA. But I verified that it also works in VBA.
Here is my code:
MY PROBLEM
My code works perfectly if I set Option Strict OFF.
But if I set Option Strict On, there is an error at this line:
Info: This line of code adds the headers which are missing because "CopyFromRecordset" does not copy headers.
The error Message is:
Can anybody help me write the above line correctly?
Thanks
Leon
I am not sure this is the correct forum to ask my question. But any help can orient me towards the solution.
I am developing a VB.NET Sub which can get data from SQL Server to my Excel Sheet at lightning speed (using ADODB connection).
My Sub is written in VB.NET, not VBA. But I verified that it also works in VBA.
Here is my code:
Code:
Option Strict On
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.ComponentModel
Imports AddinExpress.MSO
Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.DataSet
Code:
Private Sub AdxRibbonButton1_*******(ByVal sender As System.Object, ByVal control As AddinExpress.MSO.IRibbonControl, ByVal pressed As System.Boolean) Handles AdxRibbonButton1.*******
Dim Conn As New ADODB.Connection
Dim recset As New ADODB.Recordset
Dim sqlQry As String, sConnect As String
Dim xlWb As Excel._Workbook
Dim xlWsht As Excel.Worksheet = TryCast(ExcelApp.ActiveSheet, Excel.Worksheet)
xlWsht.Cells.ClearContents()
sqlQry = "EXECUTE[dbo].[MyStoredProcedure]"
sConnect = "Driver=SQL Server;Server=MyServer; Database=MyDatabase; User Id = sa; Password= 12345"
Conn.Open(sConnect)
recset = New ADODB.Recordset
recset.Open(sqlQry, Conn)
Dim icols As Integer
For iCols = 0 To recset.Fields.Count - 1
'//------------ MY PROBLEM IS HERE --------------------------------
xlWsht.Cells(1, icols + 1).value = recset.Fields(icols).Name
'// -----------------------------------------------------------------------
Next
xlWsht.Range("A2").CopyFromRecordset(recset)
recset.Close()
Conn.Close()
recset = Nothing
End Sub
MY PROBLEM
My code works perfectly if I set Option Strict OFF.
But if I set Option Strict On, there is an error at this line:
Code:
xlWsht.Cells(1, icols + 1).value = recset.Fields(icols).Name
Info: This line of code adds the headers which are missing because "CopyFromRecordset" does not copy headers.
The error Message is:
Option Strict On disallows late binding
Can anybody help me write the above line correctly?
Thanks
Leon
Last edited: