[vba access] Editer une ligne de table

Résolu
stikmou08 Messages postés 37 Statut Membre -  
stikmou08 Messages postés 37 Statut Membre -
Bonjour à tous,

j'ai un petit soucis d'édition de ma table en code. En effet j'utilise un recordset pour editer ma table, mais le problème, c'est qu'il édite la premier ligne de ma table et non celle voulue.
Jevous met mon code :
Set RST2 = CurrentDb.OpenRecordset("STOCK", dbOpenTable)
Set RST3 = CurrentDb.OpenRecordset("select code,reference,taille from STOCK where code = '" & Me.TextBox4 & "' and reference = '" & Me.TextBox7 & "' and taille = '" & Me.ComboBox0 & "'")


        If RST3.RecordCount = 0 Then
            With RST2
                .AddNew
                .Fields("code") = Me.TextBox4
                .Fields("reference") = Me.TextBox7
                .Fields("taille") = Me.ComboBox0
                    If Me.ComboBox1 = "E" Then
                        .Fields("quantite_entre") = Me.TextBox2
                    ElseIf Me.ComboBox1 = "S" Then
                        .Fields("quantite_sortie") = Me.TextBox2
                    End If
                .Fields("quantite_totale") = .Fields("quantite_entre").Value - .Fields("quantite_sortie").Value
                .Update
            End With
        Else
            With RST2
                .Edit
                If Me.ComboBox1 = "E" Then
                    .Fields("quantite_entre") = Me.TextBox2 + .Fields("quantite_entre").Value
                ElseIf Me.ComboBox1 = "S" Then
                    .Fields("quantite_sortie") = Me.TextBox2 + .Fields("quantite_sortie").Value
                End If
                    .Fields("quantite_totale") = .Fields("quantite_entre").Value - .Fields("quantite_sortie").Value
                .Update
            End With
        End If


Donc c'est quand il rentre dans la boucle Else, with RST2, .edit que ca ne fonctionne pas. Du moins ca fonctionne, car il edite bien la premiere ligne, mais moi je voudrai qu'il edite la ligne qu'il a trouver dans le recordset 3 (RST3)
Je ne sais pas si je suis très clair. Merci de votre compréhension.

1 réponse

  1. Polux31 Messages postés 7219 Statut Membre 1 204
     
    Bonjour,

    A aucun moment tu utilises RST3 !!!

    Je ne vois pas bien à quoi il sert d'ailleurs ... Ôo ....

    1
    1. stikmou08 Messages postés 37 Statut Membre 2
       
      Je l'utilise pour ma boucle If
      If RST3.RecordCount = 0 Then

      Il sert à savoir si une ligne de ma table avec les information de la textbox4, textbox7, combobox0 existe ou non.
      0
    2. Polux31 Messages postés 7219 Statut Membre 1 204
       
      Oui ça j'ai compris. Mais ensuite ???

      Je ferai comme ça :

      Set RST3 = "CurrentDb.OpenRecordset("select * from STOCK where code = '" & Me.TextBox4 & "' and reference = '" & Me.TextBox7 & "' and taille = '" & Me.ComboBox0 & "'")


      Et ensuite :

      If RST3.RecordCount <> 0 Then
                  With RST3
                      .Edit
                      If Me.ComboBox1 = "E" Then
                          .Fields("quantite_entre") = Me.TextBox2 + .Fields("quantite_entre").Value
                      ElseIf Me.ComboBox1 = "S" Then
                          .Fields("quantite_sortie") = Me.TextBox2 + .Fields("quantite_sortie").Value
                      End If
                          .Fields("quantite_totale") = .Fields("quantite_entre").Value - .Fields("quantite_sortie").Value
                      .Update
                  End With
              RST3.Close
              Set RST3 = Nothing
      
        Else
      
              Set RST2 = CurrentDb.OpenRecordset("STOCK", dbOpenTable)
      
              With RST2
                      .AddNew
                      .Fields("code") = Me.TextBox4
                      .Fields("reference") = Me.TextBox7
                      .Fields("taille") = Me.ComboBox0
                          If Me.ComboBox1 = "E" Then
                              .Fields("quantite_entre") = Me.TextBox2
                          ElseIf Me.ComboBox1 = "S" Then
                              .Fields("quantite_sortie") = Me.TextBox2
                          End If
                      .Fields("quantite_totale") = .Fields("quantite_entre").Value - .Fields("quantite_sortie").Value
                      .Update
                  End With
                  RST2.Close
                  Set RST2 = Nothing
      End If
      0
    3. stikmou08 Messages postés 37 Statut Membre 2
       
      Ca fonctionne très bien, je te remercie :)
      0