exporting a form to excel in vba

mana

Registered User.
Local time
Today, 07:21
Joined
Nov 4, 2014
Messages
265
Hello

i have a form and i want to export it to excel file
the form will be updated someties and the data will be changed
here is my code, but there is a problem with it

Code:
Private Sub Command0_Click()
Dim xlApp As Object
Dim xlBook As Object
Dim rs As ADODB.Recordset
Dim sql As String
Dim i As Integer
Dim Conn1 As ADODB.Connection
Dim Cmd1 As ADODB.Command
 
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
 sql = Forms("frm_übersichtnew").Form.RecordSource 'Your record source if not a subform
Set rs = CurrentDb
 
 For i = 1 To rs.Fields.Count
xlBook.Sheets(1).Cells(1, i) = rs.Fields(i - 1).Name 'Write Field names to Excel
Next i
xlBook.Sheets(1).Cells(2, 1).CopyFromRecordset rs 'Import the recordset data through Excel
' You can add whatever other formatting you want by running Excel VBA throught the xlApp object
xlApp.Visible = True
Set xlApp = Nothing
Set xlBook = Nothing
Set rs = Nothing

End Sub
can you help me please?
thank you
 
Take a look at the the TransferSpreadsheet command, which transfers a table ot query to an Excel file and range, as apropriate.
 

Users who are viewing this thread

Back
Top Bottom