【问题标题】:MS Access - Passing Data from a Subform to another FormMS Access - 将数据从子表单传递到另一个表单
【发布时间】:2023-03-11 01:33:01
【问题描述】:

我有一个用于搜索记录的主表单 (Form1),该表单的 子表单 包含所有搜索到的记录。 Form1 中无法编辑或添加数据。

我希望能够单击子表单中的记录并单击按钮,以将该特定记录复制到单独的表单 (Form2)。我想这样做,以便用户不必手动将数据从 Form1-subform 复制并粘贴到 Form2。

我尝试过连续形式,但它的最大宽度不够。我是 Access 新手,并试图找出解决此问题的方法。 任何人都可以提出这样做​​的方法。 TIA

【问题讨论】:

  • 相当常见的话题。你有什么尝试?为什么需要复制完整的记录?听起来数据结构可能还不够规范化。
  • 我有三个表,在不同时间输入数据。这些表之间具有相互关系(一对多关系)。表 1 的主键是表 2 的外键。同样,表 2 的主键是表 3 的外键。由于数据是在不同时间输入的,我希望能够链接新输入的数据。例如:如果我使用表格向 table3 输入数据,我希望能够将其链接到 table2 中的记录。因此,我只想复制问题中描述的主键,这样用户就不必手动输入它
  • 问题说“复制那个特定的记录”,而不仅仅是传递键值。几种方法来完成。一种是使用 OpenForm 方法的 OpenArgs。仍然,一个共同的话题。但实际上,最简单的方法是使用表单/子表单/子子表单排列进行数据输入/编辑。
  • 这能回答你的问题吗? Pass Variables From Access Form To Access Form

标签: vba ms-access subform ms-access-forms


【解决方案1】:

有几种方法可以做到这一点,我不知道正确的技术术语,但我希望它可以提供帮助;

选项 1:从子表单插入到连接到主表单的相关表中

示例: 表单数据:tblCustomer

子表单数据:tblPotentialCustomer

所以要将子表单中的数据添加到表单中,只需尝试以下操作:

    public sub Insert
    
    Dim strSQL as string
    Dim strPotentialCustomerName as string
    Dim strPotentialCustomerKey as string
    
strPotentialCustomerName  = me.subform.PotentialCustomerName
strPotentialCustomerKey = me.subform.PotentialCustomerKey

    strSQL = "Insert into tblCustomer (CustomerName,PotentialCustomerKey) Values ('"& strPotentialCustomerName &"','"& strPotentialCustomerKey  &"')"
    
    call Currentdb.execute(strSQL,dbseechanges)
    
    '--- BEWARE : In the mainform the new record is not automaticaly show, so you need an event to handle this.
        
    end sub

选项 2:直接使用 parent 将其插入主窗体

尝试到达相关对象,例如:

从您的子表单中:

me.parent!CustomerName = mySubform.PotentialCustomerName

希望这能让您朝着正确的方向前进。 万事如意!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多