Access 2013 - 启动带有预填充外键的“新记录"表单 [英] Access 2013 - launch ‘new record’ form with foreign key pre-populated

查看:63
本文介绍了Access 2013 - 启动带有预填充外键的“新记录"表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Microsoft Access 2013 数据库包含三个主表:主供应商表、产品表和产品的从表,如果产品是矩阵(多种变体,如颜色)而不是单个独立产品.

My Microsoft Access 2013 DB contains three main tables: A primary Suppliers table, a Products table, and a slave table to the products if a product is a matrix (multiple variations, like colours) instead of a single standalone product.

我已经使用本教程使用 Access 2013 创建了一个相当不错的界面:https://www.youtube.com/watch?v=4ei0PFrDUa0 创建一个从供应商填充的下拉列表,以限制显示为来自该供应商的产品.我还能够在嵌入式产品数据表 (https://www.youtube.com/watch?v=GopSdt4QNck) 为任何产品启动一个表单来编辑该产品(链接而不是按钮,因为数据表不能包含按钮).

I have already created a rather nice interface with Access 2013 using this tutorial: https://www.youtube.com/watch?v=4ei0PFrDUa0 to create a drop-down populated from the Suppliers to limit the products displayed as being from that supplier. I have also been able to create a link within the embedded products datasheet (https://www.youtube.com/watch?v=GopSdt4QNck) to launch for any one product a form to edit that product (a link instead of a button because datasheets cannot contain buttons).

我现在的难点是,我想在嵌入式产品表单下方放置一个按钮,该按钮不仅可以启动用于添加新产品的空白表单,还可以使用供应商的 ID 预先填充产品的外键从下拉菜单中.

My difficulty now is that I want to put beneath the embedded products form a button that not only launches a blank form for adding a new product, but also to pre-populate the foreign key of the product with the ID of the supplier from the drop-down menu.

所以请记住,我现在有三种形式,我想添加第四种.一种表格是包含下拉菜单的整体供应商"表格.该下拉菜单修改了其中的嵌入表单(产品显示"),以仅显示从下拉列表中选择的供应商的产品.嵌入表单的行仅显示有关产品的两个最重要的信息(我们内部使用的代码来识别它,以及它的名称)和一个链接.此链接与该产品相关联,并启动一个产品编辑"表单,该表单使用该特定产品预填充表单以进行编辑(并包含该表中的所有详细信息).

So keep in mind I now have three forms, and I want to add a fourth. One form is the overall "Suppliers" form which contains a drop-down. That drop-down modifies an embedded form within it ("Products Display") to show only those products that are for that supplier selected from the drop-down. The embedded form has got rows that show only the two most important pieces of information about the product (the code we use in-house to identify it, and its name) and a link. This link is tied to that product and launches a "Product Edit" form that pre-populates the form with that specific product for editing (and has all the details from that table).

这就是三种形式.我现在想向第一个表单(带有下拉菜单的表单)添加一个按钮,该按钮启动类似于产品编辑"表单的产品创建"表单,但仅包含基本字段和外键(供应商 ID) 预填充到单击按钮之前下拉列表中选择的最后一个供应商.这意味着每次使用下拉菜单时,按钮都必须动态更改.

So those are the three forms. I want to now add a button to the first form (the one with the drop-down) that launches a "Product Create" form similar to the "Product Edit" form, but only with the essential fields, and with the foreign key (the Supplier ID) pre-filled to the last supplier selected in the drop-down before the button was clicked. This means that the button must dynamically change every time the drop-down is used.

问题是,我对如何在 Access 2013 中执行此操作一无所知.使用带有向导的标准按钮确实允许我生成一个新的产品创建"表单,但无法预填充仅使用向导本身的外键.另外,我不知道如何让按钮在每次使用下拉菜单时自动修改.

Problem is, I haven’t the foggiest notion how to do this within Access 2013. Using a standard button with the wizard does allow me to spawn a new "Product Create" form, but there is no way to pre-populate the foreign key using just the wizard itself. Plus, I do not know how to have the button auto-modify each and every time the drop-down is used.

推荐答案

让我看看能不能解释清楚.

Lets see if I can explain this clearly.

假设你有两个表格,附有表格:

Suppose you have two forms, with attached tables:

  • 父表单
  • 子表单

每个附表定义为:

家长:

ParentID (AutoNumber)
ParentName Text

孩子:

ChildID (AutoNumber)
ParentID Long Integer
ParentName Text

每个表单都显示了它的所有字段.父表单有一个按钮来添加子表单.

Each form has all of its fields displayed. The parent Form has a button to add a child.

父表单上的 VBA 代码是:

VBA Code on Parent Form is:

Private Sub AddButton_Click()
  DoCmd.OpenForm FormName:="Children", view:=acNormal, OpenArgs:=Me.ParentID
End Sub

子表单上的 VBA 代码是:

VBA Code on Child Form is:

Private Sub Form_Dirty(Cancel As Integer)
  Me.ParentID = Me.OpenArgs
End Sub

那么这是如何工作的呢?我们通过 OpenArgs 参数将 ParentID 传递给 Child 表单.(http://msdn.microsoft.com/en-us/library/office/ff836583.aspx)

So how does this work? We are passing the ParentID to the Child form via the OpenArgs parameter. (http://msdn.microsoft.com/en-us/library/office/ff836583.aspx)

在修改任何记录之前,会触发 Dirty 事件(当用户在新记录中键入第一个字符时).我们定义了 Dirty 事件来设置 ParentID.

Before any record is modified, the Dirty event is triggered (when the user types the first character into the new record). We defined the Dirty event to set the ParentID.

理论上,您可以填充多个外键或值.您需要将它们打包成一个分隔的字符串,然后在孩子的 onDirty 事件中将它们解包.

Theoretically, you can populate more than one foreign key or value. You would need to pack them into a delimited string, then unpack them on the child's onDirty event.

希望有所帮助.

这篇关于Access 2013 - 启动带有预填充外键的“新记录"表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆