user3860954
user3860954

Reputation: 97

DoCmd.TransferSpreadsheet acImport error "External table is not in the expected format"

Hi I am getting this error

"External table is not in the expected format"

...when i try to transfer excel data to a access Table. I have data in text file that has a description field. In order to avoid truncation at 250 character limit, I convert the text file to EXCEL and then transfer excel to access database. But I get this error.... I have both excel 2010 and 2016 on my machine installed. thanks all in advance.

Sub ImportPPDE_v2()

Dim fDialog As Office.FileDialog
Dim strNewPath As String

On Error GoTo GameOver

Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

'Set up the fDialog variable
'Set the Title, add a filter for text files, and set the initial filepath we want to look at, we are defaulting to the Clarity Extract folder
fDialog.Filters.Clear
fDialog.Title = "Select Latest Project Portfolio Data Extract File"
fDialog.Filters.Add "*.txt", "*.txt"
fDialog.InitialFileName = "G:\Clarity EPPM Extracts\"
fDialog.AllowMultiSelect = False
fDialog.Show

'turn off warnings, we don't need to see this
DoCmd.SetWarnings False

'Check to make sure a file has been selected, and if so that the Project Portfolio Data Extract file has been selected
If fDialog.SelectedItems.Count = 0 Then
    MsgBox "No File has been selected. Load actions have been cancelled.", , "No File Selected"
    GoTo GameOver
ElseIf InStr(1, fDialog.SelectedItems(1), "Project Portfolio Data extract_") = 0 Then
    MsgBox "The file selected appears to be incorrect. It should be the   Data Extract file.  Load actions have been cancelled.", , "ERROR OCCURRED IN DATA LOAD"
    GoTo GameOver
End If

'First delete everything currently in the table
DoCmd.RunSQL "Delete * from tbl_Project_Portfolio_Data_Load"


'DoCmd.TransferText , "Spec_PPDE", "tbl_Project_Portfolio_Data_Load", fDialog.SelectedItems(1)

'First isolate the file name from the selected path, then change the file extension to .xls
strNewPath = Right(fDialog.SelectedItems(1), Len(fDialog.SelectedItems(1)) - InStrRev(fDialog.SelectedItems(1), "\"))
strNewPath = "C:\" & Left(strNewPath, Len(strNewPath) - 4) & ".xlsx"

'Copy the Portfolio data extract file to the user's C: drive as a .xls file
Call SaveAsFile(CStr(fDialog.SelectedItems(1)), strNewPath)

'Now import the new data as selected above
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "tbl_Project_Portfolio_Data_Load", strNewPath, True

'Now update the Load_Date to be today
DoCmd.RunSQL "Update tbl_Project_Portfolio_Data_Load set [Load Date] = #" & Date & "# Where [Load Date] IS NULL"

'Let the user know the process finished successfully
MsgBox "Project Portfolio Data Extract Data has been uploaded", , "Victory!"

GameOver:

'Turn our warnings back on
DoCmd.SetWarnings True

'Set this back to nothing
Set fDialog = Nothing

'Check if an error occurred that would prevent the expected data from being loaded
If Err.Number <> 0 Then
    MsgBox Err.Description
End If

End Sub

I get this erorr when my code try to transfer excel sheet into the empty table

here is save as code

Sub SaveAsFile(currpath As String, newpath As String)

Dim wb As Workbook, strWB As String
Dim NewWB As String

'The purpose of this module is to copy a file from a given filepath to a user's C: Drive.
'It is also converting the file from .txt to a .xls format
'This is originally intended to be used with the Project Portfolio Data Extract load

'Delete any existing workbook that is there now with the same name
On Error Resume Next
Kill newpath
On Error GoTo GameOver

'Open the current file
Set wb = Workbooks.Open(currpath)

'MsgBox wb.FileFormat
'Application.DisplayAlerts = False

'Save it as a .xls file
wb.SaveAs newpath, xlNormal

'Application.DisplayAlerts = True
'MsgBox wb.FileFormat

'Close the workbook
wb.Close False

GameOver:

Set wb = Nothing

End Sub

Upvotes: 2

Views: 1374

Answers (1)

June7
June7

Reputation: 21370

If you really cannot link to the text file without truncation (I could) and since simple Open doesn't work, do an import of the text file to Excel then SaveAs an Excel workbook. I used Excel macro recorder to generate some code and adapted to an Access procedure.

Sub TextToExcel1(currpath As String, newpath As String)
Dim xlx As Excel.Application, xlw As Excel.Workbook, xls As Excel.Worksheet
Dim blnEXCEL As Boolean
If Dir(newpath) <> "" Then Kill newPath
blnEXCEL = False
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
    Set xlx = CreateObject("Excel.Application")
    blnEXCEL = True
End If
Err.Clear
xlx.Visible = False
Set xlw = xlx.Workbooks.Add
Set xls = xlw.Worksheets("Sheet1")
With xls.QueryTables.Add("TEXT;" & currPath, Range("$A$1"))
    .Name = "Test"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = True
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = True
    .TextFileColumnDataTypes = Array(1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
xlw.SaveAs newPath
Set xls = Nothing
xlw.Close False
Set xlw = Nothing
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub

And then this version:

Sub TextToExcel2(currpath As String, newpath As String)
Dim xlx As Excel.Application
Dim blnEXCEL As Boolean
If Dir(newpath) <> "" Then Kill newPath
blnEXCEL = False
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
    Set xlx = CreateObject("Excel.Application")
    blnEXCEL = True
End If
Err.Clear
xlx.Visible = False
xlx.Workbooks.OpenText filename:=currPath, Origin:=437, StartRow:=1, _
    DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, 
    Comma:=False, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
    Array(2, 1)), TrailingMinusNumbers:=True
xlx.ActiveWorkbook.SaveAs filename:=newPath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub

Or even:

Sub TextToExcel3(currpath As String, newpath As String)
Dim xlx As Excel.Application
Dim blnEXCEL As Boolean
If Dir(newpath) <> "" Then Kill newPath
blnEXCEL = False
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
    Set xlx = CreateObject("Excel.Application")
    blnEXCEL = True
End If
Err.Clear
xlx.Workbooks.Open (currpath)
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
    Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
xlx.Visible = False
xlx.ActiveWorkbook.SaveAs filename:=newPath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub

I am having issue getting version 1 to replicate consistently. Versions 2 and 3 both seem reliable.

Upvotes: 1

Related Questions