Reputation: 35
I have this code that exports data from my database access to an excel file from http://www.vbforums.com/showthread.php?870553-How-to-export-selected-ms-access-data-to-ms-excel-with-Vb-6-0&highlight=how+to+export+selected+data+from+excel++to+ms+access. this time i want to export to an an excel file with a template, lets say my template excel filename is Template1.xltx. how can i modify my code to use this template?
Private Function LoopExport()
Dim SSql As String
Dim DateFrom As String
Dim DateTo As String
DateFrom = Format(DTPicker1.Value, "\#mm\/dd\/yyyy\#")
DateTo = Format(DTPicker2.Value, "\#mm\/dd\/yyyy\#")
SSql = "SELECT * INTO [" & (Label1.Caption) & "] In '" &
(Text1.Text) & " ' 'EXCEL 8.0;' FROM " & (Label1) & " "
SSql = SSql & " Where DateValue(Date_today) Between " & DateFrom
& " And " & DateTo & ""
con1.Execute SSql
End Function
Private Sub Export_Click()
sql = "SELECT * FROM Accounts "
With rs
Set rs = New ADODB.Recordset
rs.Open sql, con, adOpenDynamic, adLockOptimistic
Do While Not rs.EOF
Label1 = rs!AlphaIndex
LoopExport
rs.MoveNext
Loop
Set rs = Nothing
End With
End Sub
Upvotes: 0
Views: 1322
Reputation: 5696
Inside the LoopExport function you can customize the sql to reach an specific excel file. I didn't include all your code, but you can add the Database and Sheet name sections and let me know if it works.
' Credits: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/9aa810f1-8c5c-49ba-bfad-11e1c23c59cf/import-access-database-data-into-excel?forum=vbgeneral
SSql = "SELECT * INTO [Excel 12.0;DATABASE=c:\temp\Template.xlsx].[SheetName] FROM [Table1]"
Upvotes: 2