使用Excel VBA将数据导出到MS Access表
我目前使用下面的代码从工作表导出到MS Access数据库的数据,代码循环每一行,并插入数据到MS Access表。
Public Sub TransData() Application.ScreenUpdating = False Application.EnableAnimations = False Application.EnableEvents = False Application.DisplayAlerts = False ActiveWorkbook.Worksheets("Folio_Data_original").Activate Call MakeConnection("fdMasterTemp") For i = 1 To rcount - 1 rs.AddNew rs.Fields("fdName") = Cells(i + 1, 1).Value rs.Fields("fdDate") = Cells(i + 1, 2).Value rs.Update Next i Call CloseConnection Application.ScreenUpdating = True Application.EnableAnimations = True Application.EnableEvents = True Application.DisplayAlerts = True End Sub
Public Function MakeConnection(TableName As String) As Boolean '*********Routine to establish connection with database Dim DBFullName As String Dim cs As String DBFullName = Application.ActiveWorkbook.Path & "\FDData.mdb" cs = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName & ";" Set cn = CreateObject("ADODB.Connection") If Not (cn.State = adStateOpen) Then cn.Open cs End If Set rs = CreateObject("ADODB.Recordset") If Not (rs.State = adStateOpen) Then rs.Open TableName, cn, adOpenKeyset, adLockOptimistic End If End Function
Public Function CloseConnection() As Boolean '*********Routine to close connection with database On Error Resume Next If Not rs Is Nothing Then rs.Close End If If Not cn Is Nothing Then cn.Close End If CloseConnection = True Exit Function End Function
上面的代码对于数百行logging工作正常,但显然它会导出更多的数据,就像25000条logging一样,是否可以导出而不循环所有logging,只需要一条SQL INSERT语句将所有数据批量插入到Ms.Access表在一个去吗?
任何帮助将不胜感激。
编辑:问题解决
如果有人想find这个信息,我已经做了大量的search,发现下面的代码对我来说工作很好,而且由于SQL INSERT的原因,它是真正快速的(27648条logging只需3秒! ):
Public Sub DoTrans() Set cn = CreateObject("ADODB.Connection") dbPath = Application.ActiveWorkbook.Path & "\FDData.mdb" dbWb = Application.ActiveWorkbook.FullName dbWs = Application.ActiveSheet.Name scn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath dsh = "[" & Application.ActiveSheet.Name & "$]" cn.Open scn ssql = "INSERT INTO fdFolio ([fdName], [fdOne], [fdTwo]) " ssql = ssql & "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh cn.Execute ssql End Sub
仍在努力添加特定的字段名称,而不是使用“Select *”,尝试了各种方法来添加字段名称,但目前无法使其工作。
有没有可能导出没有循环所有logging
对于具有大量行的Excel中的范围,如果您在Excel中创buildAccess.Application
对象,然后使用它将Excel数据导入到Access中,则可能会看到一些性能改进。 以下代码位于包含以下testing数据的相同Excel文档中的VBA模块中
Option Explicit Sub AccImport() Dim acc As New Access.Application acc.OpenCurrentDatabase "C:\Users\Public\Database1.accdb" acc.DoCmd.TransferSpreadsheet _ TransferType:=acImport, _ SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _ TableName:="tblExcelImport", _ Filename:=Application.ActiveWorkbook.FullName, _ HasFieldNames:=True, _ Range:="Folio_Data_original$A1:B10" acc.CloseCurrentDatabase acc.Quit Set acc = Nothing End Sub
@Ahmed
下面是代码,指定插入到MS Access命名范围的字段。 关于这个代码的好处是,你可以在Excel中命名你的字段,无论你想要什么(如果你使用*,那么这些字段必须完全匹配Excel和Access之间),你可以看到我已经命名Excel列“哈哈”即使Access列被称为“dte”。
Sub test() dbWb = Application.ActiveWorkbook.FullName dsh = "[" & Application.ActiveSheet.Name & "$]" & "Data2" 'Data2 is a named range sdbpath = "C:\Users\myname\Desktop\Database2.mdb" sCommand = "INSERT INTO [main] ([dte], [test1], [values], [values2]) SELECT [haha],[test1],[values],[values2] FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh Dim dbCon As New ADODB.Connection Dim dbCommand As New ADODB.Command dbCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sdbpath & "; Jet OLEDB:Database Password=;" dbCommand.ActiveConnection = dbCon dbCommand.CommandText = sCommand dbCommand.Execute dbCon.Close End Sub