区分更新和MS Access Form.AfterUpdate中的插入

时间:2019-02-22 21:29:12

标签: vba ms-access access-vba

我在MS Access中有一个绑定表格。我想能够告诉:

  1. 插入新记录时
  2. 更新现有记录时

我认为事件Form.AfterUpdateForm.AfterInsert对此很合适,但是我刚刚测试了Form.AfterUpdate,发现它在更新和插入之后触发。

是否可以通过Form_AfterUpdate()来区分更新和插入之间的区别?还是我应该考虑使用另一种方法来检测Access中的这些事件?

Private Sub Form_AfterInsert()
    Debug.Print "after insert"
End Sub

Private Sub Form_AfterUpdate()
    Debug.Print "after update"
End Sub

当我插入新记录时,将打印此记录:

after update
after insert

当我更新现有记录时,将打印以下内容:

after update

2 个答案:

答案 0 :(得分:0)

如果您真的只需要知道这是新记录承诺还是现有记录承诺,请使用$事件设置一个模块级变量,然后可以在 @override Widget build(BuildContext context){ return new Scaffold( appBar : LBAppBar().getAppBar(), //drawer: new LBDrawer().getDrawer(), body: Container( decoration: BoxDecoration( gradient: new LinearGradient( colors: [Color.fromRGBO(1,89,99, 1.0), Colors.grey], begin: Alignment.bottomLeft, end: Alignment.topRight ) ), child: SwipeDetector( onSwipeRight: () { Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new WidgetsPage()) ); }, child: Column( mainAxisAlignment: MainAxisAlignment.center, children:[ Row( children: [ Container( margin: EdgeInsets.only(left: 20.0,top: 10.0, bottom: 10.0, right:30.0), child: Column( children: <Widget>[ Text("Hi ${user.firstName}, Today is " + formatDate(), style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.bold, fontSize: 19.0 )), ], ), ), ] ), Row( //ROW 1 children: [ Container( margin: EdgeInsets.only(left: 30.0,top: 60.0, bottom: 30.0, right:30.0), child: Column( children: <Widget>[ GestureDetector( child: Icon( FontAwesomeIcons.checkSquare, size: 50.0, color: Colors.white70, ), onTap: () { Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new CheckIn())); }), Text("Check In", style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.normal )) ], ), ), Container( margin: EdgeInsets.only(left: 50.0,top: 60.0, bottom: 30.0, right:30.0), child: Column( children: <Widget>[ GestureDetector( child: Icon( FontAwesomeIcons.list, size: 50.0, color: Colors.white70, ), onTap: () { Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new DayAtAGlance())); }), Text("My Day", style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.normal )) ], ), ), Container( margin: EdgeInsets.only(left: 30.0,top: 60.0, bottom: 30.0, right:30.0), child: Column( children: <Widget>[ GestureDetector( child: Icon( FontAwesomeIcons.phone, size: 45.0, color: Colors.white70, ), onTap: () { Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new CommunicationLinks())); }), Text("Communication", style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.normal )) ], ), ), ] ), Row(//ROW 2 children: [ Container( margin: EdgeInsets.only(left: 32.0,top: 50.0, bottom: 30.0), child: Column( children: <Widget>[ GestureDetector( child: Icon( FontAwesomeIcons.dollarSign, size: 50.0, color: Colors.white70, ), onTap: () { Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new Budget())); }), Text("Budget", style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.normal )) ], ), ), Container( margin: EdgeInsets.only(left: 75.0, top: 50.0, bottom: 30.0, right: 30.0), child: Column( children: <Widget>[ GestureDetector( child: Icon( FontAwesomeIcons.trophy, size: 50.0, color: Colors.white70, ), onTap: () { print("Pressed"); }), Text("Goals", style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.normal )) ], ), ), Container( margin: EdgeInsets.only(left: 50.0, top: 50.0, bottom: 30.0, right: 20.0), child: Column( children: <Widget>[ GestureDetector( child: Icon( FontAwesomeIcons.calendar, size: 50.0, color: Colors.white70, ), onTap: () { Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new CalendarsPage())); }), Text("Calendar", style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.normal )) ], ), ) ]), Row(// ROW 3 children: [ Container( margin: EdgeInsets.only(left: 30.0, top: 50.0, bottom: 30.0, right: 30.0), child: Column( children: <Widget>[ GestureDetector( child: Icon( FontAwesomeIcons.comments, size: 50.0, color: Colors.white70, ), onTap: () { print("Pressed"); }), Text("Community", style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.normal )) ], ), ), Container( margin: EdgeInsets.only(left: 20.0, top: 50.0, bottom: 30.0, right: 20.0), child: Column( children: <Widget>[ GestureDetector( child: Icon( FontAwesomeIcons.shoppingCart, size: 50.0, color: Colors.white70, ), onTap: () { Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new ShoppingList())); }), Text("Shopping", style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.normal )) ], ), ), Container( margin: EdgeInsets.only(left: 50.0, top: 50.0, bottom: 30.0, right: 40.0), child: Column( children: <Widget>[ GestureDetector( child: Icon( FontAwesomeIcons.solidCheckSquare, size: 50.0, color: Colors.white70, ), onTap: () { Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new CheckOut())); }), Text("Check Out", style: new TextStyle( color: Colors.white70, fontWeight: FontWeight.bold )) ], ), ), ]), ], ), ) //here ) ); }} 事件中读取该变量。 / p>

BeforeUpdate

答案 1 :(得分:0)

我认为,如果这对您很重要,那么您不应使用AfterUpdateAfterInsert,而实际上应该使用BeforeUpdate事件。您无需说出为什么要知道记录是插入还是更新,也不必说两种情况下的计划。即便如此,我还是很难构思出一个好的场景,在这个场景中,您需要在After***事件中知道这一点。

BeforeUpdate中,您仍然可以检查NewRecord属性并据此采取一些措施。更重要的是,您可以通过在Cancel事件中将BeforeUpdate参数设置为true来取消插入/更新的尝试。

另外,要呈现一个不同的选项-如果您希望执行的操作是以数据为中心的,并且不管数据是如何输入的,都是正确的,无论是否通过这种特殊形式,您都应该考虑使用数据宏,确实区分插入和更新。请注意,如果数据宏操作不足以执行所需的操作,您仍然可以从数据宏中调出VBA代码。

AfterUpdateAfterInsert事件非常适合事后处理事情,通常不关心记录的产生方式。示例包括诸如刷新相关表单,更改焦点,启用/禁用控件之类的内容。请注意,我引用的示例都是与UI相关的。这就是它的用途,应该这样使用。