如何使用VB 6.0使用模板将MS Access数据导出到excel?

时间:2019-02-26 01:46:50

标签: excel vb6 access

我有这段代码可以将数据库访问权限中的数据导出到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中的excel文件中。这次我想导出到带有模板的Excel文件,可以说我的模板excel文件名是Template1.xltx。如何修改我的代码以使用此模板?

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

1 个答案:

答案 0 :(得分:0)

在LoopExport函数中,您可以自定义sql以访问特定的excel文件。我并没有提供所有代码,但是您可以添加“数据库”和“工作表名称”部分,并让我知道它是否有效。

' 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]"