简体   繁体   English

MS Access子表单数据输入

[英]MS Access sub-subform data entry

I am building a communication/task logging system which logs incoming communications and assigns tasks based on them. 我正在构建一个通信/任务记录系统,该系统记录传入的通信并根据它们分配任务。

My database has a notes system, so that the users can make notes about a variety of things - eg a customer record might have a note which says "This customer always orders their own window frame glass". 我的数据库有一个便笺系统,以便用户可以对各种事情做便笺-例如,客户记录中可能有一个便笺,上面写着“该客户总是订购自己的窗框玻璃”。 This is all handled via a single "Notes" table which has a PrimaryNoteTableID which links to a static lookup table which identifies the primary table which the note applies to, and an PrimaryTablePK field which stores the PK of the record within that table it applies to. 所有这些操作都通过一个单独的“ Notes”表进行处理,该表具有一个PrimaryNoteTableID和一个PrimaryTablePK字段,该静态查找表标识了该注释所适用的主表,该静态查找表标识了该注释所适用的主表, PrimaryTablePK字段存储了该表在该表中的记录。

In order to avoid corruption, it is advised to keep memo fields in a table of their own with a 1:1 relationship to their parent table ( see here ), which is what I have done. 为了避免损坏,建议将备注字段保留在自己的表中,该表与它们的父表保持1:1的关系( 请参阅此处 ),这是我所做的。

When adding new communications and tasks, the user will usually want to add the following: 在添加新的通信和任务时,用户通常将需要添加以下内容:

  • Details of the person the communication is with. 通讯对象的详细信息。
  • The subject of the communication. 交流的主题。
  • The type of action required and the deadline for that action. 所需的操作类型以及该操作的期限。
  • A note with details of what has been discussed etc. 详细讨论内容等的注释。

With that in mind I have: 考虑到这一点,我有:

tblCommTaskLog with the fields CommDate , ActionRequiredTypeID(FK) , ActionDeadlineDays , CommAccountID(FK) etc. tblCommTaskLog ,其字段为CommDateActionRequiredTypeID(FK)ActionDeadlineDaysCommAccountID(FK)等。

tblNote with: NoteID(PK) , PrimaryNoteTableID(FK) , PrimaryTablePK(FK) , EnteredByUserID(FK) , EntryDate . tblNote具有: NoteID(PK)PrimaryNoteTableID(FK) PrimaryTablePK(FK)EnteredByUserID(FK)EntryDate EnteredByUserID(FK)EntryDate

tblNoteText with NoteTextID(PK) , NoteID(FK) , NoteText(Memo) tblNoteTextNoteTextID(PK)NoteID(FK)NoteText(Memo)

Some of these are foreign keys to other tables which should be self-explanatory. 其中一些是其他表的外键,这些表应该是不言自明的。

Essentially what I'm running into problems with is the form for entry with these three tables. 本质上,我遇到问题的是这三个表的输入表单。 I find that the Access handling of subforms can be pretty stupid, such that: 我发现子窗体的Access处理可能非常愚蠢,例如:

  • You're locked into displaying things in a certain way if you want to use a subform, or a subform of a subform. 如果要使用子窗体或子窗体的子窗体,则只能以某种方式显示事物。
  • Access saves records before you've clicked to save. 单击保存之前,Access会保存记录。 It also saves records in subforms which can't be handled with a simple "me.undo" in the form's code and results in you having to find the record in the subform and delete it with code. 它还将记录保存在子窗体中,而这些子窗体无法通过窗体代码中的简单“ me.undo”进行处理,从而导致您不得不在子窗体中找到记录并使用代码将其删除。
  • The subforms act in stupid ways insofar as the way they assign autonumber PKs and pick those up in the links between child and master forms. 子表单在分配自动编号PK并在子表单和主表单之间的链接中进行选择的方式方面以愚蠢的方式起作用。

I'd like to just have the fields from tblCommTaskLog and the NoteText field on one form, so that the user fills in the details from tblCommTaskLog (ActionRequired etc.) and the note text, hits a "Save" button, and then via code the items are saved correctly, such that: 我想只是从田野tblCommTaskLogNoteText一种形式的领域,从而使从细节用户填写tblCommTaskLog (ActionRequired等)和注释文本,点击“保存”按钮,然后通过代码这些项目已正确保存,例如:

  • A new record is created in tblCommTaskLog with eg a PK of 72 (Autonumber). tblCommTaskLog创建一个新记录,例如PK为72 (自动编号)。
  • A new record is created in tblNote with a PrimaryNoteTableID of 4 which corresponds to tblCommTaskLog , a PrimaryTablePK of 72 and a PK NoteID eg 422 (Autonumber). 一个新的记录中创建tblNotePrimaryNoteTableID4对应于tblCommTaskLog ,一个PrimaryTablePK72和PK NoteID例如422 (自动编号)。
  • A new record is created in tblNoteText with a NoteID of 422. tblNoteText创建一个新记录,其NoteID为422。

I think what I need is a main form with the fields from tblCommTask with an unbound text field to take the input for NoteText . 我认为我需要的是一种主要形式,其中包含来自tblCommTask的字段以及未绑定的文本字段,以接收NoteText的输入。 I just can't figure out how to create a new record in tblNote , get the AutonumberPK from that field and insert it into the FK of tblNoteText such that everything is linked together correctly with the right FKs. 我只是不知道如何在tblNote创建新记录,从该字段中获取AutonumberPK并将其插入tblNoteText的FK中, tblNoteText使所有内容都与正确的FK正确链接在一起。 Can I do this with an SQL INSERT and then a SELECT TOP on the Autonumber field? 我可以使用SQL INSERT然后在Autonumber字段上使用SELECT TOP来做到这一点吗? Would I be better off doing this with Recordsets and Lastmodified? 使用Recordsets和Lastmodified这样做会更好吗? Is there some other method I'm not thinking of? 还有我没有想到的其他方法吗?

@whatEvil: @whatEvil:

you can still make it as 1:M. 您仍然可以将其设置为1:M。 if you are allowed to alter your primary tables, alter them and add 如果允许您更改主表,请对其进行更改并添加

  1. new column pk_guid : String(38). 新列pk_guid:String(38)。 (can also be 36 if you don't use {}) (如果您不使用{},也可以是36)
  2. Add a before insert trigger to automatically add a GUID for each new record. 添加一个插入前触发器以自动为每个新记录添加一个GUID。
  3. something like: for each rows before insert set pk_guid = get_uuid() 类似于:对于插入集之前的每一行,设置pk_guid = get_uuid()

in your tbl_note just have 在您的tbl_note中

  1. Note_id : pk note_id:pk
  2. origin_guid : string(38) fK origin_guid:字符串(38)fK
  3. notes : 注意事项:
  4. added_by add_by
  5. ..... .....

now you can simply enforce 1:M relationship. 现在您只需执行1:M关系即可。 GUID are "Globally Unique Identifier". GUID是“全局唯一标识符”。 All of your primary tables will produce an unique key where you can simply join them at any stage without thinking of which table it belongs to or how to save parent table name. 您所有的主表都将产生一个唯一键,您可以在任何阶段简单地将它们联接,而无需考虑它属于哪个表或如何保存父表名。

of course you need a custom function to get GUIDs: use this code to create one 当然,您需要一个自定义函数来获取GUID:使用此代码创建一个

Public Function GET_UUID() As String
    With CreateObject("Scriptlet.TypeLib")
        GET_UUID = VBA.Left(.GUID, 38)
    End With
End Function

Edit: The idea is to have a global unique id in each of your primary table (GUID/ UUID). 编辑:想法是在每个主表(GUID / UUID)中都有一个全局唯一ID。 Which then will be used to join your primary and note table. 然后将其用于连接您的主要表和注释表。 Note: you need to add a trigger (before insert) to each of your primary table to produce a GUID. 注意:您需要在每个主表中添加一个触发器(在插入之前),以产生一个GUID。 Also note, MS access call it DataMacro (check if your version support datamacro) more about datamacro : https://support.office.com/en-ca/article/Create-a-data-macro-b1b94bca-4f17-47ad-a66d-f296ef834200?ui=en-US&rs=en-CA&ad=CA 另请注意,MS访问将其称为DataMacro(检查您的版本是否支持datamacro)有关datamacro的更多信息: https : //support.office.com/en-ca/article/Create-a-data-macro-b1b94bca-4f17-47ad- a66d-f296ef834200?ui = zh-CN&rs = en-CA&ad = CA

why this way? 为什么这样呢? currently your primary tables are producing Auto-number it is possible that one or more table will have same ID number. 当前,您的主表正在生成自动编号,一个或多个表可能具有相同的ID号。 After all, autonumber is only unique throughout a table not throughout the entire database. 毕竟,自动编号仅在整个表中唯一,而不在整个数据库中唯一。 To avoid duplicating you are adding another key (table identification key) to identify to which primary table the foreign key belongs to. 为避免重复,您要添加另一个键(表标识键)以标识外键所属的主表。 this sounds all good until you make one wrong update to your primary table with wrong value to the note ID. 这听起来很不错,直到您对主表进行了一次错误的更新,并为便笺ID设置了错误的值。

by using GUID you will be able to simply maintain the 1:m relationship no matter how many primary tables are participating. 通过使用GUID,无论有多少个主表参与,您都可以简单地保持1:m关系。 Also I personally like the idea with GUID in case if your application grows and need real unique ids throughout your database. 我个人也喜欢GUID的想法,以防您的应用程序增长并且在整个数据库中需要真正的唯一ID。 its totally up to you which way you want to go, i just explained one method.. 这完全取决于您,您想走哪条路,我只是解释了一种方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM