Bouton modifier base données
Bonjour à tous
je débute en vba et en regardant à droite à gauche j 'ai compiler ce petit programme mais je reste bloquer que l'étape de la modification de données.
Si une âme sympathique passe et m'aide ce serait vraiment sympa
Private Sub modifier_Click()
Dim Ligne As Long
'ici une boucle sur tous les controls, si un est vide on sort et on set le focus dessus
If TextBox_nom = "" Or TextBox_prenom = "" Or ComboBox_classe = "" Or ComboBox_nom_ecole = "" Or TextBox_date_j = "" Or ComboBox_horaire = "" Or ComboBox_test_initial = "" Or ComboBox_test_final = "" Or ComboBox_affectation = "" Or ComboBox_educateur = "" Then
MsgBox "Les informations sont obligatoires. MERCI DE LES SAISIR.", _
vbInformation
Exit Sub
End If
'Ici un message demandant d'accepter les changement en les listant
If MsgBox("Acceptez-vous ces changements ? ", vbQuestion + vbOKCancel) <> vbOK Then
MsgBox "Opération annulée", vbInformation
Else
'Si Réponse OK on continue
Ligne = Val(Me.Label2.Caption) 'ici que je suis pas sur
'ici avec la Feuille on va faire :
With Feuil1
.Range("A" & Ligne) = TextBox_nom
.Range("B" & Ligne) = TextBox_prenom
.Range("C" & Ligne) = ComboBox_classe
.Range("D" & Ligne) = TextBox_date_j
.Range("E" & Ligne) = ComboBox_nom_ecole
.Range("G" & Ligne) = ComboBox_horaire
.Range("H" & Ligne) = ComboBox_test_initial
.Range("I" & Ligne) = ComboBox_test_final
.Range("J" & Ligne) = ComboBox_affectation
.Range("K" & Ligne) = ComboBox_educateur
End With
'On envoie un message de confirmation
MsgBox "Opération accomplie", vbInformation
End If
End Sub
très bonne journée
Bonsoir,
Il manque une information importante ou le fichier afin que l'on puisse t'aider.
Que devrait contenir label2 ? Normalement c'est une étiquette, un texte d'information, pas comme un TextBox, un champ de text ou l'on saisi ce que l'on veut en fonction de la situation. Regarde si tu ne devrais pas remplacer label2 par un TextBox, un ComboBox ou un ListBox.
Sinon envoie ton fichier en supprimant le contenu de ta BD sauf quelques lignes, et tu auras rapidement une réponse.
Bonsoir
le fichier est trop gros même compresser ci-dessous le code vba
Private Sub CommandButton_Ajouter_Click()
'Coloration des Labels en noir
Label_nom.ForeColor = RGB(0, 0, 0)
Label_prenom.ForeColor = RGB(0, 0, 0)
Label_classe.ForeColor = RGB(0, 0, 0)
Label_nom_ecole.ForeColor = RGB(0, 0, 0)
Label_horaire.ForeColor = RGB(0, 0, 0)
Label_test_initial.ForeColor = RGB(0, 0, 0)
Label_niveau_final.ForeColor = RGB(0, 0, 0)
Label_affectation.ForeColor = RGB(0, 0, 0)
Label_educateur.ForeColor = RGB(0, 0, 0)
'Contrôles de contenu
If TextBox_nom.Value = "" Then 'SI pas de "nom" ...
Label_nom.ForeColor = RGB(255, 0, 0) 'Label "nom" en rouge
ElseIf TextBox_prenom.Value = "" Then
Label_prenom.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_classe.Value = "" Then
Label_classe.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_nom_ecole.Value = "" Then
Label_nom_ecole.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_horaire.Value = "" Then
Label_horaire.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_test_initial.Value = "" Then
Label_test_initial.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_test_final.Value = "" Then
Label_test_final.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_affectation.Value = "" Then
Label_affectation.ForeColor = RGB(255, 0, 0)
ElseIf ComboBox_educateur.Value = "" Then
Label_educateur.ForeColor = RGB(255, 0, 0)
Else
'Si le formulaire est complet, on insère les valeurs sur la feuille
Dim no_ligne As Integer, trimestre As String
'Choix du trimestre
For Each bouton_trimestre In Frame_trimestre.Controls
If bouton_trimestre.Value Then
trimestre = bouton_trimestre.Caption
End If
Next
'no_ligne = N° de ligne de la dernière cellule non vide de la colonne +1
no_ligne = Range("A65536").End(xlUp).Row + 1
'Insertion des valeurs sur la feuille
If MsgBox("confirmer l'enregistrement ?", vbYesNo, "demande de confirmation") = vbYes Then
Cells(no_ligne, 1) = TextBox_nom.Value
Cells(no_ligne, 2) = TextBox_prenom.Value
Cells(no_ligne, 3) = ComboBox_classe.Value
Cells(no_ligne, 4) = TextBox_date_j.Value
Cells(no_ligne, 5) = ComboBox_nom_ecole.Value
Cells(no_ligne, 6) = trimestre
Cells(no_ligne, 7) = ComboBox_horaire.Value
Cells(no_ligne, 8) = ComboBox_test_initial.Value
Cells(no_ligne, 9) = ComboBox_test_final.Value
Cells(no_ligne, 10) = ComboBox_affectation.Value
Cells(no_ligne, 11) = ComboBox_educateur.Value
End If
'Après insertion, on remet les valeurs initiales
TextBox_nom.Value = ""
TextBox_prenom.Value = ""
ComboBox_classe.Value = ""
ComboBox_nom_ecole.Value = ""
OptionButton1.Value = True
ComboBox_horaire.Value = ""
ComboBox_test_initial.Value = ""
ComboBox_test_final.Value = ""
ComboBox_affectation.Value = ""
ComboBox_educateur.Value = ""
End If
End Sub
Private Sub CommandButton_efface_Click()
TextBox_nom.Value = ""
TextBox_prenom.Value = ""
ComboBox_classe.Value = ""
ComboBox_nom_ecole.Value = ""
OptionButton1.Value = True
ComboBox_horaire.Value = ""
ComboBox_test_initial.Value = ""
ComboBox_test_final.Value = ""
ComboBox_affectation.Value = ""
ComboBox_educateur.Value = ""
End Sub
Private Sub CommandButton_Fermer_Click()
Unload Me
End Sub
Private Sub effacer2_Click()
tri.Value = ""
End Sub
Private Sub ListBox1_Change()
'Dim Cel As Range
If ListBox1.ListIndex = -1 Then Exit Sub
TextBox_nom = ListBox1.List(ListBox1.ListIndex, 0)
TextBox_prenom = ListBox1.List(ListBox1.ListIndex, 1)
ComboBox_classe = ListBox1.List(ListBox1.ListIndex, 2)
'OptionButton1 = ListBox1.List(ListBox1.ListIndex, 3)
ComboBox_nom_ecole = ListBox1.List(ListBox1.ListIndex, 4)
ComboBox_horaire = ListBox1.List(ListBox1.ListIndex, 5)
ComboBox_test_initial = ListBox1.List(ListBox1.ListIndex, 6)
ComboBox_test_final = ListBox1.List(ListBox1.ListIndex, 7)
ComboBox_affectation = ListBox1.List(ListBox1.ListIndex, 8)
ComboBox_educateur = ListBox1.List(ListBox1.ListIndex, 9)
End Sub
Private Sub modifier_Click() 'c'est ici que ca fonctionne pas
Dim Ligne As Long
'ici une boucle sur tous les controls, si un est vide on sort et on set le focus dessus
If TextBox_nom = "" Or TextBox_prenom = "" Or ComboBox_classe = "" Or ComboBox_nom_ecole = "" Or TextBox_date_j = "" Or ComboBox_horaire = "" Or ComboBox_test_initial = "" Or ComboBox_test_final = "" Or ComboBox_affectation = "" Or ComboBox_educateur = "" Then
MsgBox "Les informations sont obligatoires. MERCI DE LES SAISIR.", _
vbInformation
Exit Sub
End If
'Ici un message demandant d'accepter les changement en les listant
If MsgBox("Acceptez-vous ces changements ? ", vbQuestion + vbOKCancel) <> vbOK Then
MsgBox "Opération annulée", vbInformation
Else
'Si Réponse OK on continue
Ligne = Val(Me.Label2.Caption)
'ici avec la Feuille on va faire :
With Feuil1
.Range("A" & Ligne) = TextBox_nom
.Range("B" & Ligne) = TextBox_prenom
.Range("C" & Ligne) = ComboBox_classe
.Range("D" & Ligne) = TextBox_date_j
.Range("E" & Ligne) = ComboBox_nom_ecole
.Range("G" & Ligne) = ComboBox_horaire
.Range("H" & Ligne) = ComboBox_test_initial
.Range("I" & Ligne) = ComboBox_test_final
.Range("J" & Ligne) = ComboBox_affectation
.Range("K" & Ligne) = ComboBox_educateur
End With
'On envoie un message de confirmation
MsgBox "Opération accomplie", vbInformation
End If
End Sub
Private Sub recherche_Click()
Dim Plage As Range, Cell As Range
Dim recherche As String, adresse As String ', Col As String
Dim Ligne As Variant
Dim Cel As Range
Dim C As Range
Dim Col As Integer
ListBox1.Clear
recherche = tri.Value
If recherche = "" Then Exit Sub
If triintitulé.Value = True Then
Col = 1 ' "A"
ElseIf triecole.Value = True Then
Col = 5 ' "E"
End If
Ligne = Feuil1.Cells(Rows.Count, Col).End(xlUp).Row
Set Plage = Feuil1.Range(Cells(2, Col), Cells(Ligne, Col))
With Plage
Set Cel = .Find(what:=recherche, LookIn:=xlValues, lookat:=xlPart)
If Not Cel Is Nothing Then
adresse = Cel.Address
Do
With Me.ListBox1
.AddItem Feuil1.Cells(Cel.Row, "A")
.List(.ListCount - 1, 1) = Feuil1.Cells(Cel.Row, "B")
.List(.ListCount - 1, 2) = Feuil1.Cells(Cel.Row, "C")
.List(.ListCount - 1, 3) = Feuil1.Cells(Cel.Row, "D")
.List(.ListCount - 1, 4) = Feuil1.Cells(Cel.Row, "E")
.List(.ListCount - 1, 5) = Feuil1.Cells(Cel.Row, "G")
.List(.ListCount - 1, 6) = Feuil1.Cells(Cel.Row, "H")
.List(.ListCount - 1, 7) = Feuil1.Cells(Cel.Row, "I")
.List(.ListCount - 1, 8) = Feuil1.Cells(Cel.Row, "J")
.List(.ListCount - 1, 9) = Feuil1.Cells(Cel.Row, "K")
End With
Set Cel = .FindNext(Cel)
Loop While Not Cel Is Nothing And Cel.Address <> adresse
End If
End With
End Sub
'forcer le nom en majuscule
Private Sub TextBox_nom_Change()
TextBox_nom = UCase(TextBox_nom)
End Sub
'forcer prenom nom propre
Private Sub TextBox_prenom_Change()
TextBox_prenom = Application.Proper(TextBox_prenom)
End Sub
Private Sub UserForm_Initialize()
For i = 1 To 23 'Liste des combobox
ComboBox_classe.AddItem Sheets("Tables").Cells(i, 7)
ComboBox_nom_ecole.AddItem Sheets("Tables").Cells(i, 1)
ComboBox_horaire.AddItem Sheets("Tables").Cells(i, 4)
ComboBox_test_initial.AddItem Sheets("Tables").Cells(i, 5)
ComboBox_test_final.AddItem Sheets("Tables").Cells(i, 6)
ComboBox_affectation.AddItem Sheets("Tables").Cells(i, 8)
ComboBox_educateur.AddItem Sheets("Tables").Cells(i, 2)
Next
'affichage annee
TextBox_date_j.Value = Format(Date, "yyyy")
'affichage trimestre
OptionButton1.Value = True
triintitulé.Value = True
End Sub
cordialemement
Cela ne nous aide pas du tout.
Peux-tu m'expliquer ce qu'est ce label2 et ce qu'il est sensé contenir ?
ok désole
en faite dans un premier temps je saisie un nouvel enregistrement (procédure ok)
ensuite j'ai un bouton "recherche" qui m'affiche les résultats dans une listbox (ok)
si plusieurs données trouvées je clique sur la ligne concerné et les données s'affiche dans les textbox pour pouvoir être modifier lorsque je cliquerai sur le bouton "modifier"
et je ne sais pas comment modifier la ligne que j'ai sélectionné et le label c'est parce que j'ai vu dans un fichier que le gars faisait comme ca je comprenais pas non plus pourquoi mais ca fonctionnait . Je débute c'est pour ca que je vous demande votre aide
Ma question : comment modifier ou supprimer les données de la ligne sélectionnée ?
vous pouvez telecharger le fichier a l'dresse suivant
cordialement
Bonjour,
' Déclaration du tableau au niveau du module, à mettre tout au début, devant la première procédure du module Formulaire
Dim TabCompl As Variant' A mettre au début de la procédure recherche_Click(), juste après les déclarations de variables :
ReDim TabCompl(0)A mettre dans la boucle de recherche :
.List(.ListCount - 1, 9) = Feuil1.Cells(Cel.Row, "K")
ReDim Preserve TabCompl(1 To .ListCount)
TabCompl(.ListCount) = Cel.Row
End WithA mettre dans la procédure modifier_Click()
'Si RŽponse OK on continue
Ligne = TabCompl(ListBox1.ListIndex + 1)Ensuite il faut réactualiser le ListBox pour prendre en compte la modification.
Important : n'utilise pas de lettre accentuée pour déclarer les variables, parce que l'encodage peut être différent d'un ordinateur à l'autre, j'ai dû réencoder une variable (triintitulé) car les é se transformaient en Ž. Si un commentaire dans un MsgBox peut être génant, cela peut engendrer des erreurs de compilation au niveau des variables.
On ne peut pas stocker plus de 10 colonnes dans un ListBox (0 à 9), c'est pourquoi j'ai stocké le numéro de la ligne dans un tableau (TabCompl), mais quand je n'utilise pas toutes les colonnes, je stocke le numéro de ligne en colonne 9.
Si tu veux faire la même chose pour supprimer une ligne, après la suppression il faut réactualiser le ListBox pour que le TabCompl et le ListBox prennent en compte la suppression de ligne.
Cool c exactement ça
Je t en remercie