MS Access automatically creates form events
Is it possible to programmatically create inline events of an MS Access access form? I have a feeling that it is not, but I think I'll check. (I am using Access 2003).
For example, I want to do something like this in a private form:
RaiseEvent Delete(Cancel)
and make it fire the Access.Form delete event - that is, without actually deleting the associated record.
Note that my delete event is not handled by the form itself, but by an external class, so I cannot simply call Form_Delete (Cancel).
a source to share
I can understand your confusion - I have not explained any of the larger contexts. I'm sorry.
Basically, the situation is that I have an "index" form, i.e. "continuous forms", which is bound to a read-only request. The request should only be read because it includes an outer join. But I want to be able to remove base records from this form.
So my first thought was to do a delete outside of the form's recordset, eg. using a delete request. And I was hoping to catch the standard Delete / BeforeDelConfirm / AfterDelConfirm events around this manual delete procedure by raising those events myself. But alas, this is not possible.
If the form itself was handling these events, I could just call the handlers (Form_Delete, etc.), but my project has custom classes that handle form delete and update events (validation, confirmation, logging, etc.) for of all forms, ( @Smandoli , this is not well documented, I just discovered it a few months ago and am using it extensively now - you may already know about this - you can set up external classes to handle your form For example, here )
Long story short, I found a workaround that I'm happy with. This includes creating an "index" form of a subform of another form associated with a recordset that can be deleted. Thus, deletion can be performed in the external form using standard form access events based on the selection in the internal form.
@Knox , I fundamentally disagree with the fact that being able to create "inline" events yourself is difficult to document and maintain. Many other frameworks depend on this. In practice, I agree with you, as we must all work within the constraints of our tools and the "best practices" that evolve around those constraints. The blessing and curse of access is a tight binding between recordsets and forms ...
a source to share
In general, there is simply no need to fire standard form events on your own, it usually shows a misunderstanding of form events in general (if not events at all).
Form events exist to respond to user interaction with the form, or to notify code behind what normally happens (such as the Form_Load event). Subjects of events must react to this event - nothing else.
It is often believed that people want to directly trigger event subtitles, but this is also not true. There is a reason that event entities are generally declared as "Private" and not "public", so you should prevent them from being called directly from outside the code module, but in fact, you shouldn't also execute any of them inside one and of the same code module. Event subjects should always be caused exclusively by their events, although they can be named directly.
If the event child has any code that needs to be executed elsewhere as well, create a private or public element inside the same module (depending on whether you want to execute them externally or not) and then call that helper from event substitution, If you think you should be doing the same south elsewhere, you can call the same sub as well. This is not a question "can the sub event be called directly", it is mainly a design question. You should always be sure that the ancillary event was only triggered by the event itself and never was any code. The problem with invoking the event subcode is that you can quickly get into trouble if you are executing the code and also executing the real event. You end up with a lot of code chaos,which is very difficult to debug.
It is of course possible, of course, to call the event children from a class module that has a link to the form (which is necessary if you are using a class module to handle common events). You will need to declare the child events as Public, and then you can call them using a form reference, but as above: don't do that.
- If the class module is used to handle events, you can do something here, you don't need the form code.
- If the query is read-only and you want to delete a record in the base table, no auxiliary record can help you. They are triggered when the user wants to delete something that he cannot do because he only reads this way, DoCmd does not help you either.
- As David said in the comment above, just create a delete button anywhere, which can then read the current row id in continuous form and run the SQL DELETE command, then just query the continuous form and you're done. You can also handle this in your standard module, because you can not only forward form events, but you can also forward control events in the same way. Create an Init procedure in a class module that takes all the controls from your form that you want to handle, possibly any base table name in each continuous form, then the class module can assign it to the "WithEvents" standard a control variable, for example, type CommandButton and store the base table name in a string variable.(Remember to set the OnClick event to the "[Event Procedure]" in the Init procedure). On the Load event of your continuous form, where you are probably initializing your class module, you can redirect the base table name and the Delete button button to the Initialize class module procedure, which can then handle deletion in a very general way, DELETE in the base table and request the form because it already has a link to the form. There is no need to call any event procedure.a class module, which can then handle the deletion in a very general way, DELETE the underlying table and query the form since it already has a reference to the form. There is no need to call any event procedure.a class module, which can then handle the deletion in a very general way, DELETE the underlying table and query the form since it already has a reference to the form. There is no need to call any event procedure.
Last but not least, there may be frameworks that allow you to create events directly, but I would argue that the creators of such frameworks also did not understand the purpose of event procedures. If you've ever created your own event in your own module, you'll see that they can't be hoisted outside of the class module either. Of course, you CAN subtitle the RaiseEvent yourself to call them externally - and in fact, in the case of your own events, this might make sense in some scenarios. In the case of events of the form (control ...), they should inform the code about what happened, and now there should be a reaction. If you use events in modules of your own class, you usually also create a "WithEvents" variable in the external module to get information when an event occurred in another module of the class.The event should make the module objects independent from each other. A module with an event will only raise the event and it doesn't know if anyone is listening to it or reacting to that event. It tells the "world" that there was something in the class module and nothing else. Like a radio station that sends the daily news "to the world" but it doesn't know if anyone is listening to it. Usually the listener of the radio station did not go to the radio station and did not read his own news for other listeners. Only the people at the radio station decide what to send and when. The same story.into the world, "but she doesn't know if anyone is listening to her. Usually the listener of the radio station did not go to the radio station and read his own news for other listeners. Only the people on the radio station decide what to send and when. Same story.into the world, "but she doesn't know if anyone is listening to her. Usually the listener of a radio station did not go to the radio station and read his own news for other listeners. Only the people at the radio station decide what to send and when. Same story.
a source to share