IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

problème sur le formulaire


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Rédacteur ISO
    Inscrit en
    Septembre 2014
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Rédacteur ISO
    Secteur : Transports

    Informations forums :
    Inscription : Septembre 2014
    Messages : 20
    Par défaut problème sur le formulaire
    Bonjour à tous,
    je viens de créer un formulaire pour un recensement de client, et une fois les données rentré dans le formulaire tout va bien dans la BD. Le soucis est que quand je retape le nom dans le formulaire à l'emplacement de mes CheckBox tout se décale et je me trouve avec des message TRUe et Flase. Impossible de trouver le bug. Si qq pouvait jeter un coup d'oeil au code et m'aider ce serait génial.
    je précise que je suis vraiment débutant.
    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
    Private Sub MultiPage1_Change()
     
    End Sub
     
    Private Sub UserForm_Initialize()
    Dim J As Long
    Dim I As Integer
    ComboBox2.ColumnCount = 1
    ComboBox2.List() = Array("", "Rue", "Av", "Bd", "Allée", "Place", "Ch", "Imp", "Square", "Quai", "RP")
    Set Ws = Sheets("Redevance")
    With Me.ComboBox1
    For J = 2 To Ws.Range("A" & Rows.Count).End(xlUp).Row
    .AddItem Ws.Range("A" & J)
    Next J
    End With
    For I = 1 To 41
    Me.Controls("TextBox" & I).Visible = True
    Next I
    End Sub
    Private Sub ComboBox1_Change()
    Dim Ligne As Long
    Dim I As Integer
    If ComboBox1 = "" Then Exit Sub
    With Sheets("Redevance")
    Ligne = Me.ComboBox1.ListIndex + 2
    ComboBox2 = Ws.Cells(Ligne, "B")
    For I = 1 To 41
    Me.Controls("TextBox" & I) = .Cells(Ligne, I + 2).Value
    Next I
    End With
    End Sub
    Private Sub CommandButton1_Click()
    Dim L As Integer
    If MsgBox("Confirmez-vous l’insertion de ce nouveau contact ?", vbYesNo, "Demande de confirmation d’ajout") = vbYes Then
    L = Sheets("Redevance").Range("a65536").End(xlUp).Row + 1
    Range("A" & L).Value = ComboBox1
    Range("B" & L).Value = ComboBox2
    Range("C" & L).Value = TextBox1
    Range("D" & L).Value = TextBox2
    Range("E" & L).Value = TextBox3
    Range("F" & L).Value = TextBox4
    Range("G" & L).Value = TextBox5
    Range("H" & L).Value = TextBox6
    Range("I" & L).Value = TextBox7
    Range("J" & L).Value = TextBox8
    Range("K" & L).Value = TextBox9
    Range("L" & L).Value = TextBox10
    Range("M" & L).Value = TextBox11
    Range("P" & L).Value = TextBox12
    Range("s" & L).Value = TextBox13
    Range("T" & L).Value = TextBox14
    Range("U" & L).Value = TextBox15
    Range("V" & L).Value = TextBox16
    Range("W" & L).Value = TextBox17
    Range("X" & L).Value = TextBox18
    Range("Y" & L).Value = TextBox19
    Range("Z" & L).Value = TextBox20
    Range("AA" & L).Value = TextBox21
    Range("AB" & L).Value = TextBox22
    Range("AC" & L).Value = TextBox23
    Range("AD" & L).Value = TextBox24
    Range("AE" & L).Value = TextBox25
    Range("AF" & L).Value = TextBox26
    Range("AG" & L).Value = TextBox27
    Range("AH" & L).Value = TextBox28
    Range("AI" & L).Value = TextBox29
    Range("AJ" & L).Value = TextBox30
    Range("AK" & L).Value = TextBox31
    Range("AL" & L).Value = TextBox32
    Range("AM" & L).Value = TextBox33
    Range("AN" & L).Value = TextBox34
    Range("AO" & L).Value = TextBox35
    Range("AP" & L).Value = TextBox36
    Range("AQ" & L).Value = TextBox37
    Range("AR" & L).Value = TextBox38
    Range("AS" & L).Value = TextBox39
    Range("AT" & L).Value = TextBox40
    Range("BA" & L).Value = TextBox41
    Range("N" & L).Value = CheckBox1
    Range("O" & L).Value = CheckBox2
    Range("Q" & L).Value = CheckBox3
    Range("R" & L).Value = CheckBox4
    Range("AU" & L).Value = CheckBox5
    Range("AV" & L).Value = CheckBox6
    Range("AW" & L).Value = CheckBox7
    Range("AX" & L).Value = CheckBox8
    Range("AY" & L).Value = CheckBox9
    Range("AZ" & L).Value = CheckBox10
    Range("BB" & L).Value = CheckBox11
    Range("BC" & L).Value = CheckBox12
     
     
    End If
    End Sub
    Private Sub CommandButton2_Click()
    Dim Ligne As Long
    Dim I As Integer
    If MsgBox("Confirmez-vous la modification de ce contact ?", vbYesNo, "Demande de confirmation de modification") = vbYes Then
    If Me.ComboBox1.ListIndex = -1 Then Exit Sub
    Ligne = Me.ComboBox1.ListIndex + 2
    Ws.Cells(Ligne, "B") = ComboBox2
    For I = 1 To 41
    If Me.Controls("TextBox" & I).Visible = True Then
    Ws.Cells(Ligne, I + 2) = Me.Controls("TextBox" & I)
    End If
    Next I
    End If
    End Sub
    Private Sub CommandButton3_Click()
    Unload Me
    End Sub

  2. #2
    Membre Expert Avatar de Thautheme
    Homme Profil pro
    salarié
    Inscrit en
    Août 2014
    Messages
    1 373
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : salarié

    Informations forums :
    Inscription : Août 2014
    Messages : 1 373
    Par défaut
    Bonjour Pascal, bonjour le forum,

    Quand tu ajoutes une données tu ENVOIES les valeurs des 41 textboxes dans les colonnes C à AT mais tu sautes les colonnes N, O, Q, R, AU, AV, AW, AX, AY, AZ qui elles récupèrent les données des checkboxes.
    Quand tu MODIFIES tu boucles sur les 41 textboxes dans les colonne I+2 à I+43. Tu as forcément incohérence puisque tu n'as pas pris en compte les checkboxes.

    Pour éviter cela voici une méthode que j'utilise dans ce cas. J'attribue à la propriété [Tag] des contrôles contenant des données (ComboBoxes, TextBoxes et Checkboxes dans ton cas) la valeur de la colonne qui doit recevoir ces données. Par exemple la propriété [Tag] de la ComboBox1 serait égale à 1 (=A), celle de la ComboBox2 à 2 (=B), celle de la TextBox1 à 3 (=C), celle de la Checkbox1 à 14 (=N), etc. Je convertis ensuite la propriété [Tag] pour pouvoir l'utiliser dans le code...
    Cela permet de boucler avec : CTRL.Value = WS.Cells(Ligne, CByte(CTRL.Tag).Value.
    Ton code avec cette méthode :

    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
    Private WS As Worksheet
    Private CTRL As Control
     
    Private Sub UserForm_Initialize()
    Dim J As Long
    Dim I As Integer
     
    Set WS = Sheets("Redevance")
    ComboBox2.ColumnCount = 1 'ligne inutile !?
    ComboBox2.List() = Array("", "Rue", "Av", "Bd", "Allée", "Place", "Ch", "Imp", "Square", "Quai", "RP")
    With Me.ComboBox1
        For J = 2 To WS.Range("A" & Rows.Count).End(xlUp).Row
            .AddItem WS.Range("A" & J)
        Next J
    End With
    For I = 1 To 41
        Me.Controls("TextBox" & I).Visible = True
    Next I
    End Sub
     
    Private Sub ComboBox1_Change()
    Dim Ligne As Long
    Dim I As Integer
     
    If ComboBox1 = "" Then Exit Sub
    Ligne = Me.ComboBox1.ListIndex + 2
    For Each CTRL In Me.Controls
        If CTRL.Tag <> "" Then CTRL.Value = WS.Cells(Ligne, CByte(CTRL.Tag)).Value
    Next CTRL
    End Sub
     
    Private Sub CommandButton1_Click()
    Dim L As Integer
    Dim CTRL As Control
     
    If MsgBox("Confirmez-vous l’insertion de ce nouveau contact ?", vbYesNo, "Demande de confirmation d’ajout") = vbYes Then
        L = WS.Cells(Application.Rows.Count, 1).End(xlUp).Row + 1
        For Each CTRL In Me.Controls
            If CTRL.Tag <> "" Then WS.Cells(L, CByte(CTRL.Tag)).Value = CTRL.Value
        Next CTRL
    End If
    End Sub
     
    Private Sub CommandButton2_Click()
    Dim Ligne As Long
    Dim I As Integer
     
    If MsgBox("Confirmez-vous la modification de ce contact ?", vbYesNo, "Demande de confirmation de modification") = vbYes Then
        If Me.ComboBox1.ListIndex = -1 Then Exit Sub
        Ligne = Me.ComboBox1.ListIndex + 2
        WS.Cells(Ligne, "B") = ComboBox2
        For Each CTRL In Me.Controls
            If CTRL.Tag <> "" Then WS.Cells(Ligne, CByte(CTRL.Tag)).Value = CTRL.Value
        Next CTRL
    End If
    End Sub
     
    Private Sub CommandButton3_Click()
    Unload Me
    End Sub

  3. #3
    Membre averti
    Homme Profil pro
    Rédacteur ISO
    Inscrit en
    Septembre 2014
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Rédacteur ISO
    Secteur : Transports

    Informations forums :
    Inscription : Septembre 2014
    Messages : 20
    Par défaut
    Bonjour Thautheme,
    Merci pour la réponse
    Je viens de faire les changements dans chaque [tag] de la propriété des comboBox, TextBox et checkBox j'ai attribué un numéro comme tu le préconise et recopié le code que tu m'a rectifier.
    J'ai un souci dans la comboBox1 où dans le formulaire je ne peut plus écrire, à partir de la comboBox2 tout va bien.
    le deuxième souci se trouve dans la BD où les enregistrements ne passent pas à la ligne suivante mais enregistre sur la même ligne en annulant l'enregistrement précédent.
    Merci d'avance pour ta réponse

  4. #4
    Membre Expert Avatar de Thautheme
    Homme Profil pro
    salarié
    Inscrit en
    Août 2014
    Messages
    1 373
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : salarié

    Informations forums :
    Inscription : Août 2014
    Messages : 1 373
    Par défaut
    Bonjour Pascal, bonjour le forum,

    Essaie de joindre ton fichier on perdra moins de temps...

  5. #5
    Membre averti
    Homme Profil pro
    Rédacteur ISO
    Inscrit en
    Septembre 2014
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Rédacteur ISO
    Secteur : Transports

    Informations forums :
    Inscription : Septembre 2014
    Messages : 20
    Par défaut
    je te fais parvenir le fichier
    Merci
    Fichiers attachés Fichiers attachés

  6. #6
    Membre Expert Avatar de Thautheme
    Homme Profil pro
    salarié
    Inscrit en
    Août 2014
    Messages
    1 373
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : salarié

    Informations forums :
    Inscription : Août 2014
    Messages : 1 373
    Par défaut
    Bonjour Pascal, bonjour le forum,

    • Ton Tableau commence à la ligne 8 = ComboBox1.ListIndex + 8 (et pas + 2)...
    • Pour pouvoir écrire dans la ComboBox1 j'ai rajouté dans la procédure Change de celle-ci :
    If ComboBox1 = "" Or ComboBox1.ListIndex = -1 Then Exit Sub
    • J'ai masqué le bouton Enregistrer quand la propriété ListIndex de la ComboBox1 était différent de -1
    • J'ai rajouté une ligne pour les Checkboxes...

    Le code modifié :

    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
    Private WS As Worksheet
    Private CTRL As Control
     
    Private Sub MultiPage1_Change()
     
    End Sub
     
    Private Sub UserForm_Initialize()
    Dim J As Long
    Dim I As Integer
     
    Set WS = Sheets("Redevance")
    ComboBox2.List() = Array("", "Rue", "Av", "Bd", "Allée", "Place", "Ch", "Imp", "Square", "Quai", "RP")
    With Me.ComboBox1
        For J = 8 To WS.Range("A" & Rows.Count).End(xlUp).Row
            .AddItem WS.Range("A" & J)
        Next J
    End With
    'pourquoi cette boucle ? à l'initialisation tes textboxes sont par défaut "Visible"
    For I = 1 To 79
        Me.Controls("TextBox" & I).Visible = True
    Next I
    End Sub
     
    Private Sub ComboBox1_Change()
    Dim Ligne As Long
    Dim I As Integer
     
    If ComboBox1 = "" Or ComboBox1.ListIndex = -1 Then Exit Sub
    Ligne = Me.ComboBox1.ListIndex + 8
    For Each CTRL In Me.Controls
        If CTRL.Tag <> "" Then CTRL.Value = WS.Cells(Ligne, CByte(CTRL.Tag)).Value
        If TypeOf CTRL Is MSForms.CheckBox Then CTRL.Value = IIf(WS.Cells(Ligne, CByte(CTRL.Tag)).Value = "", False, True)
    Next CTRL
    Me.CommandButton1.Visible = False
    End Sub
     
    Private Sub CommandButton1_Click()
    Dim L As Integer
    Dim CTRL As Control
     
    If MsgBox("Confirmez-vous l’insertion de ce nouveau contact ?", vbYesNo, "Demande de confirmation d’ajout") = vbYes Then
        L = WS.Cells(Application.Rows.Count, 1).End(xlUp).Row + 1
        For Each CTRL In Me.Controls
            If CTRL.Tag <> "" Then WS.Cells(L, CByte(CTRL.Tag)).Value = CTRL.Value
            If TypeOf CTRL Is MSForms.CheckBox Then WS.Cells(Ligne, CByte(CTRL.Tag)).Value = IIf(CTRL.Value = True, "X", "")
        Next CTRL
    End If
    End Sub
     
    Private Sub CommandButton2_Click()
    Dim Ligne As Long
    Dim I As Integer
     
    If MsgBox("Confirmez-vous la modification de ce contact ?", vbYesNo, "Demande de confirmation de modification") = vbYes Then
        If Me.ComboBox1.ListIndex = -1 Then Exit Sub
        Ligne = Me.ComboBox1.ListIndex + 8
        WS.Cells(Ligne, "B") = ComboBox2
        For Each CTRL In Me.Controls
            If CTRL.Tag <> "" Then WS.Cells(Ligne, CByte(CTRL.Tag)).Value = CTRL.Value
            If TypeOf CTRL Is MSForms.CheckBox Then WS.Cells(Ligne, CByte(CTRL.Tag)).Value = IIf(CTRL.Value = True, "X", "")
        Next CTRL
    End If
    End Sub
     
    Private Sub CommandButton3_Click()
    Unload Me
    End Sub

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Probléme sur un formulaire
    Par grizlie dans le forum Modélisation
    Réponses: 2
    Dernier message: 19/05/2007, 07h59
  2. Probléme sur un formulaire
    Par jojo57 dans le forum Access
    Réponses: 6
    Dernier message: 31/05/2006, 16h55
  3. Problème sur un formulaire
    Par flo64 dans le forum Access
    Réponses: 5
    Dernier message: 31/05/2006, 13h35
  4. [DatasheetBackColor ] problème sur un formulaire
    Par herendel dans le forum Access
    Réponses: 3
    Dernier message: 23/05/2006, 10h31
  5. problème sur un formulaire de modification
    Par puppusse79 dans le forum Access
    Réponses: 13
    Dernier message: 14/04/2006, 15h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo