Dlookup 从表单中提取值以在条件部分中使用

问题描述 投票:0回答:1

我有一个表单和两个表:Tbl_CustomerShipToLocation、Tbl_MasterCustomerList 和 Frm_NewShipToLocation。

Tbl_CustomerShipToLocation

  • Customer_ID - 号码 - PK
  • Customer_Name - 短文本 - PK -(从 Tbl_MasterCustomerList 查找值;Customer_Name)
  • Ship_To_Location - 短文本 - PK

由于客户信息复杂,这是一场三重PK。我们有多家公司,根据他们订购的位置,对同一零件需要不同的流程。

Tbl_MasterCustomerList

  • Customer_ID - 号码 - PK
  • 客户名称 - 短文本
  • 联系人/CP电子邮件/CP电话号码(这些在我当前的表单斗争中没有使用。)

新送货地点的创建表格:

简单地保存记录总是将 Customer_Name 留空,因为它是链接到 Tbl_MasterCustomerList 中的 Customer_ID 的查找字段,但显示相应的 Customer_Name。
这是我的“保存表单”按钮的代码:

Private Sub Save_Form_Click()

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim CustID As Long
    Dim CustName As String
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("Tbl_CustomerShipToLocation")
    'Open Tbl_CustomerShipToLocation as recordset
    
    CustID = Me.cmbo_Customer_Name.Value
    
    CustName = DLookup("[Customer_Name]", "Tbl_MasterCustomerList", "[Customer_ID] =" & CustID)
    
    With rs
        .AddNew 'Add new record with values from Frm_NewShipToLocation
            ![Customer_Name] = CustName
            ![Customer_ID] = Me.txt_Customer_ID.Value
            ![Ship_To_Location] = Me.txt_Ship_To_Location.Value
        .Update
    End With
    
    rs.Close
    
    Set rs = Nothing
    Set db = Nothing
        
End Sub

VBA 正在拉动什么:

我明白为什么 by Customer_Name Combo 的价值正在吸引数字。

我不明白它是如何或为何总是提取不正确的 ID。例如,如果我使用 Customer_ID = 2072,VBA 会输入 70。

我没有 Customer_ID = 126 或 Customer_ID = 70 的条目。

vba ms-access ms-access-2016
1个回答
1
投票

您没有提供适当的最小可重现示例,因此我无法确定,但看起来您将 (cmb)CustomerName 组合框的默认列设置为错误的列。 既然你失去了理智,这里有一个完整的答案:

更深入地说,您的表没有正确规范化,您将需要多对多关系。如果联系人被解雇或者客户只有多个联系人怎么办?此外,最好查看和编辑表单和报告中的数据,因为快速在所有正确的表中输入数据变得非常容易出错并且非常耗时。此时,您可以查看表格以了解实际存在的内容,并且查找内容很模糊,因此删除所有表格查找并使用默认表单快速生成您需要的任何表单和报告的入门版本。

这是一个规范化表结构的示例:

接下来让我们利用默认表单来快速生成表单。在侧栏中,选择 CustomersShipToLocations,它是创建 Customers ShipToLOcations 多对多关系的联结表。选择表格后,使用功能区为表格创建表单(我使用表单向导来获取表格起始结构:

改进表单:例如(右键单击上下文菜单)将所有 id 文本框更改为组合框,然后将这些组合框设置为显示可读值而不是 id(我们在表单中使用查找)。例如,对于详细信息部分中的紫色 CustomerID(进入表单设计模式)并调整以下属性:

in the format tab: 
note the column count is 1 change to 2 

set the column widths to 0,1 
(hiding first column which is column 1 and will be set as the bound column)

in the data tab: 
note the bound column is 1 by default 

Set the record source to the Customers table where column 1 is the id
 the control source is CustomerID  which is that column 1
having a control source makes this a bound column box

in the other tab: I set name to cmbCustomerID
to distinguish the form control from the table field

与黄色的 CustomerName 组合框相比,我们需要一个未绑定的控件,这意味着控件值未绑定到任何数据

still a combobox so we will need 2 columns the ConsumerID and the readable Consumer Name 

under format 
set column count to 2 
set column widths to 0,1

under the data tab 
control source should be empty 

we add a row source where we will make sure the first two columns are our id column and a descriptive column to match our format.  Incidentally that is how I set up the first columns of the Customers Table so everything defaults in the right place. 

no controlsource so this combobox is unbound 
unbound means we can change the combobox value without change a value in the tables

如果你想要一个显示所选 ID 的文本框,请执行类似的操作

add a textbox like txtShowCustomerID and set it's control source to =[cmbSelectCustomer]

在此示例中,为了显示表中发生的情况,我保留了表单的详细信息部分及其表单记录源绑定控件,因此不需要按钮将记录插入到表单的记录源中,但这留下了许多除非我们做更多的表单改进,否则问题与直接编辑表格类似。通常,按钮是添加记录的好方法:MS Access 2016 - 用于向表中的多个字段添加值的多列列表框

Private Sub cmdAddCustomerShipToLocation_Click()
  Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Set db = CurrentDb
    Set rs = db.OpenRecordset("CustomersShipToLocations")
    With rs
        .AddNew
            rs!CustomerID = Me.cmbSelectCustomer
            rs!ShipToLocationID = Me.cmbSelectShipToLocation
        .Update
    End With
    rs.Close
    'The selected CustomerID is show with an unshown bound textbox(labels dont' change) 
   
   
    Set rs = Nothing
    Set db = Nothing
  Me.Requery 'show changes
End Sub

© www.soinside.com 2019 - 2024. All rights reserved.