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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| Private Sub Bascule0_Click()
Dim oDb As DAO.Database
Dim oNouvelleTable As DAO.TableDef
Dim oChamp As DAO.Field
Dim oIndex As DAO.Index
Dim Clients As TableDef
'supprime une table existante
'Si tbl=nothing alors tbl est la cause de l'erreur
If Clients = Nothing Then
Set oDb = CurrentDb()
'Crée la nouvelle table
Set oNouvelleTable = oDb.CreateTableDef("Clients")
'Crée le champ IDClient
Set oChamp = oNouvelleTable.CreateField("IDClient", dbLong)
'Définit le champ en numero_auto
oChamp.Attributes = dbAutoIncrField
'Ajoute le champ à la table
oNouvelleTable.Fields.Append oChamp
'Crée le champ nomClient et l'ajoute
oNouvelleTable.Fields.Append oNouvelleTable.CreateField("CodeClient", _
dbText, 15)
'Crée le champ PrenomClient et l'ajoute
oNouvelleTable.Fields.Append oNouvelleTable.CreateField("Agence", _
dbText, 25)
'définit la clé primaire sur l'IDClient
Set oIndex = oNouvelleTable.CreateIndex("PK_IDClient")
oIndex.Primary = True
oIndex.Fields.Append oIndex.CreateField("IdClient")
'Ajoute l'index à la table
oNouvelleTable.Indexes.Append oIndex
'Ajoute la table à la base de données
oDb.TableDefs.Append oNouvelleTable
'Libère les variables
oDb.Close
Set oIndex = Nothing
Set oChamp = Nothing
Set oNouvelleTable = Nothing
Set oDb = Nothing
CurrentDb.TableDefs.Refresh
Dim oApp As Excel.Application
Dim oWkb As Excel.Workbook
Dim oWSht As Excel.Worksheet
Set oApp = CreateObject("excel.application")
Set oWkb = oApp.Workbooks.Open("C:\Loyers_Développez_2007_3.xlsm")
Set oWSht = oWkb.Worksheets("Cible")
'premier ligne ou on commence l'import
I = 1
'pour éviter les messages lors de l'ajout des enregistrements
DoCmd.SetWarnings False
'tant que la cellule n'est pas vide
While oWSht.Range("A" & I).Value <> ""
cSQL = "insert into [Clients] ( [CodeClient], [Agence]) values (" & Chr(34) & oWSht.Cells(I, 2) & Chr(34) & ", " & Chr(34) & oWSht.Cells(I, 3) & Chr(34) & ")"
',[Champ3], [Champ4],[Champ5], [Champ6],[Champ7], [Champ8],[Champ9],[Champ10],[Champ11],[Champ12]
'exécute la requète
DoCmd.RunSQL (cSQL)
I = I + 1
Wend
DoCmd.SetWarnings True
Set oWkb = Nothing
Else
DoCmd.DeleteObject acTable, "Clients"
Set oDb = CurrentDb()
'Crée la nouvelle table
Set oNouvelleTable = oDb.CreateTableDef("TblClients")
'Crée le champ IDClient
Set oChamp = oNouvelleTable.CreateField("IDClient", dbLong)
'Définit le champ en numero_auto
oChamp.Attributes = dbAutoIncrField
'Ajoute le champ à la table
oNouvelleTable.Fields.Append oChamp
'Crée le champ nomClient et l'ajoute
oNouvelleTable.Fields.Append oNouvelleTable.CreateField("CodeClient", _
dbText, 15)
'Crée le champ PrenomClient et l'ajoute
oNouvelleTable.Fields.Append oNouvelleTable.CreateField("Agence", _
dbText, 25)
'définit la clé primaire sur l'IDClient
Set oIndex = oNouvelleTable.CreateIndex("PK_IDClient")
oIndex.Primary = True
oIndex.Fields.Append oIndex.CreateField("IdClient")
'Ajoute l'index à la table
oNouvelleTable.Indexes.Append oIndex
'Ajoute la table à la base de données
oDb.TableDefs.Append oNouvelleTable
'Libère les variables
oDb.Close
Set oIndex = Nothing
Set oChamp = Nothing
Set oNouvelleTable = Nothing
Set oDb = Nothing
''Dim MaBD As Database
'Dim MonSQL As String
'DoCmd.Requery
' detruit la table TransfertCegecom
''DoCmd.DeleteObject acTable, "Clients"
'Set MaBD = CurrentDb()
'MonSQL = " SELECT ,champ4 as Num_Appel, [Duration HH:MM:SS] as Durée,[Amount EUR] as MontantCegecom INTO "Transfert" FROM TABLE3;"
'MonSQL = MonSQL &
'MaBD.Execute "SELECT TABLE3.* INTO " _
' & "[TransfertdeCegecom] FROM TABLE3;"
'DoCmd.Requery
'MaBD.Execute MonSQL
'MsgBox "La création de la nouvelle table s'est déroulée avec succès. " & MaBD.RecordsAffected & " enregistrements", vbInformation, "Opération réussie"
'la syntaxe suivante rafrîchit la base de donnée
CurrentDb.TableDefs.Refresh
'Dim oApp As Excel.Application ???????
'Dim oWkb As Excel.Workbook ???????
'Dim oWSht As Excel.Worksheet ??????
Set oApp = CreateObject("excel.application")
Set oWkb = oApp.Workbooks.Open("C:\Loyers_Développez_2007_3.xlsm")
Set oWSht = oWkb.Worksheets("Cible")
'premier ligne ou on commence l'import
I = 1
'pour éviter les messages lors de l'ajout des enregistrements
DoCmd.SetWarnings False
'tant que la cellule n'est pas vide
While oWSht.Range("A" & I).Value <> ""
cSQL = "insert into [TblClients] ( [CodeClient], [Agence]) values (" & Chr(34) & oWSht.Cells(I, 2) & Chr(34) & ", " & Chr(34) & oWSht.Cells(I, 3) & Chr(34) & ")"
',[Champ3], [Champ4],[Champ5], [Champ6],[Champ7], [Champ8],[Champ9],[Champ10],[Champ11],[Champ12]
'exécute la requète
DoCmd.RunSQL (cSQL)
I = I + 1
Wend
DoCmd.SetWarnings True
Set oWkb = Nothing
End If
End Sub |
Partager