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
|
Private Sub cmd_Import_Click()
'------------------------------- Déclaration des variables --------------------------------------
Dim fso_FileDiag As Office.FileDialog
Dim fso_SelectedFile As Office.FileDialogSelectedItems
Dim db As Database
Dim str_Sql As String
Dim App_Xls As Excel.Application
Dim wb_Import As Excel.Workbook
Dim str_WbPath As String
Dim tbl_Plage
Dim oCell
Dim fld_TempTable As String
'------------------------------- Initialisation des variables --------------------------------------
fld_TempTable = ""
Set db = Application.CurrentDb
Set fso_FileDiag = Application.FileDialog(msoFileDialogOpen)
'Réglage pour l'ouverture de la boite de dial pour sélection d'un classeur
With fso_FileDiag
.InitialFileName = Application.CurrentProject.Path
.Filters.Clear
.Filters.Add "Excel", "*.xlsx"
.Filters.Add "Excel", "*.xlsm"
.AllowMultiSelect = False
.Show
End With
Set fso_SelectedFile = fso_FileDiag.SelectedItems
'On vérifie qu'on a bien sélectionné un classeur
If fso_SelectedFile.Count = 0 Then
MsgBox "Vous n'avez pas sélectionné de classeur Excel à importer." & vbLf & "Veuillez recommencer et bien sélectionner un classeur à importer.", _
vbOKOnly + vbCritical, "Erreur :"
End If
'Initialisation des variables pour l'application Excel et le classeur qu'on aura sélectionné
Set App_Xls = CreateObject("Excel.Application")
Set wb_Import = App_Xls.Workbooks.Open(fso_SelectedFile.Item(1))
App_Xls.Visible = True
Set tbl_Plage = wb_Import.Sheets("IMPORT").Range("A1", Range("A1").End(xlToRight))
'On récupère le nom des champs pour la création de la table
For Each oCell In tbl_Plage
fld_TempTable = fld_TempTable & oCell.Value & " Char(50), "
Next oCell
'On retravaille la variable avec tous les champs pour supprimer ", " en trop
fld_TempTable = Mid(fld_TempTable, 1, Len(fld_TempTable) - 2)
'On créé la requête sql
str_Sql = "CREATE TABLE tbl_TampTable (" & fld_TempTable & ")"
'On exe la requête
db.Execute str_Sql
'DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tbl_TampTable", fso_SelectedFile.Item(1), True
'DoCmd.RunSQL "DROP TABLE tbl_TampTable"
wb_Import.Close False
App_Xls.Quit
Set tbl_Plage = Nothing
Set wb_Import = Nothing
Set App_Xls = Nothing
Set fso_SelectedFile = Nothing
Set fso_FileDiag = Nothing
End Sub |
Partager