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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
| Public Const BDD = "Reclamation_Incident.accdb"
Sub PasteData()
'Dim Cat As Object
Dim Cat As Object
Set Cat = CreateObject("ADOX.Catalog")
'Dim Cnx As Object
Dim Cnx As Object
Set Cnx = CreateObject("ADODB.Connection")
'Dim rsT As Object
Dim rsT As Object
Set rsT = CreateObject("ADODB.Recordset")
Dim ndf As String
Dim stSQL As String
'Dim wbPath As String
Dim Derlig As Integer
Dim SDerCol As String
Dim Let_Col As String
Dim oTables As String
Dim L As Long, C As Byte
Dim TabPlage As Variant
Dim DerCol As Byte
Dim Tbl As Object
Dim Requete As String
'Set Cat = CreateObject("ADOX.Catalog")
'Cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf
On Error GoTo ErrHandl
ndf = ThisWorkbook.Path & "\" & BDD
'wbPath = ThisWorkbook.FullName
Set Ws = ThisWorkbook.Sheets("Donnees")
'Plage Collaborateurs
With Ws
DerCol = .Range("A1").End(xlToRight).Column
Derlig = .Range("A65536").End(xlUp).Row
SDerCol = .Range("A1").End(xlToRight).Address: Let_Col = Mid(SDerCol, 2, InStr(2, SDerCol, "$") - 2)
TabPlage = .Range("A1:" & Let_Col & Derlig).Value
End With
oTables = "Collaborateurs"
'oTables =replace(oTables,""","") 'Esc("Collaborateurs")
'[Master Grid_Export$A1:M7]
'stSQL = "INSERT INTO oTables * FROM" & "[" & Esc(Ws.Name) & "$A1:" & Esc(Let_Col) & Derlig & "]" & "IN '" & _
Esc(wbPath) & "' 'Excel 12.0;HDR=No;'"
'stSQL = "INSERT INTO Collaborateurs ([Id], [Site], [Equipe], [Nom], [User], [Statut], [Email], [Civilite])" & _
"SELECT * FROM [Excel 12.0;HDR=No;DATABASE=" & wbPath & "]." & "[" & Ws.Name & "$]"
'Set Cat = CreateObject("ADOX.Catalog")
'Set Cnx = CreateObject("ADODB.Connection")
With Cnx
.Provider = "Microsoft.JET.OLEDB.4.0"
'.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf '"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & ndf
.Open ndf
End With
With Cat
.ActiveConnection = Cnx
'On Error Resume Next
' .Tables.Delete COLLABORATEURS
'On Error GoTo 0
End With
Set Tbl = CreateObject("ADOX.Table")
Tbl.Name = oTables
'Set rsT = CreateObject("ADODB.Recordset")
'Cnx.Execute stSQL
'Cat.Tables.Append Tbl
Const adLockOptimistic = 3
With rsT
.ActiveConnection = Cnx
'.Open oTables, adOpenKeyset, adLockOptimistic, adCmdTableDirect ', LockType:=adLockOptimistic
.Open oTables, LockType:=adLockOptimistic
End With
'supprime l'enregistrement si le champ 'Ville' est vide
Requete = "DELETE * FROM " & oTables '& " WHERE [Ville]IS NULL "
Cnx.Execute Requete
'Boucle sur les lignes et colonnes de la plage pour remplir table
For L = 2 To UBound(TabPlage)
With rsT
.AddNew
For C = 1 To DerCol
.Fields(C - 1).Value = TabPlage(L, C)
Next
.Update
End With
Next L
'*******************************************************************
Set Cat = Nothing
Set Cnx = Nothing
Set rsT = Nothing
Set Ws = Nothing
Exit Sub
ErrHandl:
MsgBox Err.Number & vbCr & Err.Description, vbCritical
rep = Err.Number & vbCr & Err.Description
Ws.Range("J11") = rep
'If Not rsT Is Nothing Then
' If rsT.State = adStateOpen Then rsT.Close
'End If
Set rsT = Nothing
If Not Cnx Is Nothing Then
If Cnx.State = adStateOpen Then Cnx.Close
End If
Set Cnx = Nothing
Set Cat = Nothing
'Set Cnx = Nothing
'Set rsT = Nothing
Set Ws = Nothing
End Sub
Sub PasteData_LateBiding()
Dim Cat As Object
'Dim Cat As New ADOX.Catalog
Dim Cnx As Object
'Dim Cnx As New ADODB.Connection
Dim rsT As Object
'Dim rsT As New ADODB.Recordset
Dim ndf As String
Dim stSQL As String
'Dim wbPath As String
Dim Derlig As Integer
Dim SDerCol As String
Dim Let_Col As String
Dim oTables As String
Dim L As Long, C As Byte
Dim TabPlage As Variant
Dim DerCol As Byte
Dim Tbl As Object
Dim Requete As String
Dim champ As Object
'Set Cat = CreateObject("ADOX.Catalog")
'Cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf
On Error GoTo ErrHandl
ndf = ThisWorkbook.Path & "\" & BDD
'wbPath = ThisWorkbook.FullName
Set Ws = ThisWorkbook.Sheets("Donnees")
'Plage Collaborateurs
With Ws
DerCol = .Range("A1").End(xlToRight).Column
Derlig = .Range("A65536").End(xlUp).Row
SDerCol = .Range("A1").End(xlToRight).Address: Let_Col = Mid(SDerCol, 2, InStr(2, SDerCol, "$") - 2)
TabPlage = .Range("A1:" & Let_Col & Derlig).Value
End With
oTables = "Collaborateurs"
'Set Cat = CreateObject("ADOX.Catalog")
Set Cnx = CreateObject("ADODB.Connection")
With Cnx
'.Provider = "Microsoft.JET.OLEDB.4.0"
.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ndf & ";Persist Security Info=False;" '"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & ndf
'.Open ndf
End With
'With Cat
' .ActiveConnection = Cnx
''On Error Resume Next
'' .Tables.Delete COLLABORATEURS
''On Error GoTo 0
'End With
'Set Tbl = CreateObject("ADOX.Table")
'Tbl.Name = oTables
Set rsT = CreateObject("ADODB.Recordset")
'Cnx.Execute stSQL
'Cat.Tables.Append Tbl
With rsT
.ActiveConnection = Cnx
'.Open oTables, adOpenKeyset, adLockOptimistic, adCmdTableDirect ', LockType:=adLockOptimistic
'.Open "Collaborateurs", Cnx, adOpenKeyset ', LockType:=adLockOptimistic
.Open "Collaborateurs", Cnx ', adOpenKeyset, adLockOptimistic, adCmdTable
End With
'supprime l'enregistrement si le champ 'Ville' est vide
Requete = "DELETE * FROM " & "Collaborateurs" '& " WHERE [Ville]IS NULL "
Cnx.Execute Requete
'Boucle sur les lignes et colonnes de la plage pour remplir table
'j = 0
For L = 2 To UBound(TabPlage)
'' Parcourir chaque champ de l'enregistrement courant
' For Each champ In rsT.Fields
' champ = Ws.Cells(L, 1 + j)
' j = j + 1
' Next champ
With rsT
'.AddNew
For C = 1 To DerCol
.Fields(C - 1).Value = TabPlage(L, C)
Next
.AddNew
.Update
End With
Next L
'*******************************************************************
Set Cat = Nothing
Set Cnx = Nothing
Set rsT = Nothing
Set Ws = Nothing
Exit Sub
ErrHandl:
MsgBox Err.Number & vbCr & Err.Description, vbCritical
rep = Err.Number & vbCr & Err.Description
Ws.Range("J11") = rep
'If Not rsT Is Nothing Then
' If rsT.State = adStateOpen Then rsT.Close
'End If
Set rsT = Nothing
If Not Cnx Is Nothing Then
If Cnx.State = adStateOpen Then Cnx.Close
End If
Set Cnx = Nothing
Set Cat = Nothing
'Set Cnx = Nothing
'Set rsT = Nothing
Set Ws = Nothing
End Sub |
Partager