VBA Access How to Use Custom Event - learnit

Home Top Ad

Post Top Ad

Sunday, June 27, 2021

VBA Access How to Use Custom Event

VBA Access How to Use Custom Event

VBA Access How to Use Custom Event

What is VBA Access


VBA stands for Visual Basic for Applications (the application being of course Microsoft Access) and is the technology and tools used to program and automate this application.It’s not only used just within the framework of Microsoft Access Database but other applications too including Microsoft Excel, Microsoft Word, Microsoft Outlook to name a few.It has the power to communicate with other applications beyond the Microsoft range and even the Microsoft Windows operating system across other platforms.So, learning the principles of VBA using Access as the tool environment will stand you in good stead for the other applications should you wish to program and code them in the future.The only difference between other applications when wanting to use VBA will simply be learning to load and work with different libraries.Typical reasons why you would use Microsoft Access VBA include being able to


    1.Manage smaller re-usable procedures (globally).

    2.Create you own defined functions.

    3.Hold variables in memory (globally).

    4.Communicate with other applications and platforms.

    5.Attach code to an event i.e. a button on a form.

    6.Manage more powerful procedures that an Access macro lacks.

    7.Gracefully handle errors (when they happen).


Custom Event

Events must be declared within a class module. Although an event declaration may occur anywhere within a VBA module, it only makes sense to position event declarations near the top of the module where other developers easily see them.


Public Event ShowMessage(Message as String)

That is all there is to an event declaration. The Public keyword is needed, of course, to expose the event to the class's consumers. In effect, the Public keyword adds the event to the class's interface. The Event keyword, of course, specifies that the declaration's identifier - ShowMessage - is an event and should be managed by VBA's class module hosting mechanism.


You might recall that I have asserted that class modules were special in a number of regards. Events are clearly one of the special characteristics of VBA class modules.



A quick look through the Object Browser at the class module shows that the class's interface does, indeed, include the ShowMessage event.


Raising events

An event that is never invoked by a class module's code isn't much use to anybody. Events are typically triggered (or raised) whenever circumstances indicate that the consumer should be notified.


Example

Public Event ShowMessage(Message as String)
public sub init(Optional id as Variant=3, Optional Item as Variant="Naro")
RaiseEvent Showmessage("Contact class3 Initializing")
    If Not IsMissing(id) then
        sid=id
    else
        sid=1
    end if
    if Not IsMissing(item) Then
        sitem=item
    else
        sitem="reanits.com"
    end if
        RaiseEvent showmessage("Contact class3 Initializin")
    End sub

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad