Bonjour
j'ai un fichier excel qui contient ce code vba qui se connecte une ListView a une base access et qui permet de faire une recherche, un ajout, une modification, une suppression de données

je souhaite avoir la possibilité de faire la même chose avec une connexion a une base csv pour utiliser ce code sur les machines qui ne disposent pas de access .

merci d'avance

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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
 #If VBA7 Then
Private Declare PtrSafe Function FindWindowA& Lib "user32" (ByVal lpClassName$, ByVal lpWindowName$)
Private Declare PtrSafe Function EnableWindow& Lib "user32" (ByVal hWnd&, ByVal bEnable&)
Private Declare PtrSafe Function GetWindowLongA& Lib "user32" (ByVal hWnd&, ByVal nIndex&)
Private Declare PtrSafe Function SetWindowLongA& Lib "user32" (ByVal hWnd&, ByVal nIndex&, ByVal dwNewLong&)
 
 #Else
Private Declare Function FindWindowA& Lib "user32" (ByVal lpClassName$, ByVal lpWindowName$)
Private Declare Function EnableWindow& Lib "user32" (ByVal hWnd&, ByVal bEnable&)
Private Declare Function GetWindowLongA& Lib "user32" (ByVal hWnd&, ByVal nIndex&)
Private Declare Function SetWindowLongA& Lib "user32" (ByVal hWnd&, ByVal nIndex&, ByVal dwNewLong&)
#End If
Option Compare Text 'Pour ignorer les majuscules ou minuscules
Option Explicit
'swf
Dim Conn As Object      'pour connection base
Dim connstring              'pour connection base
Dim rs As Object           'recordset
Dim Sql                         'chaine requete SQL
Dim TInfos                    'tableau recup requete SQL
Dim Flag_Nok As Boolean 'pas d'enregistrement
Dim NbRecord               'nombre d'enregistrement trouves
Dim Flg_Boutons As Boolean
 
Private Sub CommandButton1_Click()
On Error Resume Next
Consultation_Fourniseurs.Show 0
End Sub
 
Private Sub DTPicker1_Change()
TextBox4.Value = DTPicker1.Value
End Sub
Private Sub DTPicker2_Change()
TextBox5.Value = DTPicker2.Value
End Sub
Private Sub DTPicker3_Change()
TextBox1.Value = DTPicker3.Value
End Sub
Private Sub Ajouter_Click()
    If TextBox3 <> "" Then
        Set rs = CreateObject("ADODB.recordset")
        'ID  Code art    Types Ets   Nom Ets (Client)    Désignation DU (F)  DU (D/P)    DU (ST) Unité   Qté Sous-traitant
        Sql = "select * from [xxxxxx]"
        rs.Open Sql, Conn, 3, 3
        If Not rs.EOF And Not rs.BOF Then
            rs.AddNew
            '[Code art]  [Types Ets]  [Nom Ets (Client)] [Désignation]  [DU (F)]  [DU (D/P)]  [DU (ST)]  [Unité]  [Qté]  [Sous-traitant]
            rs.Fields(1) = TextBox3
            rs.Fields(2) = TextBox4
            rs.Fields(3) = TextBox5
            rs.Fields(4) = TextBox6
            rs.Fields(5) = TextBox7
            rs.Fields(6) = TextBox8
            rs.Fields(7) = TextBox9
            rs.Fields(8) = TextBox10
            rs.Fields(9) = TextBox11
            rs.Fields(10) = TextBox12
            rs.Update
        End If
        rs.Close
        Set rs = Nothing
        ListView1.ListItems.Clear
        Flg_Boutons = True
        Call Recherche_Infos_Affichage_LVW
        Flg_Boutons = False
    End If
    ThisWorkbook.Save
     MsgBox "Attention: votre enregistrement est Ajouter!!"
End Sub
 
 
Private Sub Modifier_Click()
    If TextBox3 <> "" Then
        Set rs = CreateObject("ADODB.recordset")
        Sql = "select * from [xxxxx] where ID=" & CLng(TextBox2) & ";"
        rs.Open Sql, Conn, 3, 3
        If Not rs.EOF And Not rs.BOF Then
            rs.Fields(1) = TextBox3
            rs.Fields(2) = TextBox4
            rs.Fields(3) = TextBox5
            rs.Fields(4) = TextBox6
            rs.Fields(5) = TextBox7
            rs.Fields(6) = TextBox8
            rs.Fields(7) = TextBox9
            rs.Fields(8) = TextBox10
            rs.Fields(9) = TextBox11
            rs.Fields(10) = TextBox12
            rs.Update
        End If
        rs.Close
        Set rs = Nothing
        ListView1.ListItems.Clear
        Flg_Boutons = True
        Call Recherche_Infos_Affichage_LVW
        Flg_Boutons = False
    End If
    ThisWorkbook.Save
     MsgBox "Attention: votre enregistrement est Modifier!!"
End Sub
 
Private Sub Sortir_Click()
ThisWorkbook.Save
Unload Me
End Sub
 
Private Sub Supprimer_Click()
    If TextBox3 <> "" Then
        Set rs = CreateObject("ADODB.Recordset")
        Sql = "select * from [xxxxxxxx] where ID=" & CLng(TextBox2) & ";"
        rs.Open Sql, Conn, 3, 3
        If Not rs.EOF And Not rs.BOF Then
            rs.Delete
            rs.Update
        End If
        rs.Close
        Set rs = Nothing
        ListView1.ListItems.Clear
        Flg_Boutons = True
        Call Recherche_Infos_Affichage_LVW
        Flg_Boutons = False
    End If
    ThisWorkbook.Save
     MsgBox "Attention: votre enregistrement est Supprimer!!"
End Sub
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
TextBox13 = "OUI"
Else
TextBox13 = "NON"
End If
End Sub
 
 
Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
    Dim i As Long
    Dim J As Long
    On Error Resume Next
    ComboBox1 = ""
    i = Me.ListView1.SelectedItem.Index
    Me.TextBox2 = Me.ListView1.ListItems(i).Text
    For J = 3 To Me.ListView1.ColumnHeaders.Count + 1
        Me.Controls("Textbox" & J) = ListView1.ListItems(i).ListSubItems(J - 2).Text
    Next J
 
End Sub
Private Sub UserForm_Activate()
    EnableWindow FindWindowA("XLMAIN", Application.Caption), 1
    If TextBox13 = "OUI" Then
    CheckBox1.Value = True
Else
    CheckBox1.Value = False
End If
End Sub
 
Private Sub UserForm_Initialize()
    Dim hWnd As Long
    Dim Ligne, Password
 
    hWnd = FindWindowA(vbNullString, Me.Caption)
    SetWindowLongA hWnd, -16, GetWindowLongA(hWnd, -16) Or &H20000
    With Me.ListView1
        With .ColumnHeaders
            .Clear
            .Add , , "ID", 40, lvwColumnLeft
            .Add , , "xxxxxxxxx", 130, lvwColumnCenter
            .Add , , "xxxxxxxxx", 80, lvwColumnCenter
            .Add , , "xxxxxxxxx", 77, lvwColumnCenter
            .Add , , "xxxxxxxxx", 40, lvwColumnCenter
            .Add , , "xxxxxxxxx", 128, lvwColumnCenter
            .Add , , "xxxxxxxxx", 80, lvwColumnCenter
            .Add , , "xxxxxxxxx", 100, lvwColumnCenter
            .Add , , "xxxxxxxxx", 110, lvwColumnCenter
            .Add , , "xxxxxxxxx", 40, lvwColumnCenter
            .Add , , "xxxxxxxxx", 40, lvwColumnCenter
        End With
        Ligne = 1
        .Gridlines = True
        .View = lvwReport
        .FullRowSelect = True
'        DTPicker1.Value = Date
'        DTPicker2.Value = Date
'        DTPicker3.Value = Date
        Ajouter.Visible = False
        Modifier.Visible = False
        Supprimer.Visible = False
        TextBox3.Value = "xxxxxxxxx"
        TextBox4.Value = "xxxxxxxxx"
        TextBox5.Value = "xxxxxxxxx"
        TextBox6.Value = "xxxxxxxxx"
        TextBox7.Value = "xxxxxxxxx"
        TextBox8.Value = "xxxxxxxxx"
        TextBox9.Value = "xxxxxxxxx"
        TextBox10.Value = "xxxxxxxxx"
        TextBox11.Value = "xxxxxxxxx"
        TextBox12.Value = "xxxxxxxxx"
    End With
    'connection base
    Call Connecte_base_Access
End Sub
 
Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim i As Long
    Dim C As Range
    If ComboBox1 <> "" Then
 
        TextBox2 = ""
        TextBox3 = ""
        TextBox4 = ""
        TextBox5 = ""
        TextBox6 = ""
        TextBox7 = ""
        TextBox8 = ""
        TextBox9 = ""
        TextBox10 = ""
        TextBox11 = ""
        TextBox12 = ""
        ListView1.ListItems.Clear
        Call Recherche_Infos_Affichage_LVW
    End If
End Sub
 
'connection base et recherche
Sub Connecte_base_Access()
    Dim rs As Object
    Dim Nom_Base, Chemin_Base, Sql      ', connstring
 
    Set Conn = CreateObject("ADODB.Connection")
    Nom_Base = "table acces.accdb"
    Chemin_Base = ThisWorkbook.Path & "\" & Nom_Base
    'chaine de connexion
    connstring = "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & Chemin_Base & ";Uid=Admin;Pwd=xxxxxxxxx;ExtendedAnsiSQL=1;"
    'Connexion a la base
    Conn.Open connstring
End Sub
 
Sub Recherche_Infos_Affichage_LVW()
    Dim rs As Object
    Dim DT1, dt2
    Dim PartTxt, Sql, SQL1, N, L, C, D, e, NbF
On Error Resume Next
    Set rs = CreateObject("ADODB.recordset")
    PartTxt = ComboBox1
 
 
    Sql = "select * from [xxxxxxxxx] where [xxxxxxxxx] like '%" & PartTxt & "%' or [xxxxxxxxx] like '%" & PartTxt & "%' or [xxxxxxxxx] like '%" & PartTxt & "%' or [xxxxxxxxx] like '%" & PartTxt & "%' or [xxxxxxxxx] like '%" & PartTxt & "%' or [xxxxxxxxx] like '%" & PartTxt & "%' or [xxxxxxxxx] like '%" & PartTxt & "%' or [xxxxxxxxx] like '%" & PartTxt & "%' or [xxxxxxxxx] like '%" & PartTxt & "%' or [xxxxxxxxx] like '%" & PartTxt & "%'"
    rs.Open Sql, Conn, 3, 3
'    rs.MoveFirst
'Do While Not rs.EOF
'    ComboBox1.AddItem rs!xxxxxxxxx
'    rs.MoveNext
'Loop
 
    If Not rs.EOF Then
        rs.MoveFirst
        NbF = rs.Fields.Count
        NbRecord = rs.RecordCount
        N = 1
        Do While Not rs.EOF
            With ListView1
                .ListItems.Add , , rs.Fields(0)
                For L = 2 To NbF
 
                    .ListItems(N).ListSubItems.Add , , rs.Fields(L - 1)
                Next L
                 If .ListItems(N) = ComboBox1 Then .ListItems(N).Bold = True
                If .ListItems(N).ListSubItems(7).Text = "xxxxxxxxx" Then
                    .ListItems(N).Bold = True
                    .ListItems(N).ForeColor = vbGreen
                    For C = 1 To .ColumnHeaders.Count - 1
                        .ListItems(N).ListSubItems(C).Bold = True
                        .ListItems(N).ListSubItems(C).ForeColor = vbGreen     'couleur colonne 7
                    Next C
                    End If
 
                  If .ListItems(N).ListSubItems(7).Text = "xxxxxxxxx" Then
                    .ListItems(N).Bold = True
                    .ListItems(N).ForeColor = vbBlue
                    For D = 1 To .ColumnHeaders.Count - 1
                        .ListItems(N).ListSubItems(D).Bold = True
                        .ListItems(N).ListSubItems(D).ForeColor = vbBlue     'couleur colonne 7
                    Next D
                    End If
 
                   If .ListItems(N).ListSubItems(8).Text = "xxxxxxxxx" Then
                    .ListItems(N).Bold = True
                    .ListItems(N).ForeColor = vbRed
                    For e = 1 To .ColumnHeaders.Count - 1
                        .ListItems(N).ListSubItems(e).Bold = True
                        .ListItems(N).ListSubItems(e).ForeColor = vbRed     'couleur colonne 8
                    Next e
                End If
            End With
            N = N + 1
            rs.MoveNext
        Loop
        Label2.Caption = NbRecord & " enregistrement(s) !"
    Else
        MsgBox "Attention: pas d'enregistrement trouvé!!"
    End If
    rs.Close
    Set rs = Nothing
 
End Sub