Stl
G sui débutant en vb, et j'ai pb: J'aimerais afficher ds 1 textbox le prix d'article après avoir sélectionné ce dernier ds 1 combobox.Aidez-moi, svp
Merci d'avance!!
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
Hervé
Bonjour Eric, En admettant que tes produits et prix soit dans une base de données, il y a un champ "Produit" et un champ "Prix". Il te faut cocher la référence "Microsoft ActiveX Data Objects 2.5 Library" (Projet|Références...). Mets ce code dans le module de ta Form, adapte et teste : ------------------------------------------------------ 'Un tableau est utilisé pour stocker les prix afin d'éviter les ouvertures/fermetures de la base à chaque clic dans le combo. Dim Tbl() As Double
Private Sub ConnecterBase(ConnectBD As ADODB.Connection, _ Optional Rs)
Set ConnectBD = New ADODB.Connection If Not IsMissing(Rs) Then Set Rs = New ADODB.Recordset End If
With ConnectBD .Provider = "Microsoft.Jet.OLEDB.4.0" 'ici changer le chemin de ta base .ConnectionString = "D:Produits.mdb" .Open End With End Sub
Private Sub LireTable() Dim ConnectBD As ADODB.Connection Dim Rs As ADODB.Recordset Dim ChaineSQL As String Dim I As Integer
ConnecterBase ConnectBD, Rs
ChaineSQL = "SELECT * FROM T_Produits"
With Rs .CursorType = adOpenKeyset .LockType = adLockOptimistic .Open ChaineSQL, ConnectBD Do Until .EOF Combo1.AddItem .Fields("Produit") I = I + 1 ReDim Preserve Tbl(1 To I) Tbl(I) = .Fields("Prix") .MoveNext Loop End With
ConnectBD.Close
Set ConnectBD = Nothing Set Rs = Nothing End Sub
Private Sub Combo1_Click() Text1 = Tbl(Combo1.ListIndex + 1) End Sub
Private Sub Form_Load() LireTable End Sub
---------------------------------------------------------------- Si tu n'utilise pas de base de données, tu peux utiliser un fichier texte dont voici un exemple :
Private Type Enregistrement Nom As String * 20 prix As Double End Type
Private Sub Command1_Click() 'proc servant juste pour le remplissage du fichier texte 'pour l'exemple Dim Enrg As Enregistrement Dim NF As Integer Dim I As Integer
NF = FreeFile
Open "D:Prix.txt" For Random As #NF Len = Len(Enrg) For I = 1 To 10 With Enrg .Nom = "Produit " & I .prix = I End With Put #NF, I, Enrg Next I Close #NF
End Sub
Private Sub Form_Load() Dim Enrg As Enregistrement Dim NF As Integer Dim I As Integer
NF = FreeFile
Open "D:Prix.txt" For Random As #NF Len = Len(Enrg) For I = 1 To LOF(NF) / Len(Enrg) Get #NF, I, Enrg Combo1.AddItem Enrg.Nom Next I Close #NF
End Sub
Private Sub Combo1_Click() Dim Enrg As Enregistrement Dim NF As Integer
NF = FreeFile
Open "D:Prix.txt" For Random As #NF Len = Len(Enrg) Get #NF, Combo1.ListIndex + 1, Enrg Close #NF
Text1 = Enrg.prix
End Sub
Hervé.
"Eric" a écrit dans le message news:
Stl! G sui débutant en vb, et j'ai pb: J'aimerais afficher ds 1 textbox le prix
d'article après avoir sélectionné ce dernier ds 1 combobox.Aidez-moi, svp.
Merci d'avance!!
Bonjour Eric,
En admettant que tes produits et prix soit dans une base de données, il y a
un champ "Produit" et un champ "Prix". Il te faut cocher la référence
"Microsoft ActiveX Data Objects 2.5 Library" (Projet|Références...). Mets ce
code dans le module de ta Form, adapte et teste :
------------------------------------------------------
'Un tableau est utilisé pour stocker les prix afin d'éviter les
ouvertures/fermetures de la base à chaque clic dans le combo.
Dim Tbl() As Double
Private Sub ConnecterBase(ConnectBD As ADODB.Connection, _
Optional Rs)
Set ConnectBD = New ADODB.Connection
If Not IsMissing(Rs) Then
Set Rs = New ADODB.Recordset
End If
With ConnectBD
.Provider = "Microsoft.Jet.OLEDB.4.0"
'ici changer le chemin de ta base
.ConnectionString = "D:Produits.mdb"
.Open
End With
End Sub
Private Sub LireTable()
Dim ConnectBD As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim ChaineSQL As String
Dim I As Integer
ConnecterBase ConnectBD, Rs
ChaineSQL = "SELECT * FROM T_Produits"
With Rs
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open ChaineSQL, ConnectBD
Do Until .EOF
Combo1.AddItem .Fields("Produit")
I = I + 1
ReDim Preserve Tbl(1 To I)
Tbl(I) = .Fields("Prix")
.MoveNext
Loop
End With
ConnectBD.Close
Set ConnectBD = Nothing
Set Rs = Nothing
End Sub
Private Sub Combo1_Click()
Text1 = Tbl(Combo1.ListIndex + 1)
End Sub
Private Sub Form_Load()
LireTable
End Sub
----------------------------------------------------------------
Si tu n'utilise pas de base de données, tu peux utiliser un fichier texte
dont voici un exemple :
Private Type Enregistrement
Nom As String * 20
prix As Double
End Type
Private Sub Command1_Click()
'proc servant juste pour le remplissage du fichier texte
'pour l'exemple
Dim Enrg As Enregistrement
Dim NF As Integer
Dim I As Integer
NF = FreeFile
Open "D:Prix.txt" For Random As #NF Len = Len(Enrg)
For I = 1 To 10
With Enrg
.Nom = "Produit " & I
.prix = I
End With
Put #NF, I, Enrg
Next I
Close #NF
End Sub
Private Sub Form_Load()
Dim Enrg As Enregistrement
Dim NF As Integer
Dim I As Integer
NF = FreeFile
Open "D:Prix.txt" For Random As #NF Len = Len(Enrg)
For I = 1 To LOF(NF) / Len(Enrg)
Get #NF, I, Enrg
Combo1.AddItem Enrg.Nom
Next I
Close #NF
End Sub
Private Sub Combo1_Click()
Dim Enrg As Enregistrement
Dim NF As Integer
NF = FreeFile
Open "D:Prix.txt" For Random As #NF Len = Len(Enrg)
Get #NF, Combo1.ListIndex + 1, Enrg
Close #NF
Text1 = Enrg.prix
End Sub
Hervé.
"Eric" <anonymous@discussions.microsoft.com> a écrit dans le message news:
B6082FF0-A3B0-4289-83D8-906A6E9C5D40@microsoft.com...
Stl!
G sui débutant en vb, et j'ai pb: J'aimerais afficher ds 1 textbox le prix
d'article après avoir sélectionné ce dernier ds 1 combobox.Aidez-moi, svp.
Bonjour Eric, En admettant que tes produits et prix soit dans une base de données, il y a un champ "Produit" et un champ "Prix". Il te faut cocher la référence "Microsoft ActiveX Data Objects 2.5 Library" (Projet|Références...). Mets ce code dans le module de ta Form, adapte et teste : ------------------------------------------------------ 'Un tableau est utilisé pour stocker les prix afin d'éviter les ouvertures/fermetures de la base à chaque clic dans le combo. Dim Tbl() As Double
Private Sub ConnecterBase(ConnectBD As ADODB.Connection, _ Optional Rs)
Set ConnectBD = New ADODB.Connection If Not IsMissing(Rs) Then Set Rs = New ADODB.Recordset End If
With ConnectBD .Provider = "Microsoft.Jet.OLEDB.4.0" 'ici changer le chemin de ta base .ConnectionString = "D:Produits.mdb" .Open End With End Sub
Private Sub LireTable() Dim ConnectBD As ADODB.Connection Dim Rs As ADODB.Recordset Dim ChaineSQL As String Dim I As Integer
ConnecterBase ConnectBD, Rs
ChaineSQL = "SELECT * FROM T_Produits"
With Rs .CursorType = adOpenKeyset .LockType = adLockOptimistic .Open ChaineSQL, ConnectBD Do Until .EOF Combo1.AddItem .Fields("Produit") I = I + 1 ReDim Preserve Tbl(1 To I) Tbl(I) = .Fields("Prix") .MoveNext Loop End With
ConnectBD.Close
Set ConnectBD = Nothing Set Rs = Nothing End Sub
Private Sub Combo1_Click() Text1 = Tbl(Combo1.ListIndex + 1) End Sub
Private Sub Form_Load() LireTable End Sub
---------------------------------------------------------------- Si tu n'utilise pas de base de données, tu peux utiliser un fichier texte dont voici un exemple :
Private Type Enregistrement Nom As String * 20 prix As Double End Type
Private Sub Command1_Click() 'proc servant juste pour le remplissage du fichier texte 'pour l'exemple Dim Enrg As Enregistrement Dim NF As Integer Dim I As Integer
NF = FreeFile
Open "D:Prix.txt" For Random As #NF Len = Len(Enrg) For I = 1 To 10 With Enrg .Nom = "Produit " & I .prix = I End With Put #NF, I, Enrg Next I Close #NF
End Sub
Private Sub Form_Load() Dim Enrg As Enregistrement Dim NF As Integer Dim I As Integer
NF = FreeFile
Open "D:Prix.txt" For Random As #NF Len = Len(Enrg) For I = 1 To LOF(NF) / Len(Enrg) Get #NF, I, Enrg Combo1.AddItem Enrg.Nom Next I Close #NF
End Sub
Private Sub Combo1_Click() Dim Enrg As Enregistrement Dim NF As Integer
NF = FreeFile
Open "D:Prix.txt" For Random As #NF Len = Len(Enrg) Get #NF, Combo1.ListIndex + 1, Enrg Close #NF
Text1 = Enrg.prix
End Sub
Hervé.
"Eric" a écrit dans le message news:
Stl! G sui débutant en vb, et j'ai pb: J'aimerais afficher ds 1 textbox le prix
d'article après avoir sélectionné ce dernier ds 1 combobox.Aidez-moi, svp.
Merci d'avance!!
Jean-Marc
"Eric" a écrit dans le message de news:
Stl! G sui débutant en vb, et j'ai pb: J'aimerais afficher ds 1 textbox le prix
d'article après avoir sélectionné ce dernier ds 1 combobox.Aidez-moi, svp.
Merci d'avance!!
Hello,
la question est très vague, mais voici une imlémentation possible. Utilisation : - mettre sur la forme un textBox et une combo, avecc la propriété style de la combo = Dropdown List. - Les données relatives à l'aticle doivent etre dans un fichier, au format: libelle, prix <CRLF> Principe: Le tableau d'articles est chargé en mémoire, on afiche dans la combo les libellés. Puis à chaque évènement clic du combo, on va chercher dans le tableau d'articles le prix correspondant à l'article sélectionné. Limitations: c'est un mini exemple, qui ne gère pas les erreurs, la recherche dans le tableau ne tient pas compte des doublons, la recherche est faite de façon brutale, etc. Ca illustre juste le principe:
Code: ----------------------------------
Option Explicit
Const NB_ART = 1000 Private Type Tarticle libelle As String prix As Single End Type
Dim fic_db As String Dim article(1 To NB_ART) As Tarticle
Private Function dbLookup(t() As Tarticle, key As String) As Single Dim n As Long
For n = LBound(t) To UBound(t) If t(n).libelle <> "" Then If t(n).libelle = key Then dbLookup = t(n).prix Exit Function End If Else Exit For End If Next n End Function
Private Sub Combo1_Click() Text1.Text = dbLookup(article, Combo1.Text) End Sub
Private Sub Form_Load() Dim f As Integer Dim n As Integer Dim i As Integer
fic_db = "c:db_art.art"
If Not existeFic(fic_db) Then MsgBox "The db file " & fic_db & " can not be found." Else f = FreeFile Open fic_db For Input As #f While Not EOF(f) n = n + 1 Input #f, article(n).libelle, article(n).prix Wend Close #f For i = 1 To n Combo1.AddItem article(i).libelle Next i Combo1.ListIndex = 0 End If End Sub
Private Function existeFic(filename As String) As Boolean Dim f As Integer On Error GoTo existeFic_PB
f = FreeFile Open filename For Input As #f Close #f existeFic = True existeFic_End: Exit Function existeFic_PB: existeFic = False Resume existeFic_End: End Function
--------------------
Bonne prog,
Jean-marc
"Eric" <anonymous@discussions.microsoft.com> a écrit dans le message de
news:B6082FF0-A3B0-4289-83D8-906A6E9C5D40@microsoft.com...
Stl!
G sui débutant en vb, et j'ai pb: J'aimerais afficher ds 1 textbox le prix
d'article après avoir sélectionné ce dernier ds 1 combobox.Aidez-moi, svp.
Merci d'avance!!
Hello,
la question est très vague, mais voici une imlémentation possible.
Utilisation :
- mettre sur la forme un textBox et une combo, avecc la propriété style de
la combo = Dropdown List.
- Les données relatives à l'aticle doivent etre dans un fichier, au format:
libelle, prix <CRLF>
Principe:
Le tableau d'articles est chargé en mémoire, on afiche dans la combo les
libellés.
Puis à chaque évènement clic du combo, on va chercher dans le tableau
d'articles le prix correspondant à l'article sélectionné.
Limitations:
c'est un mini exemple, qui ne gère pas les erreurs, la recherche dans le
tableau ne tient pas compte des doublons, la recherche est faite de façon
brutale, etc. Ca illustre juste le principe:
Code:
----------------------------------
Option Explicit
Const NB_ART = 1000
Private Type Tarticle
libelle As String
prix As Single
End Type
Dim fic_db As String
Dim article(1 To NB_ART) As Tarticle
Private Function dbLookup(t() As Tarticle, key As String) As Single
Dim n As Long
For n = LBound(t) To UBound(t)
If t(n).libelle <> "" Then
If t(n).libelle = key Then
dbLookup = t(n).prix
Exit Function
End If
Else
Exit For
End If
Next n
End Function
Private Sub Combo1_Click()
Text1.Text = dbLookup(article, Combo1.Text)
End Sub
Private Sub Form_Load()
Dim f As Integer
Dim n As Integer
Dim i As Integer
fic_db = "c:db_art.art"
If Not existeFic(fic_db) Then
MsgBox "The db file " & fic_db & " can not be found."
Else
f = FreeFile
Open fic_db For Input As #f
While Not EOF(f)
n = n + 1
Input #f, article(n).libelle, article(n).prix
Wend
Close #f
For i = 1 To n
Combo1.AddItem article(i).libelle
Next i
Combo1.ListIndex = 0
End If
End Sub
Private Function existeFic(filename As String) As Boolean
Dim f As Integer
On Error GoTo existeFic_PB
f = FreeFile
Open filename For Input As #f
Close #f
existeFic = True
existeFic_End:
Exit Function
existeFic_PB:
existeFic = False
Resume existeFic_End:
End Function
Stl! G sui débutant en vb, et j'ai pb: J'aimerais afficher ds 1 textbox le prix
d'article après avoir sélectionné ce dernier ds 1 combobox.Aidez-moi, svp.
Merci d'avance!!
Hello,
la question est très vague, mais voici une imlémentation possible. Utilisation : - mettre sur la forme un textBox et une combo, avecc la propriété style de la combo = Dropdown List. - Les données relatives à l'aticle doivent etre dans un fichier, au format: libelle, prix <CRLF> Principe: Le tableau d'articles est chargé en mémoire, on afiche dans la combo les libellés. Puis à chaque évènement clic du combo, on va chercher dans le tableau d'articles le prix correspondant à l'article sélectionné. Limitations: c'est un mini exemple, qui ne gère pas les erreurs, la recherche dans le tableau ne tient pas compte des doublons, la recherche est faite de façon brutale, etc. Ca illustre juste le principe:
Code: ----------------------------------
Option Explicit
Const NB_ART = 1000 Private Type Tarticle libelle As String prix As Single End Type
Dim fic_db As String Dim article(1 To NB_ART) As Tarticle
Private Function dbLookup(t() As Tarticle, key As String) As Single Dim n As Long
For n = LBound(t) To UBound(t) If t(n).libelle <> "" Then If t(n).libelle = key Then dbLookup = t(n).prix Exit Function End If Else Exit For End If Next n End Function
Private Sub Combo1_Click() Text1.Text = dbLookup(article, Combo1.Text) End Sub
Private Sub Form_Load() Dim f As Integer Dim n As Integer Dim i As Integer
fic_db = "c:db_art.art"
If Not existeFic(fic_db) Then MsgBox "The db file " & fic_db & " can not be found." Else f = FreeFile Open fic_db For Input As #f While Not EOF(f) n = n + 1 Input #f, article(n).libelle, article(n).prix Wend Close #f For i = 1 To n Combo1.AddItem article(i).libelle Next i Combo1.ListIndex = 0 End If End Sub
Private Function existeFic(filename As String) As Boolean Dim f As Integer On Error GoTo existeFic_PB
f = FreeFile Open filename For Input As #f Close #f existeFic = True existeFic_End: Exit Function existeFic_PB: existeFic = False Resume existeFic_End: End Function