Global Form Events

  • Thread starter Thread starter CrazyAccessProgrammer
  • Start date Start date
C

CrazyAccessProgrammer

Is it possilbel to VBA Code event that you always want to occur based on a
form event?

Say I always want certain code to run on a form's OnCurrent event. Is there
a way to invoke this code without having to place a call to the procedure in
every single form in my database?
 
I'm not sure what you're looking for...

If you want a form's OnCurrent event to run some code, as I understand it,
you have to put the code in that form's OnCurrent event procedure.

Even if you built a complex routine that you wanted to run for each form's
OnCurrent event, you'd have to tell the form what it was supposed to do...

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
You answered my question thank you very much.

Jeff Boyce said:
I'm not sure what you're looking for...

If you want a form's OnCurrent event to run some code, as I understand it,
you have to put the code in that form's OnCurrent event procedure.

Even if you built a complex routine that you wanted to run for each form's
OnCurrent event, you'd have to tell the form what it was supposed to do...

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
CrazyAccessProgrammer said:
Is it possilbel to VBA Code event that you always want to occur based on a
form event?

Say I always want certain code to run on a form's OnCurrent event. Is
there
a way to invoke this code without having to place a call to the procedure
in
every single form in my database?

well, you can put the name of the function in the forms property setting.
Thus, in theory, you don't actually have to place code in the forms
on-current event.....

This also means it quite easy to write code that would update all forms to
have this setting.

This "trick" also works if you select 10 controls on a form, and then type
in the name of a function for some event/property for those controls. You
only have to type in the function name once, and "all" controls you selected
will now run that code for the given event.

The limitation in the above is that you can't use this trick for events that
have parameters (such as the before update event).

The format for the property setting is:

=NameOfFunction()

that function can be global function in a standard code module...

In the "general" code module, you can't use "me" anymore, but the code can
be written as:

Public Function AskInvoicePrint()

Dim tblgroupid As Long
Dim frmActive As Form

Set frmActive = Screen.ActiveForm

tblgroupid = frmActive.frmMainClientB.Form!ID

If frmActive.InvoiceNumber = 0 Then
frmActive.InvoiceNumber = nextinvoice
frmActive.Refresh
End If

The above is the same approach I use for menu bars (and now ribbon) code
that gets run when you click on the menu/ribbon. Note how the code picks up
the active form, and thus I use "rmActive" in place me.
 
Thank you very much for your insight =)

Albert D. Kallal said:
well, you can put the name of the function in the forms property setting.
Thus, in theory, you don't actually have to place code in the forms
on-current event.....

This also means it quite easy to write code that would update all forms to
have this setting.

This "trick" also works if you select 10 controls on a form, and then type
in the name of a function for some event/property for those controls. You
only have to type in the function name once, and "all" controls you selected
will now run that code for the given event.

The limitation in the above is that you can't use this trick for events that
have parameters (such as the before update event).

The format for the property setting is:

=NameOfFunction()

that function can be global function in a standard code module...

In the "general" code module, you can't use "me" anymore, but the code can
be written as:

Public Function AskInvoicePrint()

Dim tblgroupid As Long
Dim frmActive As Form

Set frmActive = Screen.ActiveForm

tblgroupid = frmActive.frmMainClientB.Form!ID

If frmActive.InvoiceNumber = 0 Then
frmActive.InvoiceNumber = nextinvoice
frmActive.Refresh
End If

The above is the same approach I use for menu bars (and now ribbon) code
that gets run when you click on the menu/ribbon. Note how the code picks up
the active form, and thus I use "rmActive" in place me.
 
Back
Top