vba-Access-2013-Tabellen als Datensätze und die Verwendung der Daten
Ich habe ein Formular, das zur Eingabe von Daten. Ich habe ein Listenfeld, das eine Liste aller Produkte. Ich habe auch ein zweites Listenfeld, das alle Unternehmen. Ich habe eine Tabelle für Kunden, die Ihre Namen und einer auto-ID. Ich habe eine Produkt-Liste enthält außerdem einen Namen und eine auto-ID. Ich habe eine Dritte Tabelle, die auflistet, was die Kunden haben die Produkte.
Beispiel:
tblCustomer
1 Company1
2 Company2
3 Company3
tblProducts
1 Product1
2 Product2
3 Product3
tblCustomerProducts
1 1 2 years
1 2 3 years
2 3 2 years
Also heißt es 1 ist die Firma, 1 ist das Produkt, und Sie haben es für 2 Jahre
Nun, wenn ich den Eintrag Formular-ich werde versuchen, öffnen Sie ein recordset für die beiden Tabellen und die Schleife Durchlaufen, und dann, wenn es eine übereinstimmung findet, wird die entsprechende Zahl in das entsprechende Textfeld ein. Dies ist, was ich haben
Private Sub Command15_Click()
Dim db As Database
Dim rst As Recordset 'Gets a variable ready to put a table into
Dim rst2 As Recordset 'Gets second variable ready to put a table into
Set db = CurrentDb()
Set rst = db.OpenRecordset("customer") 'Sets variable rst to the contents of the customer table
Set rst2 = db.OpenRecordset("product") 'Sets variable rst2 to the contents of the product table
Do While Not rst.EOF 'Loop through the customer table until it gets to the end of the file
If rst!["customer_name"] = lstCustomerName Then 'If the contents of the field customer_name is equal to that of the company highlighted on the form
txtCustomerFX.Value = rst!["id"] 'Then make the value of the the CustomerFX box equal to the ID associated with that company
rst.MoveNext
Loop
rst.Close
Do While Not rst2.EOF 'Loop through the product table until it gets to the end of the file
If rst2!["product_name"] = lstProductName Then 'If the contents of the field product_name is equal to that of the product highlighted on the form
txtProductFX.Value = rst2!["id"] 'Then make the value of the the ProductFX box equal to the ID associated with that product
rst.MoveNext
Loop
rst2.Close
End Sub
Scheint es nicht zu sein, wenn die Daten in die Textfelder, wenn.
Sie müssen möglicherweise beenden Ihr Wenn..Dann Blöcke mit End If. VB nur erlaubt, Ihre Ausgrenzung, wenn es auf der gleichen Linie.
Verwenden Sie korrekte Einrücken auf Ihre if-Anweisung und beenden Sie mit
End If
.Vielen Dank. Ich habe es. Info geholfen
InformationsquelleAutor jordankoal | 2014-01-28
Du musst angemeldet sein, um einen Kommentar abzugeben.
Sie brauchen nicht zu Tauchen in die recordsets zu-match-display die Namen und ids wie diese. Eine combobox oder listbox gebunden werden können, um eine versteckte Spalte mit ids und nur die Namen anzeigen. Benutzer-Namen, die Datenbank sieht zahlen.
Mit dem Steuerelement-Assistenten aktiviert ist (Standardwert) versuchen Sie, erstellen Sie eine neue combobox, die auf Ihrer Kreuzung bilden. Wählen Sie:
tblCustomer
id
undcustomer_name
customer_name
Diese legt die Eigenschaften der combobox zum speichern der ID in seinem Wert, aber nur Anzeige der Namen der Benutzer. Sie können binden Sie dieses Feld, direkt auf das Feld Kunde auf die Zuordnungstabelle und dann beseitigen Sie den code (und den button) alle zusammen. Wiederholen Sie für das Produkt, auch!
InformationsquelleAutor pteranodon