使用相同的字段和表组合多个查询

发布于 2025-01-10 22:20:25 字数 400 浏览 0 评论 0原文

我有一份订单信息表格。从表单上的组合框中选择订单号后,它将填充订单日期、产品信息、客户 ID 等字段(所有这些信息的来源是 OrderInfo 表)。我有一个单独的 CustomerInfo 表,其中包含客户 ID、姓名、地址...表单上有四个客户 ID 字段。我想根据客户 ID 字段自动显示姓名、地址...。 输入图片这里的描述

我对第一个客户进行了查询,以查找表单上的客户 ID 值并找到姓名和名称。地址。但我不想为四个客户 ID 字段编写四个查询。有没有什么方法可以将所有四个合而为一,或者有没有更好的方法来完成此过程?

I have a Form for order information. It will populate the fields order date, product info, Customer ID... after selecting the Order number from a combo box on the form(Source for all this information is OrderInfo table). I have a separate table for CustomerInfo which has Customer ID, name, address... There are four Customer ID fields on the form. I want to automatically display the name, address... based on Customer ID field.
enter image description here

I did a query for the first customer to look for the customer ID value on the form and find the name & address. But I do not want to write four queries for the four customer ID fields. Is there any way to combine all four into one or is there a way better to do this process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2025-01-17 22:20:25

正如 June7 评论的那样,八行 DLookup 代码可能是最快的解决方案。

我个人不喜欢使用 DLookup,并且我确信它们是比我建议的更优雅的解决方案,但下面是我尝试过的(并且我假设未绑定文本框)。

在表单上创建一个私有子例程:

Private Sub PopulateNameAddress(CustomerID As Long, CustomerNameTextBox As TextBox, CustomerAddressTextBox As TextBox)
    
'CustomerNameTextBox is a variable for the names of the text boxes containing the customer's name
'CustomerAddressTextBox is a variable for the names of the text boxes containing the customer's address
    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT CustomerName, CustomerAddress FROM CustomerInfo WHERE CustomerID=" & CustomerID)
    
        CustomerNameTextBox = rs!CustomerName
        CustomerAddressTextBox = rs!CustomerAddress
    
    rs.Close
    db.Close
    
    Set rs = Nothing
    Set db = Nothing
    
End Sub

然后在填充 CustomerID 后(如您提供的图像所示),调用此子例程,并为其提供 CustomerID、客户姓名的文本框名称以及客户的地址。运行一个循环可能会更好,但由于您只有四个记录要填充,这可能是最直接的:

    'Send the PopulateNameAddress subroutine the name of each text box containing the CustomerID, CustomerName, and CustomerAddress
    PopulateNameAddress Customer1IDTextBox, Customer1NameTextBox, Customer1AddressTextBox
    PopulateNameAddress Customer2IDTextBox, Customer2NameTextBox, Customer2AddressTextBox
    PopulateNameAddress Customer3IDTextBox, Customer3NameTextBox, Customer3AddressTextBox
    PopulateNameAddress Customer4IDTextBox, Customer4NameTextBox, Customer4AddressTextBox

不幸的是,我不确定您如何设置数据库,因此这可能不一定有效。让我知道。

As June7 commented, eight lines of DLookup code might be the quickest solution.

I don't personally like to use DLookup, and I'm sure their is a more elegant solution than what I'm suggesting, but below is what I tried (and I assumed unbound textboxes).

Create a private subroutine on the form:

Private Sub PopulateNameAddress(CustomerID As Long, CustomerNameTextBox As TextBox, CustomerAddressTextBox As TextBox)
    
'CustomerNameTextBox is a variable for the names of the text boxes containing the customer's name
'CustomerAddressTextBox is a variable for the names of the text boxes containing the customer's address
    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT CustomerName, CustomerAddress FROM CustomerInfo WHERE CustomerID=" & CustomerID)
    
        CustomerNameTextBox = rs!CustomerName
        CustomerAddressTextBox = rs!CustomerAddress
    
    rs.Close
    db.Close
    
    Set rs = Nothing
    Set db = Nothing
    
End Sub

Then after whatever populates the CustomerID (as shown in the image you provided), call this subroutine, and give it the CustomerID, the name of textbox for the customer's name, and the name of the textbox for the customer's address. Could be better running a loop, but since you only have four records to fill, this might be the most straight forward:

    'Send the PopulateNameAddress subroutine the name of each text box containing the CustomerID, CustomerName, and CustomerAddress
    PopulateNameAddress Customer1IDTextBox, Customer1NameTextBox, Customer1AddressTextBox
    PopulateNameAddress Customer2IDTextBox, Customer2NameTextBox, Customer2AddressTextBox
    PopulateNameAddress Customer3IDTextBox, Customer3NameTextBox, Customer3AddressTextBox
    PopulateNameAddress Customer4IDTextBox, Customer4NameTextBox, Customer4AddressTextBox

Unfortunately, I'm not sure how you have your database setup, so this may not necessarily work. Let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文