将多个Excel文件导入一个Access表时如何添加文件名

时间:2016-06-16 14:16:41

标签: excel ms-access access-vba runtime-error import-from-excel

我使用Access VBA将多个Excel文件导入Access数据库。这将是一个包含20-50个文件和10-60K记录的月度过程。我需要包含一个"应用程序名称"它不包含在电子表格文件本身中,但是在其文件名中。我不是手动将应用程序名称添加到Excel文件中,而是希望通过我的VBA代码添加它。

我不熟练使用Access,并且通过搜索如何完成将大部分内容拼凑在一起。这"工作"但是当我在较大的批次上运行时,我收到一个错误"运行时错误' 3035':超出系统资源。'当我删除添加文件名(循环记录)的部分时,它运行正常。我认为这是因为这些步骤没有有效排序?任何帮助将不胜感激。

 Public Function Import_System_Access_Reports()

 Dim strFolder As String
 Dim db As DAO.Database
 Dim tdf As DAO.TableDef
 Dim fld As DAO.Field
 Dim rstTable As DAO.Recordset
 Dim strFile As String
 Dim strTable As String
 Dim lngPos As Long
 Dim strExtension As String
 Dim lngFileType As Long
 Dim strSQL As String
 Dim strFullFileName As String

 With Application.FileDialog(4) ' msoFileDialogFolderPicker
     If .Show Then
         strFolder = .SelectedItems(1)
     Else
         MsgBox "No folder specified!", vbCritical
         Exit Function
     End If
 End With
 If Right(strFolder, 1) <> "\" Then
     strFolder = strFolder & "\"
 End If
 strFile = Dir(strFolder & "*.xls*")
 Do While strFile <> ""

     lngPos = InStrRev(strFile, ".")
     strTable = "RawData"
     'MsgBox "table is:" & strTable
     strExtension = Mid(strFile, lngPos + 1)
     Select Case strExtension
         Case "xls"
             lngFileType = acSpreadsheetTypeExcel9
         Case "xlsx", "xlsm"
             lngFileType = acSpreadsheetTypeExcel12Xml
         Case "xlsb"
             lngFileType = acSpreadsheetTypeExcel12
     End Select
    DoCmd.TransferSpreadsheet _
         TransferType:=acImport, _
         SpreadsheetType:=lngFileType, _
         TableName:=strTable, _
         FileName:=strFolder & strFile, _
         HasFieldNames:=True ' or False if no headers

'Add and populate the new field
 'set the full file name
 strFullFileName = strFolder & strFile

'Initialize
 Set db = CurrentDb()
 Set tdf = db.TableDefs(strTable)

 'Add the field to the table.
 'tdf.Fields.Append tdf.CreateField("FileName", dbText, 255)

 'Create Recordset
 Set rstTable = db.OpenRecordset(strTable)
 rstTable.MoveFirst

 'Loop records
 Do Until rstTable.EOF
 If (IsNull(rstTable("FileName")) Or rstTable("FileName") = "") Then
     rstTable.Edit
     rstTable("FileName") = strFile
     rstTable.Update
     End If
     rstTable.MoveNext
 Loop

     strFile = Dir

 'Move to the next file
 Loop
     'Clean up
     Set fld = Nothing
     Set tdf = Nothing
     Set db = Nothing
     'rstTable.Close
     Set rstTable = Nothing

End Function

1 个答案:

答案 0 :(得分:0)

代码更简单,如果消除Recordset,运行时性能会更好。您可以在每个UPDATE

之后执行TransferSpreadsheet
Dim strFolder As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strFile As String
Dim strTable As String
Dim strExtension As String
Dim lngFileType As Long
Dim strSQL As String
Dim strFullFileName As String
Dim varPieces As Variant

' --------------------------------------------------------
'* I left out the part where the user selects strFolder *'
' --------------------------------------------------------

strTable = "RawData" '<- this could be a constant instead of a variable
Set db = CurrentDb()
' make the UPDATE a parameter query ...
strSQL = "UPDATE [" & strTable & "] SET FileName=[pFileName]" & vbCrLf & _
    "WHERE FileName Is Null OR FileName='';"
Set qdf = db.CreateQueryDef(vbNullString, strSQL)

strFile = Dir(strFolder & "*.xls*")
Do While Len(strFile) > 0
    varPieces = Split(strFile, ".")
    strExtension = varPieces(UBound(varPieces))
    Select Case strExtension
    Case "xls"
        lngFileType = acSpreadsheetTypeExcel9
    Case "xlsx", "xlsm"
        lngFileType = acSpreadsheetTypeExcel12Xml
    Case "xlsb"
        lngFileType = acSpreadsheetTypeExcel12
    End Select
    strFullFileName = strFolder & strFile
    DoCmd.TransferSpreadsheet _
            TransferType:=acImport, _
            SpreadsheetType:=lngFileType, _
            TableName:=strTable, _
            FileName:=strFullFileName, _
            HasFieldNames:=True ' or False if no headers

    ' supply the parameter value for the UPDATE and execute it ...        
    qdf.Parameters("pFileName").Value = strFile
    qdf.Execute dbFailOnError

    'Move to the next file
    strFile = Dir
Loop