[VBA] - La table externe n'est pas dans le format attendu -ADODB.Connection

Bonsoir,

Habituellement, lorsque j'importe les données d'un fichier au format .xlsx cela fonctionne. Mais en l'occurrence, j'ai une erreur qui dit que la table externe n'est pas dans le format attendu.

Est-ce que vous sauriez d'où ça peut bien provenir ?

Je joins à ce post, le document d'import et une feuille à importer.

Je vous remercie de votre attention,

Bonne fin de journée !

30test-carto.xlsx (14.81 Ko)
27imp-hab.xlsm (19.84 Ko)

Bonjour,

Essayez comme ceci

Private Sub CommandButton1_Click()
  Dim source$, fichier$, fe$, lastfich$, fich, wbks As Workbook, wbkc As Workbook, ddt As Date ' sFeuille$
  Dim sPath As String, sFic As String
  Dim oSource As ADODB.Connection, oRS As ADODB.Recordset, oCommand As ADODB.Command
  chk2 = 0
  sPath = ThisWorkbook.Path & "\"
  If ActiveSheet.Name = "HAB" Then
    Set wbkc = ThisWorkbook: fe = ActiveSheet.Name
    sFic = Dir(sPath & "*.xlsx")
    If sFic = "" Then
      sFic = Application.GetOpenFilename("Fichiers xlsx,*.xlsx")
      sPath = Left(sFic, InStrRev(sFic, "\"))
      sFic = Mid(sFic, Len(sPath) + 1)
    End If
  End If

  If sFic = "" Then MsgBox "Aucun fichier sélectionné." & vbCrLf, vbCritical: chk2 = 1: Exit Sub
  If Dir(sPath & sFic) = "" Then MsgBox "Fichier absent." & vbCrLf & fich, vbCritical: chk2 = 1: Exit Sub

  Set oSource = New ADODB.Connection
  With oSource
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
            & sPath & "\" & sFic & ";Extended Properties=""Excel 12.0;HDR=YES;"""
    .Open
  End With
  Set oCommand = New ADODB.Command
  With oCommand
    .ActiveConnection = oSource
    If ActiveSheet.Name = "BDD_SAISIE_FLORE" Then .CommandText = "SELECT * FROM [" & "BDD_SAISIE_FLORE$" & "]" 'where 1=0"
    If ActiveSheet.Name = "HABREF" Then .CommandText = "SELECT * FROM [" & "BDD_HABITATS$" & "]" 'where 1=0"
    If ActiveSheet.Name = "BDC_STATUTS" Then .CommandText = "SELECT * FROM [" & "MAJ_BDC_STATUTS$" & "]" 'where 1=0"
    If ActiveSheet.Name = "TAXREF" Then .CommandText = "SELECT * FROM [" & "Maj_TAXREF$" & "]" 'where 1=0"
    If ActiveSheet.Name = "BASEFLOR" Then .CommandText = "SELECT * FROM [" & "Maj_BASEFLOR$" & "]" 'where 1=0"
    If ActiveSheet.Name = "TABLE DES CORRESPONDANCES" Then .CommandText = "SELECT * FROM [" & "HABREF_MAJ$" & "]" 'where 1=0"
    'If ActiveSheet.Name = "HABREF" Then 'fe =
  End With

  Set oRS = New ADODB.Recordset
  oRS.Open oCommand, , adOpenKeyset, adLockOptimistic
  ActiveSheet.Range("A1").CopyFromRecordset oRS
  oRS.Close: oSource.Close

  Set oSource = Nothing: Set oRS = Nothing: Set oCommand = Nothing

End Sub

@+

Bonsoir,

Merci pour votre réponse. En l'occurrence, lorsque le code arrive à .open ; ici :

  With oSource
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
            & sPath & "\" & sFic & ";Extended Properties=""Excel 12.0;HDR=YES;"""
    .Open
  End With

L'erreur se reproduit.

Avez-vous le même souci chez vous ?

Par la suite, à cette partie du code (voir code ci-après) , il devrait y avoir une erreur, ici j'ai indiqué des "????" pour le nom de la feuille, il me suffira de rechercher ce nom avant d'ouvrir la feuille. Mais tant que la partie amont de la macro ne fonctionne pas, je suis bloqué :/

  Set oCommand = New ADODB.Command
  With oCommand
    .ActiveConnection = oSource
    If ActiveSheet.Name = "HABREF" Then .CommandText = "SELECT * FROM [" & "????$" & "]" 'where 1=0"

Bonne soirée !

Re,

Donc le problème vient du fichier source lui même

Je l'ai ouvert et fait un enregistrer sous en écrasant l'existant

@+

Re,

En effet, j'ai enregistré le fichier en écrasant le fichier existant, tout fonctionne, que ce soit avec votre macro ou celle que j'utilisais initialement.

Ça m'embête... ces fichiers sont obtenus après export depuis un logiciel de cartographie. J'aimerais éviter d'avoir à écraser manuellement le fichier systématiquement...

Je vais chercher un peu.

Merci de vous être penché sur mon problème Et d'avoir trouvé l'origine !

Bonne soirée !

Rechercher des sujets similaires à "vba table externe pas format attendu adodb connection"