Bonjour,
C'est ma première application dans laquelle je veux importer des données d'Excel.
J'ai basé mon code sur la discussion de NITROMARD avec TAZ.
J'obtiens une erreur sur la ligne 46 du code avec le message d'Access "Run-time error '-2147217900 (80040e14): Syntax error in FROM clause.
Mon fichier Excel est le suivant "C:\Kalymar\Importation\Inscriptions.xls" et le nom du classeur ou feuille (le nom situé en bas des données) est toujours "Inscriptions". Les titres de colonnes peuvent différer d'une fois à l'autre mais les informations que je veux importer sont toujours dans les 23 premières colonnes.
J'apprécierais beaucoup recevoir la réponse à mon problème.
Merci beaucoup.
Mon code est le suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 Private Sub Importer_Click() On Error GoTo Handler Dim connExcel As New ADODB.Connection Dim connAccess As ADODB.Connection Dim tabImportation As ADODB.Recordset Dim excInscriptions As New ADODB.Recordset Dim j As Integer Dim NombreChamps As Long Dim Fichier As String Fichier = "C:\Kalymar\Importation\Inscriptions.xls" NombreChamps = 22 ' = Nombre de champs à importer (23 premiers champs) moins 1 ' ' Connection à la base Access ' Set connAccess = CurrentProject.Connection ' ' Connection à la table tabImportation ' Set tabImportation = New ADODB.Recordset tabImportation.Open "SELECT * FROM Importation", connAccess, adOpenKeyset, adLockOptimistic ' ' Supprimer le contenu de la table tabImportation si elle contient des données ' If tabImportation.BOF = False Then Do Until tabImportation.EOF tabImportation.Delete tabImportation.MoveNext Loop End If ' ' Connection au fichier Excel excInscriptions ' connExcel.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Fichier & ";" & _ "Extended Properties=""Excel 8.0;""" ' ' Connection à la feuille nommée "Inscriptions" ' excInscriptions.Open "SELECT * FROM [" & Inscriptions & "] ", connExcel, adOpenStatic ' ' Importer les données de la feuille Excel dans la table ' Do While Not (excInscriptions.EOF) tabImportation.AddNew For j = 0 To NombreChamps tabImportation.Fields(j) = excInscriptions.Fields(j).Value Next j tabImportation.Update excInscriptions.MoveNext Loop MsgBox "L'importation est terminée." DoCmd.Close acForm, "Importation3" Fin: On Error Resume Next excInscriptions.Close Set excInscriptions = Nothing tabImportation.Close Set tabImportation = Nothing connAccess.Close Set connAccess = Nothing connExcel.Close Set connExcel = Nothing Exit Sub Handler: MessageErreur Err.Number, Err.Description, "Importation3 - Importer_Click" Resume Fin End Sub
Partager