如何使用字符串调用控件的afterUpdate事件。 [英] How do you call a controls's afterUpdate event using a string.

查看:170
本文介绍了如何使用字符串调用控件的afterUpdate事件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个想法是这样的:

代码将数据输入到控件txtRR1,txtRR2,txtRR3等中。

forms(me.name)(" txtRR"& ; cstr(i))= aValue,工作正常。


现在我想从类似

forms(me.name)(" txtRR"& cstr(i))(")中调用txtRRi_afterUpdate。 _afterUpdate")或表单(me.name)(" txtRR"& cstr(i))_ afterUpdate。这两个都失败了。


到目前为止,所有尝试编写afterUpdate调用的尝试都失败了。

解决方案

一般来说,它'直接调用事件过程并不是一个好主意。默认情况下,无论如何都无法从对象模块本身的外部访问它们,但更重要的是,它们并不是设计为直接通过事件以外的任何其他方式调用。


而是,处理这种要求的标准方法是定义一个过程(如果需要从对象自己的模块之外的任何地方调用,则为Public),事件过程调用该过程来执行所需的任何工作,并调用该过程而不是程序。


在同一个模块中调用任何程序并不要求程序名称完全合格(似乎是在您发布的代码中的尝试)。如果该过程被命名为Bob,则该过程被称为Bob。那么所有你需要做的就是调用Bob。 :

展开 | 选择 | Wrap | 行号


事件过程由同一表单对象模块中的代码调用。代码从文本文件输入数据,并用数据填充一系列文本框。每个文本框填满后,其更新后的事件需要触发,因此我的第一个问题。通过将事件过程声明为公共过程,可以在代码中将其称为controlName_afterUpdate。由于我的文本框被串行命名为txt1,txt2,txt3 ....调用过程的理想方法是使用for循环,如我的第一个注释。我已经决定使用带有大型select语句的sub,并使用控件的整数作为输入参数调用。这不是很优雅。


是的,您通常可以调用事件通常在同一表单中运行的代码(例如,如果控件名称为ctrlTextBox1则为:调用ctrlTextBox1_AfterUpdate 将调用该程序。


VBA通​​常不会跳过afterupdate事件,这是恕我直言,愚蠢。有人会认为更改值通过人工或代码交互的控件会导致事件触发 - 这不是Access似乎总是发生的事情。


----------是的,它是ACC2003;但是,这并没有改变:

AfterUpdate事件[Access 2003 VBA语言参考]

•使用Visual Basic或a更改控件中的数据包含SetValue操作的宏不会触发控件的这些事件。但是,如果你转移到另一条记录或保存记录,表格的'AfterUpdate事件确实发生。



----------


The idea is this:
The code enters data into controls txtRR1, txtRR2, txtRR3 etc. by
forms(me.name)("txtRR" & cstr(i)) = aValue, which works fine.

Now I want to call txtRRi_afterUpdate from something like
forms(me.name)("txtRR" & cstr(i))("_afterUpdate") or forms(me.name)("txtRR" & cstr(i))_afterUpdate. These both fail.

All attempts to code the afterUpdate call have failed so far.

解决方案

Generally speaking, it''s not a good idea ever to call an event procedure directly. By default, they are not accessible from outside of the object module itself anyway, but more than that, they are not designed to be called directly by anything other than the event anyway.

Instead, the standard way to handle such a requirement is to define a procedure (as Public if it needs to be called from anywhere outside of the object''s own module) that the event procedure calls to do whatever work is required, and call that procedure instead.

Calling any procedure from within the same module doesn''t require that the procedure name be qualified at all (as seems to be the attempt in your posted code). If the procedure is named "Bob" then all you need do to refer to it is call "Bob" :

Expand|Select|Wrap|Line Numbers


The event procedures are being called by code within the same form object module. The code inputs data from a text file and fills a series of text boxes with data. After each text box is filled its after update event needs to fire, hence my first question. By declaring an event procedure as public one can call it within code simply as controlName_afterUpdate. Since my text boxes are named serially as txt1, txt2, txt3.... the ideal way to call the procedures is with a for loop as in my first note. I have settled on using a sub with a large select statement called with the integer number of the control as an input parameter. This is not elegant.


Yes you can usually call the code that the event would normally run within the same form by (for example if the control name is ctrlTextBox1 then : call ctrlTextBox1_AfterUpdate will call the procedure.

VBA will not usually trip the afterupdate event, which is IMHO, stupid. One would think that changing the value in a control either via human or code interaction would result in the event trigger - this is not what seems to always happen with Access.

----------Yes, It''s ACC2003; However, this hasn''t changed:

AfterUpdate Event [Access 2003 VBA Language Reference]

•Changing data in a control by using Visual Basic or a macro containing the SetValue action doesn''t trigger these events for the control. However, if you then move to another record or save the record, the form''s AfterUpdate event does occur.

----------


这篇关于如何使用字符串调用控件的afterUpdate事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆