Forum

External table is n...
 
Notifications
Clear all

External table is not in the expected format

4 Posts
3 Users
0 Reactions
133 Views
(@questvba)
Posts: 125
Estimable Member
Topic starter
 

Hi,

I am using the following code to retrieve data from a closed file (.xlsx format). Everything works fine for data retrieval and viewing.

Public Function PickData(oConn As Object, oRec As Object, sSQL As String) As Integer
    'connect to the DB, apply Query, (paste result in  sheet Search), return #result
    'connection DB----------------------------------------------------------------------------------
    oConn.Open CONNSTR_DB
 
    On Error Resume Next
    If oConn.State <> adStateOpen Then
        MsgBox "Error connecting to Database!"
        Exit Function
    End If
    On Error GoTo 0
 
    oRec.Open sSQL, oConn, adOpenStatic, adLockOptimistic, adCmdText
    PickData = oRec.RecordCount '#result
    'ClearSearch
    If PickData <> 0 Then shRecup.Range("M2").CopyFromRecordset oRec
    oRec.Close
    oConn.Close
    'deconnection-----------------------------------------------------------------------------------
End Function

 

----

 
CONNSTR_DB = "PROVIDER=MICROSOFT.ACE.OLEDB.12.0;" & _
                       "DATA SOURCE=" & DataPath & DataFile & ";" & _
                       "Extended Properties=""Excel 12.0;HDR=YES;"";"

 

However, when I insert a new line (when I do an UPDATE, everything works correctly), I get an error message 'External table is not in the expected format' and the code stops at the line 'oConn.Open CONNSTR_DB'.

4VQ2I.png

 

I've checked the file and everything seems correct in terms of the data, same format etc.

Additional information:
If I pick up the code where it left off and continue via F8, everything works perfectly again. If I then go back to this function, no problem.

If you have any clues, I'd love to hear from you. Thanks in advance.

 
Posted : 07/09/2023 8:30 am
(@keebellah)
Posts: 373
Reputable Member
 

Without the data to help i would suggest you move the On Error Goto 0 line to after the If PickData ... and see what happens

 
Posted : 08/09/2023 2:05 am
(@debaser)
Posts: 836
Member Moderator
 

Technically, for an xlsx file your extended properties should have Excel 12.0 Xml rather than just Excel 12.0, but that wouldn't explain this issue. It seems odd that you say it occurs only for an insert given that the error occurs before you actually execute any SQL, so it shouldn't really make any difference what type of SQL command it is.

 
Posted : 08/09/2023 7:18 am
(@questvba)
Posts: 125
Estimable Member
Topic starter
 

Hi,

It seems that the db is falling asleep.

So I created a little 'WAKE_UP' macro and everything seems to be working.

Thanks for your comments.

BR,

Lionel

 
Posted : 09/09/2023 5:43 am
Share: