创建与计算字段的关系

huangapple go评论70阅读模式
英文:

Creating a relationship with a calculated field

问题

我正在尝试在Access中的两个表格之间建立关系。一个表格里有顾客信息,另一个表格里有按邮政区域划分的地区信息。在顾客表格中,我有所有常规地址信息,还有一个简单的计算字段,该字段只包含国家代码和邮政编码的前两个字符。例如,英国,伯明翰的邮编为1,那么该字段的数值就是“UKB1”,而英国的赫尔地区则是“UKHU”。在另一个表格中,我们有这些四位字符代码(不像顾客表格中那样是计算得出的),以及一个地区编号和员工成员。

Access提示无法创建关系,原因是它来自一个计算字段。

在不必手动输入数据到顾客表格的情况下,我该如何解决这个问题呢?

英文:

I am trying to create a relationship between 2 tables in Access. In one table I have customers, in another I have areas by postal region. In the customers table I have all the usual address information and a simple calculated field which is just the country code and the first 2 characters of a post code. For example United Kingdom, Birmingham 1 would be "UKB1" and United Kingdom Hull would be "UKHU". In the other table we have these 4 character codes (not calculated like the customer table) alongside a regional number and member of staff.

Access is stating that it cannot create the relationship due to it coming from a calculated field.

How do I get around this without having to manually enter the data into the customer table?

答案1

得分: 1

我只会使用代码来维护额外的列。因此,在允许编辑该客户的表单中,只需在该表单上的控件的“后更新”事件中输入邮政编码或其他字段,将其前两个字符“推送”到该国家代码列中。

例如,像这样:

Private Sub PostalCode_AfterUpdate()
    Me!CountryCode = Left(Me.PostalCode, 2)
End Sub

现在,以上代码将自动处理任何编辑或新记录的输入。

但是,这并不解决现有记录的问题。对于这种情况,您需要运行一次更新查询,为现有数据“填充”这个新列。

因此,请启动查询生成器,添加该表,将查询类型更改为“更新查询”。

您会得到类似这样的内容:

UPDATE tblHotels SET CountryCode = Left([postalCode],2)

当然,您也可以按Ctrl-G进入命令(即时)窗口,并输入以下内容:

CurrentDb.Execute "UPDATE tblHotels SET CountryCode = Left([postalCode],2)"

所以,即时窗口或查询生成器。您只需添加该新列,并填充它。

正如注意到的那样,在您的编辑表单中与邮政编码相关联的简单“后更新”事件,那么该值将在以后的任何时间点得以保持,而不需要任何额外的努力。

现在,您有了一个普通的列,您可以自由地构建查询等,以允许加入第二个PostalRegion表格。我怀疑(怀疑)您是否需要强制执行引用完整性,但是无论如何,有了一个真正的国家代码列,您可以自由地加入该“区域”表格,以供您构建的任何查询中使用。

英文:

I would just use code to maintain the extra column. So, in the form(s) that allow edit of that customer, just put in the post code or whatever field a after update event for that control on that form that "shoves" into that country code column the first 2 characters.

For example, like this:

Private Sub PostalCode_AfterUpdate()

    Me!CountryCode = Left(Me.PostalCode, 2)


End Sub

Now, above will automatically take care of any edits, or new records you enter.

However, that does not address existing records. For that, you want to run an update query once to "fill out" this new column for existing data.

So, fire up query builder, add that table, and change query type to "update query".

So, you get/have something like this:

创建与计算字段的关系

Of course, you could also I suppose hit Ctrl-G to get to command (immediate) window, and type in this:

currentdb.Execute "UPDATE tblHotels SET CountryCode = Left([postalCode],2)"

So, immediate window, or query builder. You simple add that new column, and fill it out.

As noted, with the simple after update event attached to the post-code in your edit form, then the value will be thus maintained without any extra efforts from that point forward.

And, now that you have a plain column, then you are free to build queries etc. that will allow join of that 2nd PostalRegion table.

I suspect (doubt) that you need to enforce RI, but regardless, with a real living column of country code, then you are free to join in that "region" table for any query you build.

huangapple
  • 本文由 发表于 2023年3月3日 20:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定