【发布时间】:2014-01-29 23:35:11
【问题描述】:
我有一个用于数据输入的表格。我有一个包含所有产品列表的列表框。我还有一个包含所有公司的第二个列表框。我有一张客户表,上面有他们的名字和一个自动 ID。我有一个产品列表,其中还包含名称和自动 ID。我有第三张表,列出了客户拥有哪些产品。
例子:
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
所以这意味着 1 是公司,1 是产品,他们拥有 2 年
现在,当我输入表格时,我试图为两个表打开一个记录集并循环遍历它,然后当它找到匹配项时,它会将相应的数字放在相应的文本框中。这就是我所拥有的
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
它似乎并没有将数据放入文本框中。
【问题讨论】:
-
请务必阅读选择标签时弹出的有用说明。
-
您可能必须使用 End If 终止您的 If..Then 块。 VB 只允许在同一行排除它们。
-
在 if 语句中使用适当的缩进并用
End If终止它。 -
非常感谢。我知道了。信息帮助
标签: ms-access vba ms-access-2013