【问题标题】:vba Access 2013 tables as recordsets and using the datavba Access 2013 表作为记录集并使用数据
【发布时间】: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


【解决方案1】:

您无需像这样深入研究记录集来匹配显示名称和 ID。组合框或列表框可以绑定到隐藏的 id 列并仅显示名称。用户看到名字,数据库看到数字。 打开控制向导(默认设置)后,尝试在您的联结表单上创建一个新的组合框。选择:

  • 选择“我希望组合框从另一个表或查询中获取值”
  • 选择tblCustomer
  • 添加idcustomer_name
  • customer_name排序
  • 确保选中隐藏键列框
  • 选择标签

这将设置组合框的属性以将 ID 存储在其值中,但仅向用户显示名称。您可以将此字段直接绑定到联结表上的客户字段,然后一起消除代码(和按钮)。对产品也重复一遍!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-29
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多