hello expert
you are write, i am trying to insert the data in excel file from sql server
but i am not properly understanding your code like int32
i am sending my code for your clarification please modify it because when i execute it produces the error on line
int32 and can i change it into integer or not
other error is in for loop
" class does't support automation or interface"
please provide some help
thanks
Private Sub Command1_Click()
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim con As ADODB.Connection
Dim RSFile As ADODB.Recordset
Dim strQuery As String
Dim StrConnection As String
Set con = New ADODB.Connection
Set RSFile = New ADODB.Recordset
StrConnection = "Driver={SQL Server};Server=Irish-vul; Database=shrmgmtDb;Uid=sa"
strQuery = "select * from output"
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
oExcel.Visible = True
Dim n As int32
For n = 1 To RSFile.Fields.Count
oSheet.Cells(1, n).Value = RSFile.Fields(n - 1).Name
Next
'Transfer the data to Excel.
oSheet.Range("A2").CopyFromRecordset (RSFile)
'Save the workbook and quit Excel.
oBook.SaveAs ("C:\var\Book2.xls")
oSheet = Nothing
oBook = Nothing
'oExcel = Quit()
oExcel = Nothing
con.Open (StrConnection)
con.Execute (strQuery)
con.Close
End Sub
I assume here that your are getting data from a database say a Access or SQL? If that is the case, you must first create a connection string for that, afterwards, you can use the below code
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim RSFile as ADODB.Recordset
Dim strQuery as string
Dim StrConnection as string
strConnection(your connection string here)
strQuery(Your query here)
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
Dim n As Int32
For n = 1 To RSFile.Fields.Count
oSheet.Cells(1, n).Value = RSFile.Fields(n - 1).Name
Next
'Transfer the data to Excel.
oSheet.Range("A2").CopyFromRecordset(RSFile)
'Save the workbook and quit Excel.
oBook.SaveAs("C:\Path\Filename.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
.open (strconnection)
.execute(strquery)
.close
Hope this will help. Cheers!