我目前需要每两周将许多SQL_Developer'Loader'文件加载到一个Access DB中,而进行导入的机器可以是随机的。下面的代码在较新的电脑上可以正常工作(i5 16gb ram,i7 16gb ram)。但由于某些原因,在一台不太好的计算机上(i3 8gb ram,各种笔记本电脑),我调用 imexImport_LoaderFile 几次后,它不会将任何东西加载到指定的表中。它在前几次都能正常工作,然后随着对imexImport_LoaderFile函数的调用列表的下降,它就会随机停止,而不会将信息加载到访问表中。如果我重新启动计算机,它就可以在列表或 imexImport_LoaderFile 调用中继续往下走。它失败的表是随机的,但在随后的每次尝试后,它在imexImport_LoaderFile调用列表中的位置越来越低。然后我可以放弃这台可悲的计算机,走到一个新的模型上,它从头到尾都能正常加载。我假设是内存管理问题,但我在函数中清除了我的对象,所以我真的很困惑。任何输入都将是惊人的。哦,源文件必须是LOADER文件,这就是为什么我甚至使用RunSavedImportExport。
Sub XYZ
do things..
ColNames = Array("a", "b", "c", "d", "e", "f", "g")
CurrentTempTableMaxDate = imexImport_LoaderFile(FilePath, FileName, AccessTableNameTemp, AccessTableName, ColNames)
If CurrentTempTableMaxDate <= DBTablesMaxDate Then Err.Raise Number:=10002
ColNames = Array("1", "2", "3", "4", "5", "6", "7")
CurrentTempTableMaxDate = imexImport_LoaderFile(FilePath, FileName, AccessTableNameTemp, AccessTableName, ColNames)
If CurrentTempTableMaxDate <= DBTablesMaxDate Then Err.Raise Number:=10002
ColNames = Array("a1", "b2", "c3", "d4", "e5", "f6", "g7")
CurrentTempTableMaxDate = imexImport_LoaderFile(FilePath, FileName, AccessTableNameTemp, AccessTableName, ColNames)
If CurrentTempTableMaxDate <= DBTablesMaxDate Then Err.Raise Number:=10002
do other things...
end sub
Public Function imexImport_LoaderFile(ByVal FilePath As String, ByVal FileName As String, ByVal AccessTempTableName As String, ByVal AccessTableName As String, ByVal ColNames As Variant) As Date
Dim name_of_spec As String
Dim imexObjs As Object
Dim ColNumber As Integer
Dim xml As String
name_of_spec = "imspec" & FileName
On Error Resume Next
If CurrentProject.ImportExportSpecifications.Count > 0 Then
If Err.Number = 0 Then
Set imexObjs = CurrentProject.ImportExportSpecifications
For Each imexObj In imexObjs
If imexObj.NAME = name_of_spec Then imexObj.Delete
Next
Set imexObjs = Nothing
End If
End If
If TableExist(AccessTempTableName) Then DoCmd.DeleteObject acTable = acDefault, AccessTempTableName
LoaderFile_Cleanup (FilePath & FileName)
xml = ""
xml = xml & "<?xml version=""1.0"" encoding=""utf-8"" ?>" & vbCrLf
xml = xml & "<ImportExportSpecification Path=" & Chr(34) & FilePath & FileName & Chr(34) & " xmlns=""urn:www.microsoft.com/office/access/imexspec"">" & vbCrLf
xml = xml & " <ImportText TextFormat=""Delimited"" FirstRowHasNames=""0"" FieldDelimiter=""|"" CodePage=""437"" Destination=" & Chr(34) & AccessTempTableName & Chr(34) & " >" & vbCrLf
xml = xml & " <DateFormat DateOrder=""MDY"" DateDelimiter=""/"" TimeDelimiter="":"" FourYearDates=""true"" DatesLeadingZeros=""false"" />" & vbCrLf
xml = xml & " <NumberFormat DecimalSymbol=""."" />" & vbCrLf
xml = xml & " <Columns PrimaryKey=""{none}"">" & vbCrLf
For ColNumber = LBound(ColNames) To UBound(ColNames)
xml = xml & " <Column Name=""Col" & ColNumber + 1 & """ FieldName=""" & ColNames(ColNumber) & """ Indexed=""NO"" SkipColumn=""false"" DataType=""Text"" Width=""12"" />" & vbCrLf
Next ColNumber
xml = xml & " </Columns>" & vbCrLf
xml = xml & " </ImportText>" & vbCrLf
xml = xml & "</ImportExportSpecification>"
'''debug.print xml
CurrentProject.ImportExportSpecifications.Add name_of_spec, Trim(xml)
DoCmd.RunSavedImportExport name_of_spec
imexImport_LoaderFile = DMax("[MEGA_EFFDT]", "[" & AccessTempTableName & "]") 'Format(DMax("[EXPORTDATE]", "[" & AccessTempTableName & "]"), "mm/dd/yyyy") 'CDate(Int(DMax("[EXPORTDATE]", "[" & AccessTempTableName & "]")))
''debug.print imexImport_LoaderFile
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, AccessTempTableName, CID_BE_Dir & AccessTempTableName, True
CleanExitTask:
Set imexObjs = Nothing
End Function
好吧,所以这并不是真正的修复,但它的工作方式,似乎在我的网络上工作。我只是把它贴在这里,以防它能帮助别人,因为这对我来说是个噩梦。我最好的猜测是当使用imex规范和muliple大文件时,它的网络防病毒问题。总之,我的解决方法是增加一个重试。现在,当导入失败时,它只是再次尝试。到目前为止,它已经工作在每一个第二次尝试时,如果它出现。
Public Function imexImport_LoaderFile(ByVal FilePath As String, ByVal FileName As String, ByVal AccessTempTableName As String, ByVal AccessTableName As String, ByVal ColNames As Variant) As Date
Dim name_of_spec As String
Dim imexObjs As Object
Dim ColNumber As Integer
Dim xml As String
name_of_spec = "imspec" & FileName
On Error Resume Next
retry_import:
If CurrentProject.ImportExportSpecifications.Count > 0 Then
If Err.Number = 0 Then
Set imexObjs = CurrentProject.ImportExportSpecifications
For Each imexObj In imexObjs
If imexObj.NAME = name_of_spec Then imexObj.Delete
Next
Set imexObjs = Nothing
End If
End If
LoaderFile_Cleanup (FilePath & FileName)
xml = ""
xml = xml & "<?xml version=""1.0"" encoding=""utf-8"" ?>" & vbCrLf
xml = xml & "<ImportExportSpecification Path=" & Chr(34) & FilePath & FileName & Chr(34) & " xmlns=""urn:www.microsoft.com/office/access/imexspec"">" & vbCrLf
xml = xml & " <ImportText TextFormat=""Delimited"" FirstRowHasNames=""0"" FieldDelimiter=""|"" CodePage=""437"" AppendToTable=" & Chr(34) & AccessTempTableName & Chr(34) & " >" & vbCrLf
xml = xml & " <DateFormat DateOrder=""MDY"" DateDelimiter=""/"" TimeDelimiter="":"" FourYearDates=""true"" DatesLeadingZeros=""false"" />" & vbCrLf
xml = xml & " <NumberFormat DecimalSymbol=""."" />" & vbCrLf
xml = xml & " <Columns PrimaryKey=""{none}"">" & vbCrLf
For ColNumber = LBound(ColNames) To UBound(ColNames)
xml = xml & " <Column Name=""Col" & ColNumber + 1 & """ FieldName=""" & ColNames(ColNumber) & """ Indexed=""NO"" SkipColumn=""false"" DataType=""Text"" Width=""12"" />" & vbCrLf
Next ColNumber
xml = xml & " </Columns>" & vbCrLf
xml = xml & " </ImportText>" & vbCrLf
xml = xml & "</ImportExportSpecification>"
'''debug.print xml
CurrentProject.ImportExportSpecifications.Add name_of_spec, Trim(xml)
DoCmd.RunSavedImportExport name_of_spec
DoEvents
imexImport_LoaderFile = DMax("[MEGA_EFFDT]", "[" & AccessTempTableName & "]") 'Format(DMax("[EXPORTDATE]", "[" & AccessTempTableName & "]"), "mm/dd/yyyy") 'CDate(Int(DMax("[EXPORTDATE]", "[" & AccessTempTableName & "]")))
''debug.print imexImport_LoaderFile
If DCount("EMPLID", AccessTempTableName) = 0 Then
If MsgBox(FileName & " import failed. Would you like to retry?", vbYesNo) = vbYes Then
GoTo retry_import
End If
End If
CleanExitTask:
Set imexObjs = Nothing
End Function