简体   繁体   English

如何在 Access 表单中以多对多关系填充第三个表? (MySQL 后端)

[英]How can I populate the third table in a many-to-many relationship in an Access form? (MySQL backend)

Firstly, here is the relationship between the relevant tables.首先,这里是相关表之间的关系。

I'm developing an Access frontend to serve as a CRUD for an underlying MySQL database, and I'm most of the way there for properly representing and interacting with the tables in a many-to-many relationship.我正在开发一个 Access 前端,作为底层 MySQL 数据库的 CRUD,我主要是在多对多关系中正确表示表并与之交互。 I followed the instructions in this tutorial and my form is mostly functional.我按照本教程中的说明进行操作,我的表单大部分都可以使用。 But the tutorial example is limited in that the third table in the relationship (in my case, Addresses) is static, and cannot be changed from the form.但是本教程示例的局限性在于关系中的第三个表(在我的例子中,地址)是 static,并且不能从表格中更改。

Here's how my form looks currently.这是我的表单目前的外观。 When adding a new entry in the people_has_addresses subform, if I wish to add an address which already exists in the addresses table by entering its ID into the addresses_addressID column, everything works.在 people_has_addresses 子表单中添加新条目时,如果我希望通过将其 ID 输入到addresses_addressID 列中来添加地址表中已经存在的地址,一切正常。 The rest of the columns automatically load in the address, and people_has_addresses automatically populates a new row with this unique combination of PersonID and AddressID.列的 rest 自动加载到地址中,并且 people_has_addresses 使用 PersonID 和 AddressID 的这种唯一组合自动填充新行。 The problem comes when I want to create a new address on the fly, add it to the Addresses table, and populate people_has_addresses with this new combination.当我想动态创建一个新地址,将其添加到地址表中,并用这个新组合填充 people_has_addresses 时,问题就来了。 If I free-form type a new address into the subform and hit enter, I get the following error: "The Microsoft Access database engine cannot find a record in the table 'addresses' with key matching fields 'addresses_AddressID'."如果我在子表单中自由输入新地址并按回车键,我会收到以下错误:“Microsoft Access 数据库引擎无法在表 'addresses' 中找到具有关键匹配字段 'addresses_AddressID' 的记录。”

My question is, what do I need to do in order to allow the creation of new rows in Addresses?我的问题是,我需要做什么才能允许在地址中创建新行? Ideally working the way I describe above.理想情况下按照我上面描述的方式工作。 I feel that I'm close to getting this right, but don't know what to do from here.我觉得我快要做到这一点了,但不知道从这里该怎么做。

Ok, so the combo box in that row lets you select a given address.好的,所以该行中的组合框让您 select 给定地址。 You not really described that if you have 10 address,which one you want to appear say in a mailing list or what not.您并没有真正描述过,如果您有 10 个地址,您想在邮件列表中显示哪个地址,或者什么不是。 What you could do is add an extra column to the table where you shove in (save) the address id, and add a "default" check box.您可以做的是在您插入(保存)地址ID的表中添加一个额外的列,并添加一个“默认”复选框。 Then you can have a query with the default Address column = true to only pull the one address for mailing etc. So in this example, I do have a check box to select which out of possible many address are to be used by default.然后,您可以使用默认地址列 = true 进行查询,以仅提取一个地址用于邮寄等。因此,在此示例中,我确实有一个 select 复选框,默认情况下将使用许多地址。 And we could add another column for home, business, holiday location or whatever.我们可以为家庭、商业、度假地点或其他任何内容添加另一列。 That way you can categorize the address to what it is.这样,您可以将地址归类为它是什么。

However, the issue and your question is of course how to add a new address?但是,问题和您的问题当然是如何添加新地址?

There is as many ways to do this as their are flavors of ice cream.有很多方法可以做到这一点,就像冰淇淋的味道一样。 In other words, it up to your creative mind.换句话说,这取决于你的创造性思维。 One way would be to use the combo box not in list event.一种方法是使用不在列表事件中的组合框。 So if you typed in a address id that don't exist, you could be prompted to enter it.因此,如果您输入的地址 ID 不存在,系统可能会提示您输入。 However, users should never have to see, know, or use some silly "id" during data entry.然而,用户在数据输入过程中不应该看到、知道或使用一些愚蠢的“id”。 Users in fact should never even see PK or FK id's during use of the application.事实上,用户在使用应用程序期间甚至不应该看到 PK 或 FK id。

I would suggest you next to the combo box that lets you select a address is place a button to add a new address.我建议你在组合框旁边,让你 select 一个地址是放置一个按钮来添加一个新地址。 You could use a graphic image like a + sign.您可以使用 + 号之类的图形图像。 So, they can use the combo box to select an existing address, or click on on the + button to add a new address.因此,他们可以使用组合框来 select 一个现有地址,或者单击 + 按钮添加一个新地址。 What the code behind the button would do is launch a form to edit the address, but launch the form in add mode.按钮后面的代码会启动一个表单来编辑地址,但在添加模式下启动表单。 And when you close/save the form, then the code could/would stuff in the PK id of the new address you just added into the FK Address_ID column for the address.当您关闭/保存表单时,代码可以/将填充您刚刚添加到地址的 FK Address_ID 列中的新地址的 PK id。

And in the interest of cruelty to animals and your users?为了虐待动物和你的用户? They should not need to know or see or type in some "number".他们不需要知道或看到或输入一些“数字”。 The combo box can hide the first PK column.组合框可以隐藏第一个PK列。 So when you select a given address (in this example hotel, but it could be just a address) you display the name.因此,当您 select 给定地址(在此示例中为酒店,但它可能只是一个地址)时,您会显示名称。 So, users in place of expanding the combo box can just start typing in the hotel name.因此,用户无需展开组合框,只需开始输入酒店名称即可。 To be fair, with address, you really don't have a "name" to type in, so your case is somewhat different.公平地说,有了地址,你真的没有要输入的“名字”,所以你的情况有些不同。 In most cases the thing you are selecting has some name, or part name/number, or in our case a Hotel Name.在大多数情况下,您选择的东西有一些名称,或部件名称/编号,或者在我们的例子中是酒店名称。

Note that the sub form is based on a query that joins in the other columns.请注意,子表单基于连接其他列的查询。 So, when they crack open the combo box, they are in fact selecting a PK.所以,当他们破解组合框的时候,他们其实是在选择一个PK。 So like magic (and without code), then the city,province, description columns automatic display from that other table.就像魔术一样(并且没有代码),然后从另一个表中自动显示城市,省,描述列。

So the form would look like this:所以表格看起来像这样: 在此处输入图像描述

In above, we selected several hotels.在上面,我们选择了几家酒店。 However, note the check box as to which address to use, since we might have several, but for printing a letter or envelope, then the check box will determine which address will appear.但是,请注意要使用哪个地址的复选框,因为我们可能有多个,但对于打印信函或信封,复选框将确定将出现哪个地址。

And in the above, if we don't find or have a Hotel (or address) we want to use, then we can hit the + sign beside the combo box.在上面,如果我们没有找到或没有我们想要使用的酒店(或地址),那么我们可以点击组合框旁边的 + 号。 That would launch up a nice single form layout to enter a new Hotel (or address).这将启动一个很好的单一表单布局来输入新的酒店(或地址)。 As noted, how you setup your UI to work is quite much a open canvas.如前所述,如何设置 UI 以使其工作在很大程度上是一个开放的 canvas。 One great way to get ideas is to look at other software you use.获得灵感的一种好方法是查看您使用的其他软件。 So, how do you add multiple address to outlook users?那么,如何为 outlook 用户添加多个地址? So, a gazilion ways and ideas exist as to how you want to do this.所以,关于你想如何做到这一点,存在大量的方法和想法。 It not really how to do this, but HOW YOU want this to work, since you can design this to your hearts content to work quite much anyway you want.这并不是真正的如何做到这一点,而是你希望它如何工作,因为你可以根据自己的意愿设计它,让它随心所欲地工作。

So, the PK id should not be exposed here.所以,PK id 不应该在这里暴露。 Users can't care or know about such things.用户无法关心或知道这些事情。 I mean, as I am typing this response, no doubt the SQL server that drives stack overflow generated a new number for this post - but it not a user thing, only a developer thing to care about.我的意思是,当我输入这个回复时,毫无疑问,驱动堆栈溢出的 SQL 服务器为这篇文章生成了一个新数字——但这不是用户的事情,只是开发人员需要关心的事情。

Ok, so we build that sub form.好的,所以我们构建了那个子表单。 The table driving that sub form is only驱动那个子表的表只有

ID  (PK) 
Customer_ID   (FK to parent form driving this)
Hotel_ID      (FK to hotel - or in your case address)
DefaultAddress (true/false column)

And as noted, we should add ONE MORE column that sets the address as the default address to use.如前所述,我们应该添加 ONE MORE 列,将地址设置为要使用的默认地址。 eg: default address.例如:默认地址。

. . All of the other columns display as a result of a left join.所有其他列都显示为左连接的结果。 (a left join for this sub form is VERY important). (此子表单的左连接非常重要)。

Now, if a hotel is not in our list of choices, then the user can simply hit the + sign beside the combo box, and we pop up a nice form to edit/add a new hotel.现在,如果酒店不在我们的选择列表中,那么用户只需点击组合框旁边的 + 号,我们就会弹出一个漂亮的表单来编辑/添加新酒店。 And take well note that when you use outlook, an accounting system, or just about any software?请注意,当您使用 outlook、会计系统或任何软件时? You not seeing and typing in PK id values - that only really for you the developer to see, and end users should never see or have to deal with actual PK numbers.您没有看到和输入 PK id 值 - 只有开发人员才能看到,最终用户永远不会看到或必须处理实际的 PK 编号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何引用多对多关系的相交表? - How can I reference an intersection table of a many-to-many relationship? 如何优化具有多对多关系的 mysql COUNT() 查询(通过第 3 个表) - How can i optimize the mysql COUNT() query with many-to-many relationship(through the 3rd table) MySQL多对多关系表用法 - MySQL many-to-many relationship table usage MySQL查询多对多关系表 - MySQL query for a many-to-many relationship table MySQL - 如何插入具有多对多关系的表中 - MySQL - How to insert into table that has many-to-many relationship 如何通过Laravel中的多对多关系访问第三个表? - How do I access third table via Many-to-Many Relation in Laravel? 如何编写SQL查询以检查一个表上的多对多关系是否是另一个表上多对多关系的子集? - How do I write a SQL query to check if a many-to-many relationship on one table is a subset of a many-to-many relationship on another table? 具有多对多关系的递归关系 MySQL 表 - Recursive relationship MySQL table with many-to-many relationship Sqlalchemy:template_id和部门之间多对多关系的关联表。 如何删除关系? - Sqlalchemy : association table for many-to-many relationship between template_id and department. How can I delete a relationship? MySQL表关系布局(多对多含糊) - MySQL table relationship layout (many-to-many with ambiguity)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM