【问题标题】:MS Access event-driven Data Macro to update table (example)MS Access 事件驱动数据宏更新表(示例)
【发布时间】:2013-06-10 20:42:23
【问题描述】:

我已将一些数据从 Excel 导入 Access。现在我想编写一个类似于触发器的代码,每当我从 Excel 中获取新数据时,它将更新另一个表。

我知道我无法在 Access 中编写触发器,因此我正在尝试使用数据宏。谁能帮助我如何使用数据宏执行此操作?

【问题讨论】:

  • excel数据是怎么添加的?复制和粘贴还是使用宏或 VBA?
  • 从 Access 导入选项
  • 我唯一知道的是如何使用BeforeUpdate 在表单上执行某些操作...我不知道如何使用宏来执行此操作,抱歉。

标签: ms-access ms-access-2010 ms-access-2013 ms-access-data-macro


【解决方案1】:

假设您有一个名为 [Events] 的表,并且您正在从 Excel 导入数据并将其附加到表中

ID  EventName              EventType           EventDate 
--  ---------------------  ------------------  ----------
 1  New Staff Orientation  Training: in-house  2013-06-07
 2  TGIF barbecue lunch    Social              2013-06-14

假设您还有一个名为 [EventTypes] 的表来跟踪可以分配给事件的类别。来自 Excel 数据的 [EventType] 值需要经过批准,以避免不必要的重复、拼写错误等。您的 [EventTypes] 表如下所示

EventType           Added                Approved
------------------  -------------------  -------------------
Training: in-house  2013-06-01 09:15:33  2013-06-01 09:37:16
Social              2013-06-07 10:01:23  2013-06-07 10:22:00

您可以在 [Events] 表上创建“插入后”数据宏,以将新的 [EventType] 值插入到 [EventTypes] 表中,如下所示:

    SetLocalVar
名称  未找到
表达式  = 真

    在 EventTypes 中查找记录
条件=[EventTypes].[EventType]=[Events].[EventType]

        SetLocalVar
名称  未找到
表达式  = 假

    如果  [未找到]  那么

        在 EventTypes 中创建记录

            SetField
名称 EventTypes.EventType
值  = [Events].[EventType]

            SetField
名称  EventTypes.Added
值  = 现在()

    结束如果

现在,如果您使用新的 EventType 导入事件...

ID  EventName              EventType           EventDate 
--  ---------------------  ------------------  ----------
 1  New Staff Orientation  Training: in-house  2013-06-07
 2  TGIF barbecue lunch    Social              2013-06-14
 3  Bathtub races          Team Building       2013-06-15

...数据宏会自动将其添加到 EventTypes 表中

EventType           Added                Approved
------------------  -------------------  -------------------
Training: in-house  2013-06-01 09:15:33  2013-06-01 09:37:16
Social              2013-06-07 10:01:23  2013-06-07 10:22:00
Team Building       2013-06-11 08:38:37          

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2018-07-26
    相关资源
    最近更新 更多