Common Event Procedures

P

Phil Hood

Hello,

I have a form with a number of checkboxes on it. I want to
trigger the same event procedure whichever of the boxes is
checked.

I've worked out that you can set an event procedure for
each separate control (i.e. for each check box) but I want
to write a common event procedure that is used in all
cases.

Is there a way of doing this?

Thanks in advance,

Phil.
 
P

Phil Hood

Hi,

Thanks for the idea but more than one check box can
be 'checked' on the form (my understanding of option
groups is that only one can be 'checked')

Is there another way?

Phil.
 
P

PC Datasheet

Write a function in the Modules section that does what you want. Open your
form in design view and select all the checkboxes. Open properties, go to
the Events tab and in the AfterUpdate event type:
=NameOfMyFunction

The function will execute any time you check or uncheck any of the
checkboxes you selected.
 
A

Arvin Meyer

Write your code as a function in the General section of your form's module.
Then you can call the code in the AfterUpdate event of each checkbox:

=MyFunction()

You can also simulate a control array by naming your check boxes like chk1,
chk2, chk3 ... etc. That way if you need to know which check box is calling
it you can use:

Dim i As Integer
i = 1 to n 'n being the number of checkboxes
Me.chk & i
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
P

Phil Hood

Thanks again.

I'm not sure how to go about writing a function in the
General section of the form's module.

A few pointers would be really helpful (not how to write
the procedure but where to put it so it is accessible to
each checkbox.

Thanks

Phil.
 
P

PC Datasheet

Open the database window and go to the Modules tab. Write your function
there. Then see my response for what to do with it.
 
P

Phil Hood

Hi,

I've written the function and called
it 'AddDeleteYearRecord' so it appears with this name in
the Modules section.

I then added the name of the function (above) to the
OnClick event type of the check boxes.

But, when I check (or uncheck) any of the boxes get the
following message:

------------------------
The expression On Click you entered as the event property
setting produced the following error: The expression you
entered has a function name that Microsoft Access can't
find.
* The expression may not result in the name of a macro,
the name of a user-defined function, or [event procedure].
* There may have been an error evaluating the function,
event, or macro.
------------------------

I know the procedure works OK as I have tested it by
attaching to the OnClick event of a single control. But
when I try to make it a common function, I get this
message.

This is the first time I've set up a new module so it's
probably something I'm doing wrong.

Please help.

Thanks

Phil.
 
P

Phil Hood

Hi,

I've worked it out now - thanks again for your help.

Phil.
-----Original Message-----
Hi,

I've written the function and called
it 'AddDeleteYearRecord' so it appears with this name in
the Modules section.

I then added the name of the function (above) to the
OnClick event type of the check boxes.

But, when I check (or uncheck) any of the boxes get the
following message:

------------------------
The expression On Click you entered as the event property
setting produced the following error: The expression you
entered has a function name that Microsoft Access can't
find.
* The expression may not result in the name of a macro,
the name of a user-defined function, or [event procedure].
* There may have been an error evaluating the function,
event, or macro.
------------------------

I know the procedure works OK as I have tested it by
attaching to the OnClick event of a single control. But
when I try to make it a common function, I get this
message.

This is the first time I've set up a new module so it's
probably something I'm doing wrong.

Please help.

Thanks

Phil.


-----Original Message-----
Write a function in the Modules section that does what you want. Open your
form in design view and select all the checkboxes. Open properties, go to
the Events tab and in the AfterUpdate event type:
=NameOfMyFunction

The function will execute any time you check or uncheck any of the
checkboxes you selected.

--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
(e-mail address removed)
www.pcdatasheet.com
want
boxes
.
 
A

Arvin Meyer

When you open a new code module in a form, it opens with Declarations in the
General Section which generally looks like:

Option Compare Database
Option Explicit

Under General, if you use the combo, you'll see that it refers to each
object in the form (or report) class module. Each of those objects has a
number of events which can be coded, but if you simple write a sub or
function without attaching it to one of those events, it stays in the
General Section and can be called from any of the other subs or functions.
Further, if you use a function, you can call it in the event property sheet.

Here's an example of a General function:

Private Function GetFormattedDate(DateIn As String) As Date
GetFormattedDate = Format(DateIn, "m/d/yyyy")
End Function

Everywhere you call that on the form it will return the date formatted as
m/d/yyyy. If you put the code in a Standard Module, you could call it from
anywhere in the database. The code you are looking for is more form
specific, so we'll just keep it there for easy maintainability.

An Object submight look like this:

Private Sub Form_Open(Cancel As Integer)
Me.Caption = "The current date is: " & GetFormattedDate(Date)
End Sub

This code is tied to the form's Open event.

HTH
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top