A voir également:
- Insérer ligne dans un fichier EXCEL à partir d'ACCESS
- Fichier rar - Guide
- Aller à la ligne excel - Guide
- Insérer liste déroulante excel - Guide
- Fichier host - Guide
- Fichier iso - Guide
1 réponse
J'ai une commande qui importe les données d'un fichier EXCEL vers ma table Access.
Mon code :
Private Sub Importer_Click()
Dim varFeuilles As Variant
' Liste des feuilles Excel à importer
varFeuilles = Array("Sheet1")
' Importation proprement dite
ImportExcel pathfichier, varFeuilles, True, "T_INSCRIPTION"
End Sub
Sub ImportExcel( _
ByVal strChemin As String, _
ByVal varFeuilles As Variant, _
ByVal blnNoms As Boolean, _
ByVal strTable As String _
)
' Déclaration des variables
Dim strFeuille As Variant
' Est-ce que le classeur Excel existe ?
If Dir(strChemin) = "" Then
MsgBox "Le classeur ['" & strChemin & "] est introuvable.", vbExclamation
Exit Sub
End If
' Vidage de la table si nécessaire
On Error GoTo ImportExcelErr
If MsgBox("Souhaitez-vous vider la table [" & strTable & "] avant l'importation ?", _
vbQuestion + vbYesNo) = vbYes Then
CurrentDb.Execute "DELETE * FROM [" & strTable & "];"
End If
' Procédure d'importation
'For Each strFeuille In varFeuilles
'DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
' strTable, strChemin, blnNoms, strFeuille & "!"
'Next
DoCmd.SetWarnings False
DoCmd.TransferSpreadsheet acImport, 8, "T_INSCRIPTION", strChemin, True, "A1:C2000"
DoCmd.SetWarnings True
MsgBox "Opération terminée !", vbInformation
Exit Sub
ImportExcelErr:
MsgBox "Erreur d'importation : " & Err.Description, vbExclamation
Exit Sub
End Sub
Avec ce code, mon entête de colonne doit correspondre avec le nom de mon champ dans ma table T_INSCRIPTION. J'aimerais enregistrer A1:A2000 vers champ fiche, B1:2000 vers nom et C1:C2000 vers prenom.
Mon code :
Private Sub Importer_Click()
Dim varFeuilles As Variant
' Liste des feuilles Excel à importer
varFeuilles = Array("Sheet1")
' Importation proprement dite
ImportExcel pathfichier, varFeuilles, True, "T_INSCRIPTION"
End Sub
Sub ImportExcel( _
ByVal strChemin As String, _
ByVal varFeuilles As Variant, _
ByVal blnNoms As Boolean, _
ByVal strTable As String _
)
' Déclaration des variables
Dim strFeuille As Variant
' Est-ce que le classeur Excel existe ?
If Dir(strChemin) = "" Then
MsgBox "Le classeur ['" & strChemin & "] est introuvable.", vbExclamation
Exit Sub
End If
' Vidage de la table si nécessaire
On Error GoTo ImportExcelErr
If MsgBox("Souhaitez-vous vider la table [" & strTable & "] avant l'importation ?", _
vbQuestion + vbYesNo) = vbYes Then
CurrentDb.Execute "DELETE * FROM [" & strTable & "];"
End If
' Procédure d'importation
'For Each strFeuille In varFeuilles
'DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
' strTable, strChemin, blnNoms, strFeuille & "!"
'Next
DoCmd.SetWarnings False
DoCmd.TransferSpreadsheet acImport, 8, "T_INSCRIPTION", strChemin, True, "A1:C2000"
DoCmd.SetWarnings True
MsgBox "Opération terminée !", vbInformation
Exit Sub
ImportExcelErr:
MsgBox "Erreur d'importation : " & Err.Description, vbExclamation
Exit Sub
End Sub
Avec ce code, mon entête de colonne doit correspondre avec le nom de mon champ dans ma table T_INSCRIPTION. J'aimerais enregistrer A1:A2000 vers champ fiche, B1:2000 vers nom et C1:C2000 vers prenom.