Vba access pb de connection bdd et pb requète
Résolu
ffmanfan
-
ffmanfan Messages postés 25 Date d'inscription Statut Membre Dernière intervention -
ffmanfan Messages postés 25 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
J'ai un problème avec ce code il ne fonctionnent pas. je travail sur access 2007 pour un projet qui sera sur access 2003 et j'utilise un livre sur le vba access 2002.
Voilà le code:
Private Sub cmdEnreg_Click()
Dim db As Connection
DoCmd.Hourglass True
Set db = CurrentProject.Connection
db.Execute " INSERT INTO TblProduit VALUES LibProduit = " & txtNomPdt.Text & " , N°Catégorie = (SELECT N°Catégorie FROM TblCatégorieProduit WHERE LibCatégorie = " & lstCategorie.Text & " ) , Conditionnement = " & txtConditionnement.Text & " , N°Fournisseur = ( SELECT N°Fournisseur FROM TblFournisseur WHERE RaisonSociale = " & LstFournisseur.Text & " ) , RéfFournisseur = " & txtRefFourn.Text & " , Commentaire = " & txtCommentaire.Text & " ; "
db.Execute " INSERT INTO TblTarifs VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Année = " & txtAnnée.Text & " , DateActualisation = " & txtDateActu.Text & " , DateDébut = " & txtDateDébut & " , DateFin = " & txtDateFin & " Commentaire = " & txtCommentaire.Text
If lstCategorie.Text = "Parquet" Then
If lstTypeParquet.Text = "Massif" Then
db.Execute " INSERT INTO TblParquet VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Essence = ( SELECT idNatureBois FROM tblNatureBois WHERE NatureBois = " & lstNatureBois.Text & " ) , Epaisseur = " & txtEpaisseurBois.Text & " , Longueur = " & txtLongueur.Text & " , Largeur = " & txtLargeur.Text & " , Bordure = " & txtBordure.Text & " , Commentaire = " & txtCommentaire.Text & " ; "
End If
If lstTypeParquet.Text = "Contre Colé" Then
db.Execute " INSERT INTO TblParquet VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Essence = ( SELECT idNatureBois FROM tblNatureBois WHERE NatureBois = " & lstNatureBois.Text & " ) , Epaisseur = " & txtEpaisseurBois.Text & " , Longueur = " & txtLongueur.Text & " , Largeur = " & txtLargeur.Text & " , NombreDeFrise = " & txtNbFrise.Text & " , Commentaire = " & txtCommentaire.Text & " ; "
End If
End If
If lstCategorie.Text = "Abrasif" Then
If lstTypeAbrasif.Text = "Rouleau" Then
db.Execute " INSERT INTO TblAbrasif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , EpaisseurGrain = " & txtEpaisseurGrain.Text & " , Longueur = " & txtLongueur.Text & " , Largeur = " & txtLargeur.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstTypeAbrasif.Text = "Disque" Then
db.Execute " INSERT INTO TblAbrasif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , EpaisseurGrain = " & txtEpaisseurGrain.Text & " , Diamètre = " & txtDiamètre.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstTypeAbrasif.Text = "Bande sans fin" Then
db.Execute " INSERT INTO TblAbrsif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , EpaisseurGrain = " & txtEpaisseurGrain.Text & " , Longueur = " & txtLongueur.Text & " , Largeur = " & txtLargeur.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstTypeAbrasif.Text = "Grille" Then
db.Execute " INSERT INTO TblAbrasif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Diamètre = " & txtDiamètre.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstTypeAbrasif.Text = "Pad" Then
db.Execute " INSERT INTO TblAbrasif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Diamètre = " & txtDiamètre.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
End If
If lstCategorie.Text = "Liquide" Then
db.Execute " INSERT INTO TblLiquide VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , TypeLiquide = ( SELECT N°Type FROM TblTypeLiquide WHERE LibTypeLiquide = " & lstTypeLiquid.Text & " , Effet = " & txtEffet.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstCategorie.Text = "Outillage" Then
db.Execute " INSERT INTO TblOutillage VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , N°Employé = ( SELECT idEmploye FROM tblEmployes WHERE NomEmploye = " & lstEmploye.Text & " , DateAquisition = " & txtDate.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
End Sub
Merci d'avance
J'ai un problème avec ce code il ne fonctionnent pas. je travail sur access 2007 pour un projet qui sera sur access 2003 et j'utilise un livre sur le vba access 2002.
Voilà le code:
Private Sub cmdEnreg_Click()
Dim db As Connection
DoCmd.Hourglass True
Set db = CurrentProject.Connection
db.Execute " INSERT INTO TblProduit VALUES LibProduit = " & txtNomPdt.Text & " , N°Catégorie = (SELECT N°Catégorie FROM TblCatégorieProduit WHERE LibCatégorie = " & lstCategorie.Text & " ) , Conditionnement = " & txtConditionnement.Text & " , N°Fournisseur = ( SELECT N°Fournisseur FROM TblFournisseur WHERE RaisonSociale = " & LstFournisseur.Text & " ) , RéfFournisseur = " & txtRefFourn.Text & " , Commentaire = " & txtCommentaire.Text & " ; "
db.Execute " INSERT INTO TblTarifs VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Année = " & txtAnnée.Text & " , DateActualisation = " & txtDateActu.Text & " , DateDébut = " & txtDateDébut & " , DateFin = " & txtDateFin & " Commentaire = " & txtCommentaire.Text
If lstCategorie.Text = "Parquet" Then
If lstTypeParquet.Text = "Massif" Then
db.Execute " INSERT INTO TblParquet VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Essence = ( SELECT idNatureBois FROM tblNatureBois WHERE NatureBois = " & lstNatureBois.Text & " ) , Epaisseur = " & txtEpaisseurBois.Text & " , Longueur = " & txtLongueur.Text & " , Largeur = " & txtLargeur.Text & " , Bordure = " & txtBordure.Text & " , Commentaire = " & txtCommentaire.Text & " ; "
End If
If lstTypeParquet.Text = "Contre Colé" Then
db.Execute " INSERT INTO TblParquet VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Essence = ( SELECT idNatureBois FROM tblNatureBois WHERE NatureBois = " & lstNatureBois.Text & " ) , Epaisseur = " & txtEpaisseurBois.Text & " , Longueur = " & txtLongueur.Text & " , Largeur = " & txtLargeur.Text & " , NombreDeFrise = " & txtNbFrise.Text & " , Commentaire = " & txtCommentaire.Text & " ; "
End If
End If
If lstCategorie.Text = "Abrasif" Then
If lstTypeAbrasif.Text = "Rouleau" Then
db.Execute " INSERT INTO TblAbrasif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , EpaisseurGrain = " & txtEpaisseurGrain.Text & " , Longueur = " & txtLongueur.Text & " , Largeur = " & txtLargeur.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstTypeAbrasif.Text = "Disque" Then
db.Execute " INSERT INTO TblAbrasif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , EpaisseurGrain = " & txtEpaisseurGrain.Text & " , Diamètre = " & txtDiamètre.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstTypeAbrasif.Text = "Bande sans fin" Then
db.Execute " INSERT INTO TblAbrsif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , EpaisseurGrain = " & txtEpaisseurGrain.Text & " , Longueur = " & txtLongueur.Text & " , Largeur = " & txtLargeur.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstTypeAbrasif.Text = "Grille" Then
db.Execute " INSERT INTO TblAbrasif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Diamètre = " & txtDiamètre.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstTypeAbrasif.Text = "Pad" Then
db.Execute " INSERT INTO TblAbrasif VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , Diamètre = " & txtDiamètre.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
End If
If lstCategorie.Text = "Liquide" Then
db.Execute " INSERT INTO TblLiquide VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , TypeLiquide = ( SELECT N°Type FROM TblTypeLiquide WHERE LibTypeLiquide = " & lstTypeLiquid.Text & " , Effet = " & txtEffet.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
If lstCategorie.Text = "Outillage" Then
db.Execute " INSERT INTO TblOutillage VALUES N°Produit = ( SELECT N°Produit FROM TblProduit WHERE LibProduit = " & txtNomPdt.Text & " ) , N°Employé = ( SELECT idEmploye FROM tblEmployes WHERE NomEmploye = " & lstEmploye.Text & " , DateAquisition = " & txtDate.Text & " , Commentaire = " & txtCommentaire & " ; "
End If
End Sub
Merci d'avance
A voir également:
- Vba access pb de connection bdd et pb requète
- Gmail connection - Guide
- Check cable connection - Forum Matériel & Système
- L'indice n'appartient pas à la sélection vba - Forum VB / VBA
- Facebook connection - Guide
- Acer quick access - Forum logiciel systeme
34 réponses
Y'a un truc qui ne va pas marcher, c'est db.execute, qui ne s'applique qu'à un objet de type database, donc je pense qu'un des problèmes vient de la définition de la base courante, que veux-tu faire avec "CurrentProject.Connection" ?
Si tu veux travailler sur la base courante, il faut déclarer "db as database" et mettre "set db=currentdb", ensuite tu pourras exécuter des requêtes...
Si tu veux travailler sur la base courante, il faut déclarer "db as database" et mettre "set db=currentdb", ensuite tu pourras exécuter des requêtes...
Salut,
si tu commençais par nous dire ce qui ne fonctionne pas : quel est le message d'erreur et à quelle ligne ? plutôt que de déposer 100 lignes de code...
si tu commençais par nous dire ce qui ne fonctionne pas : quel est le message d'erreur et à quelle ligne ? plutôt que de déposer 100 lignes de code...
Merci pour la connection, mais maintenant C la ligne pour db.execute qui affiche:
Erreur d'exécution '2185':
Impossible de faire référence à une propriété ou de la définir pour un contrôle si ce dernier n'est pas activé.
La ligne de code est la suivante:
db.Execute " INSERT INTO TblProduit VALUES LibProduit = " & txtNomPdt.Text & " , N°Catégorie = (SELECT N°Catégorie FROM TblCatégorieProduit WHERE LibCatégorie = " & lstCategorie.Text & " ) , Conditionnement = " & txtConditionnement.Text & " , N°Fournisseur = ( SELECT N°Fournisseur FROM TblFournisseur WHERE RaisonSociale = " & LstFournisseur.Text & " ) , RéfFournisseur = " & txtRefFourn.Text & " , Commentaire = " & txtCommentaire.Text & " ; "
merci d'avance
Erreur d'exécution '2185':
Impossible de faire référence à une propriété ou de la définir pour un contrôle si ce dernier n'est pas activé.
La ligne de code est la suivante:
db.Execute " INSERT INTO TblProduit VALUES LibProduit = " & txtNomPdt.Text & " , N°Catégorie = (SELECT N°Catégorie FROM TblCatégorieProduit WHERE LibCatégorie = " & lstCategorie.Text & " ) , Conditionnement = " & txtConditionnement.Text & " , N°Fournisseur = ( SELECT N°Fournisseur FROM TblFournisseur WHERE RaisonSociale = " & LstFournisseur.Text & " ) , RéfFournisseur = " & txtRefFourn.Text & " , Commentaire = " & txtCommentaire.Text & " ; "
merci d'avance
je crois que l'action 'execute' met entre () ses arguments
Erreur de syntaxe.
par contre je ne vois pas où car le curseur se place sur INTO. A moin que ce ne soit les & qui posent problème?
voilà le code:
db.Execute (INSERT INTO TblProduit VALUES LibProduit = " & txtNomPdt.Text & " , N°Catégorie = (SELECT N°Catégorie FROM TblCatégorieProduit WHERE LibCatégorie = " & lstCategorie.Text & ") , Conditionnement = " & txtConditionnement.Text & " , N°Fournisseur = (SELECT N°Fournisseur FROM TblFournisseur WHERE RaisonSociale = " & LstFournisseur.Text & ") , RéfFournisseur = " & txtRefFourn.Text & " , Commentaire = " & txtCommentaire.Text & " ;)
Merci d'avance
par contre je ne vois pas où car le curseur se place sur INTO. A moin que ce ne soit les & qui posent problème?
voilà le code:
db.Execute (INSERT INTO TblProduit VALUES LibProduit = " & txtNomPdt.Text & " , N°Catégorie = (SELECT N°Catégorie FROM TblCatégorieProduit WHERE LibCatégorie = " & lstCategorie.Text & ") , Conditionnement = " & txtConditionnement.Text & " , N°Fournisseur = (SELECT N°Fournisseur FROM TblFournisseur WHERE RaisonSociale = " & LstFournisseur.Text & ") , RéfFournisseur = " & txtRefFourn.Text & " , Commentaire = " & txtCommentaire.Text & " ;)
Merci d'avance
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Bonjour,
L'argument doit être de type string pour la commande Execute. Il faut donc la placer entre double cote (" "). Ensuite les insertions de type String doivent être entre simple cote (' ') dans une instruction SQL. Seules les insertions de type numérique n'en ont pas besoin.
;o)
polux
L'argument doit être de type string pour la commande Execute. Il faut donc la placer entre double cote (" "). Ensuite les insertions de type String doivent être entre simple cote (' ') dans une instruction SQL. Seules les insertions de type numérique n'en ont pas besoin.
db.Execute ("INSERT INTO TblProduit VALUES LibProduit = ' " & txtNomPdt.Text & " ' , N°Catégorie = _ (SELECT N°Catégorie FROM TblCatégorieProduit WHERE LibCatégorie = ' " & lstCategorie.Text & " ') _ , Conditionnement = ' " & txtConditionnement.Text & "' , N°Fournisseur = _ (SELECT N°Fournisseur FROM TblFournisseur WHERE RaisonSociale = ' " & LstFournisseur.Text & " ') , _ RéfFournisseur = ' " & txtRefFourn.Text & " ' , Commentaire = ' " & txtCommentaire.Text & "' ;")
;o)
polux
Bonjour,
Je retombe sur l'erreur 2185:
Impossible de faire référence à une propriété ou de la définir pour un contrôle si ce dernier n'est pas activé.
Je retombe sur l'erreur 2185:
Impossible de faire référence à une propriété ou de la définir pour un contrôle si ce dernier n'est pas activé.
voilà ce que j'ai trouvé sur le site de support Microsoft:
https://support.microsoft.com/fr-fr/help/466677
;o)
https://support.microsoft.com/fr-fr/help/466677
;o)
Merci pour cette aide.
Je débute en vba et je suis mal réveillé même a 15H.
Et je pense avoir trouver un moyen plus simple.
Je vous tient au courant.
Je débute en vba et je suis mal réveillé même a 15H.
Et je pense avoir trouver un moyen plus simple.
Je vous tient au courant.
bonjour,
Mon code marche en partie. Mais le code sql ne fonctionne toujours pas. snif.
voilà le code:
db.Execute ("INSERT INTO TblTarfs (N°Produit, Année, DateActualisation, PrixHT, DateDébut, DateFin, Commentaire) VALUES ((SELECT N°Produit FROM TblProduit WHERE LibProduit=" & txtNomPdt.Text & "), " & txtAnnée.Text & ", " & txtDateActu.Text & ", " & txtPrix.Text & ", " & txtDateDébut.Text & ", " & txtDateFin.Text & ", " & txtCommentaire.Text & ");")
Mon code marche en partie. Mais le code sql ne fonctionne toujours pas. snif.
voilà le code:
db.Execute ("INSERT INTO TblTarfs (N°Produit, Année, DateActualisation, PrixHT, DateDébut, DateFin, Commentaire) VALUES ((SELECT N°Produit FROM TblProduit WHERE LibProduit=" & txtNomPdt.Text & "), " & txtAnnée.Text & ", " & txtDateActu.Text & ", " & txtPrix.Text & ", " & txtDateDébut.Text & ", " & txtDateFin.Text & ", " & txtCommentaire.Text & ");")
relis le post 8 ... les insertions de type String (texte) doivent être encadrées d'une simple cote ('papa') ... et les types Date avec (# 12/12/2007 #) ... seules les types numérique ne sont pas encadrées.
db.Execute ("INSERT INTO TblTarfs (N°Produit, Année, DateActualisation, PrixHT, DateDébut, DateFin, Commentaire) VALUES ((SELECT N°Produit FROM TblProduit WHERE LibProduit='" & txtNomPdt.Text & "'), " & txtAnnée.Text & ", #" & txtDateActu.Text & "#, " & txtPrix.Text & ", #" & txtDateDébut.Text & "#, #" & txtDateFin.Text & "#, '" & txtCommentaire.Text & "');")
Comme sa ça me fait la même chose.
Comme sa ça me fait la même chose.
C parce qu'en faite j'utilise une liste déroulante ou j'affiche un libelle mais le pb C que je doit récupérer l'identifiant du libelle d'où le select dans le insert into.
Mais qu'est ce que je raconte je suis encore sur l'ancien programme.
Là C parce que j'utilise un numéro automatique pour N°Produit dans le table TblProduit mais elle est relié a une table TblTarifs qui a le même identifiant que le produit ainsi que l'année car le prix peut changer tout les ans.
Là C parce que j'utilise un numéro automatique pour N°Produit dans le table TblProduit mais elle est relié a une table TblTarifs qui a le même identifiant que le produit ainsi que l'année car le prix peut changer tout les ans.