Problem recherche clef double !

theyas Messages postés 10 Date d'inscription samedi 13 mai 2006 Statut Membre Dernière intervention 4 octobre 2008 - 27 mai 2008 à 20:09
JPGVB Messages postés 96 Date d'inscription mardi 29 mars 2005 Statut Membre Dernière intervention 10 mars 2011 - 28 mai 2008 à 17:10
Salut a tout les developpeur
je veut verifier si une enregistrement et deja dans ma base de donnee
si non en va enregistrer a nouveaux
mais jai une erreur : (Run-time error "3221")
voila la source
------------------------------------------------------------------------------------------
Private Sub Command1_Click()

Set RST = New ADODB.Recordset
sql "select * from stock where code_stock" & Val(Text1)

RST.Open sql, CNX, adOpenKeyset, adLockOptimistic

RST.MoveFirst

While Not RST.EOF
    If RST!code_stock = Text1 Then
    MsgBox " deja !"

    Else

    RST.AddNew
  With RST
    !code_stock = Text1
    !date_achat = Text2
    !qte_stock = Text3
    !code_med = Text4
    !nom_med = Text5
    !code_for = Text6
    !nom_for = Text7
    !prix = Text8
    .Update
    MsgBox " Bien enregistrer !"

  End With

Text1.SetFocus
End If
RST.MoveNext
Wend
RST.Close

End Sub
----------------------------------------------------------------------------

2 réponses

alosamoelle Messages postés 129 Date d'inscription jeudi 28 octobre 2004 Statut Membre Dernière intervention 23 mai 2009 1
27 mai 2008 à 20:23
Bonjour,
si je ne plante pas le probeme dans ta boucle c'est que pour chaque ligne tu testes et tu ajoutes aussitôt la nouvelle variable donc le tour suivant tu essayes de la rajouter de nouveau.
Il faudrait 
If RST!code_stock = Text1 Then
    Exite = 1

et ensuite a la fin de ton wend mettre if exite = 1 then msgbox "existe" else tu ecris la nouvelle donnee
0
JPGVB Messages postés 96 Date d'inscription mardi 29 mars 2005 Statut Membre Dernière intervention 10 mars 2011 2
28 mai 2008 à 17:10
Bonjour, les petites modifications ci-dessous devraient régler ton pb :
Private Sub Command1_Click()

Set RST = New ADODB.Recordset
sql "select * from stock where code_stock" & Val(Text1)

RST.Open sql, CNX, adOpenKeyset, adLockOptimistic

if rst.eof and rst.bof then
    With RST
      .addnew
       !code_stock = Text1
       !date_achat = Text2
       !qte_stock = Text3
       !code_med = Text4
       !nom_med = Text5
       !code_for = Text6
       !nom_for = Text7
       !prix = Text8
       .Update
   end with
    MsgBox " Enregistrement ajouté!"
   Text1.SetFocus
End If

End Sub
0
Rejoignez-nous