简体   繁体   English

在MS Access中计算日期,然后将值写入表中的字段

[英]Calculate date in MS Access then write value to a field in a table

Following on from this question , how would I go about writing the resulting date value to the field ( DEADLINE (25 WD) ) which is located in a form? 这个问题继续 ,我将如何将结果日期值写入表单中的字段( DEADLINE (25 WD) )?

(The field is located in the form which is linked to a table.) (该字段位于链接到表格的表单中。)

The real solution is to not store this "calculation", but use it when and where required. 真正的解决方案是不存储此“计算”,而是在需要的时间和位置使用它。 Calculations belong to Queries not in Tables. 计算属于不在表中的查询。 The reason why it is best to use in Queries than storing in tables using Form method is because, when the DateOpened is changed the Deadline is automatically updated. 最好在查询中使用而不是使用Form方法存储在表中的原因是,当DateOpened更改时,截止日期会自动更新。 BUT this happens only if you are using the Form to edit the information. 但是只有当您使用表单来编辑信息时,这种情况才会发生。 If you are editing the date directly in the table or using a Query to Update the record, the Deadline will not be consistent. 如果直接在表中编辑日期或使用查询来更新记录,则截止日期将不一致。

However, if you use a query like 但是,如果您使用类似

SELECT 
    DateOpened, 
    addWorkDays(25, DateOpened) As Deadline 
FROM 
    yourTable;

This will work based on the DateOpened, as when the Query is run. 这将在运行查询时基于DateOpened起作用。 Even if it gets changed in the Query (while it is open), the Deadline will be updated accordingly. 即使在查询中将其更改(处于打开状态),截止日期也会相应地更新。 You loose this flexibility when you use a Form to store calculations back to the table. 当您使用窗体将计算结果存储回表时,将失去这种灵活性。

That being said , as your need is to calculate this based on another field; 话虽这么说 ,因为您需要的是基于另一个字段进行计算; I would suggest that you make use of the AfterUpdate event of the DateOpened control. 我建议您利用DateOpened控件的AfterUpdate事件。

Private sub DateOpened_AfterUpdate()
    If Len(Me.DateOpened & vbNullString) <> 0 Then 
        Me.DeadlineDate = addWorkDays(25, DateOpened)
    Else
        Me.DeadlineDate = ""
    End If
End Sub

Since the control is bound to the table, there will be no need to run an update code. 由于控件已绑定到表,因此无需运行更新代码。 This will automatically have the deadline filled in. 这将自动填写截止日期。

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

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