-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserForm1.frm
More file actions
180 lines (146 loc) · 5.67 KB
/
UserForm1.frm
File metadata and controls
180 lines (146 loc) · 5.67 KB
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
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} UserForm1
Caption = "UserForm1"
ClientHeight = 6480
ClientLeft = 108
ClientTop = 456
ClientWidth = 11748
OleObjectBlob = "UserForm1.frx":0000
StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "UserForm1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub CommandButton1_Click()
Dim lastRow As Long
Dim conn As Object
Dim cmd As Object
Dim rs As Object
Dim strConn As String
Dim strSQLClient As String
Dim strSQLProduit As String
Dim strSQLCommande As String
Dim strSQLLigneCommande As String
Dim prodPrix As Variant
Dim quantite As Variant
Dim commandeDate As String
Dim clientID As Long
Dim produitID As Long
Dim commandeID As Long
' Validation des champs obligatoires
If Trim(UserForm1.Tclientnom.Value) = "" Or _
Trim(UserForm1.Tclientemail.Value) = "" Or _
Trim(UserForm1.Tclienttel.Value) = "" Or _
Trim(UserForm1.Tprodnom.Value) = "" Or _
Trim(UserForm1.Tprodcat.Value) = "" Or _
Trim(UserForm1.Tquantite.Value) = "" Or _
Trim(UserForm1.Tdate.Value) = "" Then
MsgBox "Veuillez remplir tous les champs obligatoires.", vbExclamation
Exit Sub
End If
' Validation et préparation des champs numériques
If IsNumeric(UserForm1.Tprodprix.Value) And UserForm1.Tprodprix.Value <> "" Then
prodPrix = UserForm1.Tprodprix.Value
Else
MsgBox "Veuillez entrer un prix valide.", vbExclamation
Exit Sub
End If
If IsNumeric(UserForm1.Tquantite.Value) And UserForm1.Tquantite.Value <> "" Then
quantite = UserForm1.Tquantite.Value
Else
MsgBox "Veuillez entrer une quantité valide.", vbExclamation
Exit Sub
End If
commandeDate = Trim(UserForm1.Tdate.Value) ' Format date (assurez-vous qu'il soit valide)
' Ajout des données dans Excel
Sheets("Feuil1").Activate
With Sheets("Feuil1")
lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1
.Cells(lastRow, 2).Value = Trim(UserForm1.Tclientnom.Value)
.Cells(lastRow, 3).Value = Trim(UserForm1.Tclientemail.Value)
.Cells(lastRow, 4).Value = Trim(UserForm1.Tclienttel.Value)
.Cells(lastRow, 6).Value = Trim(UserForm1.Tprodnom.Value)
.Cells(lastRow, 7).Value = Trim(UserForm1.Tprodcat.Value)
.Cells(lastRow, 8).Value = quantite ' Quantité dans la 8ème colonne
.Cells(lastRow, 9).Value = prodPrix ' Prix dans la 9ème colonne
.Cells(lastRow, 10).Value = commandeDate
End With
' Connexion à la base MySQL
On Error GoTo Erreur
strConn = "Driver={MySQL ODBC 9.1 Unicode Driver};Server=localhost;Database=BD_Gestion_de_Commandes;User=root;Password=adam123;"
Set conn = CreateObject("ADODB.Connection")
conn.Open strConn
If conn.State = 0 Then
MsgBox "Échec de la connexion à MySQL.", vbCritical
Exit Sub
End If
' Transactions
conn.BeginTrans
' Requête pour insérer le client
strSQLClient = "INSERT INTO clients (nom_complet, email, telephone) VALUES ('" & _
Replace(Trim(UserForm1.Tclientnom.Value), "'", "''") & "', '" & _
Replace(Trim(UserForm1.Tclientemail.Value), "'", "''") & "', '" & _
Replace(Trim(UserForm1.Tclienttel.Value), "'", "''") & "')"
Debug.Print "Requête client : " & strSQLClient
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = strSQLClient
cmd.Execute
' Récupérer l'ID du client inséré
Set rs = conn.Execute("SELECT LAST_INSERT_ID()")
clientID = rs.Fields(0).Value
' Requête pour insérer le produit
strSQLProduit = "INSERT INTO produits (nom, categorie, pric) VALUES ('" & _
Replace(Trim(UserForm1.Tprodnom.Value), "'", "''") & "', '" & _
Replace(Trim(UserForm1.Tprodcat.Value), "'", "''") & "', " & _
prodPrix & ")"
Debug.Print "Requête produit : " & strSQLProduit
cmd.CommandText = strSQLProduit
cmd.Execute
' Récupérer l'ID du produit inséré
Set rs = conn.Execute("SELECT LAST_INSERT_ID()")
produitID = rs.Fields(0).Value
' Requête pour insérer la commande
strSQLCommande = "INSERT INTO commandes (client_id, date_cammande) VALUES (" & clientID & ", '" & Replace(commandeDate, "'", "''") & "')"
Debug.Print "Requête commande : " & strSQLCommande
cmd.CommandText = strSQLCommande
cmd.Execute
' Récupérer l'ID de la commande insérée
Set rs = conn.Execute("SELECT LAST_INSERT_ID()")
commandeID = rs.Fields(0).Value
' Requête pour insérer la ligne de commande
strSQLLigneCommande = "INSERT INTO ligne_commandes (commande_id, produit_id, quantite) VALUES (" & commandeID & ", " & produitID & ", " & quantite & ")"
Debug.Print "Requête ligne commande : " & strSQLLigneCommande
cmd.CommandText = strSQLLigneCommande
cmd.Execute
' Commit
conn.CommitTrans
MsgBox "Données insérées avec succès dans MySQL et Excel!", vbInformation
' Nettoyage
conn.Close
Set conn = Nothing
Set cmd = Nothing
Exit Sub
Erreur:
conn.RollbackTrans
MsgBox "Erreur lors de l'insertion des données : " & Err.Description, vbCritical
If Not conn Is Nothing Then conn.Close
Set conn = Nothing
Set cmd = Nothing
End Sub
Private Sub Label1_Click()
End Sub
Private Sub Label4_Click()
End Sub
Private Sub Label6_Click()
End Sub
Private Sub Label8_Click()
End Sub
Private Sub Label9_Click()
End Sub
Private Sub Tprodnom_Change()
End Sub
Private Sub UserForm_Click()
End Sub