aymane19
Messages postés4Date d'inscriptionmardi 31 janvier 2012StatutMembreDernière intervention 1 février 2012
-
11 mai 2008 à 15:02
aymane19
Messages postés4Date d'inscriptionmardi 31 janvier 2012StatutMembreDernière intervention 1 février 2012
-
11 mai 2008 à 22:25
Bonjour à tout le monde;
j'utilise ce code pour insérer dans la table,
le champs NUM_COMPTE est une clé de la table.
je veux retourner un message en cas de saisie d'un élément qui existe déja
*****************************
Public Sub AddNewCompte()
Dim cnn1 As ADODB.Connection
Dim rstCompte As ADODB.Recordset
Dim strCnn As String
Dim strCodeCompte As String
Dim strLibelleCompte As String
Dim booRecordAdded As Boolean
' Open a connection.
Set cnn1 = New ADODB.Connection
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Compta_v4.mdb;Persist Security Info=False"
cnn1.Open strCnn
' Open COMPTE table.
Set rstCompte = New ADODB.Recordset
rstCompte.CursorType = adOpenKeyset
rstCompte.LockType = adLockOptimistic
rstCompte.Open "COMPTE", cnn1, , , adCmdTable
' Get data from the user.
strCodeCompte = Trim(TxtCodeCompte)
strLibelleCompte = Trim(txtIntitule)
' Proceed only if the user actually entered something
' for both the CodeCompte and LibelleCompte.
If (strCodeCompte <> "") Then
rstCompte.AddNew
rstCompte!NUM_COMPTE = strCodeCompte
rstCompte!LIBELLE = strLibelleCompte
rstCompte.Update
booRecordAdded = True
' Show the newly added data.
MsgBox "New record: " & rstCompte!NUM_COMPTE & " " & _
rstCompte!LIBELLE & " "
Else
MsgBox "SVP entez le numéro du compte, " & _
" et l'intitulé du compte"
End If
' Delete the new record because this is a demonstration.
'cnn1.Execute "DELETE FROM employee WHERE emp_id = '" & strCodeCompte & "'"
aymane19
Messages postés4Date d'inscriptionmardi 31 janvier 2012StatutMembreDernière intervention 1 février 2012 11 mai 2008 à 22:25
Ceci est le code amélioré
******************************************
Public Sub AddNewCompte()
Dim cnn1 As ADODB.Connection
Dim rstCompte As ADODB.Recordset
Dim strCnn As String
Dim strCodeCompte As String
Dim strLibelleCompte As String
Dim booRecordAdded As Boolean
' Open a connection.
Set cnn1 = New ADODB.Connection
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\dahmani\Bureau\Nouveau dossier (2)\compta_v4.mdb;Persist Security Info=False"
cnn1.Open strCnn
Set rstCompte = New ADODB.Recordset
rstCompte.CursorType = adOpenKeyset
rstCompte.LockType = adLockOptimistic
'Get data from the user.
strCodeCompte = Trim(TxtCodeCompte)
strLibelleCompte = Trim(txtIntitule)
If strCodeCompte "" Or strLibelleCompte "" Then
MsgBox "SVP entez le numéro du compte, " & _
" et l'intitulé du compte"
Exit Sub
End If
If strCodeCompte <> "" And strLibelleCompte <> "" Then
' Open COMPTE table.
Set rstCompte = New ADODB.Recordset
rstCompte.CursorType = adOpenKeyset
rstCompte.LockType = adLockOptimistic
'ouverture de la table avec essais d'au moins une entrée deja existante dans la table rstCompte.Open "COMPTE Where NUM_COMPTE '" & strCodeCompte & "' Or LIBELLE '" & strLibelleCompte & "'", cnn1, , , adCmdTable
' "select * from commande where code ='" & dt.Columns(0).Text & "'"
If Not rstCompte.EOF Then
' une ligne d'enregistrement correspond a au moins un des 2 champs
Dim Msg$ If rstCompte!NUM_COMPTE strCodeCompte Then Msg$ strCodeCompte
If rstCompte!LIBELLE = strLibelleCompte Then
If Msg$ = "" Then
Msg$ = strLibelleCompte
Else
Msg$ = Msg$ & "et " & strLibelleCompte
End If
End If
rstCompte.Close
cnn1.Close
Msg$ = Msg$ & vbCrLf & "existe deja"
MsgBox Msg$, vbCritical, "Doublon"
Exit Sub
End If
End If
'd'ici on est sûr que pas de doublon, pas de prposition vide
'rstCompte.Open "COMPTE", cnn1, , , adCmdTable
rstCompte.AddNew
rstCompte!NUM_COMPTE = strCodeCompte
rstCompte!LIBELLE = strLibelleCompte
rstCompte.Update
booRecordAdded = True
' Show the newly added data.
MsgBox "New record: " & rstCompte!NUM_COMPTE & " " & _
rstCompte!LIBELLE & " "
rstCompte.Close
cnn1.Close
End Sub
PCPT
Messages postés13272Date d'inscriptionlundi 13 décembre 2004StatutMembreDernière intervention 3 février 201847 11 mai 2008 à 17:21
salut,
tu t'embêtes à rien, çà se configure dans la base, pas par code...
ps :
' Proceed only if the user actually entered something
' for both the CodeCompte and LibelleCompte.
attention tu ne testes que pour un des 2 :)
++
<hr size="2" width="100%" />Prenez un instant pour répondre à [infomsg_SONDAGE-POP3-POUR-CS_769706.aspx ce sondage] svp
aymane19
Messages postés4Date d'inscriptionmardi 31 janvier 2012StatutMembreDernière intervention 1 février 2012 11 mai 2008 à 21:52
Salut PCPT;
merci pour ta réponse, c'est correcte pourtant, comment récupérer le message d'erreur retourné par le SGBD dans le cas d'une saisie double.