trying to code an event[afterInsert]not ready for prime time

G

Guest

The help file is not very helpful explaining how to do this. I want some
code to happen IF a new record is created. I'm trying to write the event
module. I picked the beforeInsert event. I don't have a form only
datasheet. This event should trigger if the new record is clicked in
datasheet view. I'm not sure how to set up the event trigger? Where do I put
the afterInsert event?


MyClass module:


Option Explicit
public event Flag(byref booFlag as Boolean)

Public Sub getFlag( )
RaiseEvent Flag(boolean)
End Sub

Option Explicit
Dim WithEvents objOtherClass As MyClass
Sub LoadClass()
Set objOtherClass = New MyClass
End Sub

Sub objOtherClass_getFlag(by ref booFlag as boolean)
booFlag
End sub


MyClass.Flag

AfterInsert

thanks,
 
W

Wolfgang Kais

Hello Janis.

Janis said:
The help file is not very helpful explaining how to do this.
I want some code to happen IF a new record is created. I'm trying
to write the event module. I picked the beforeInsert event. I
don't have a form only datasheet. This event should trigger if
the new record is clicked in datasheet view.
I'm not sure how to set up the event trigger?
Where do I put the afterInsert event?

MyClass module:

Option Explicit
public event Flag(byref booFlag as Boolean)

Public Sub getFlag( )
RaiseEvent Flag(boolean)
End Sub

Option Explicit
Dim WithEvents objOtherClass As MyClass
Sub LoadClass()
Set objOtherClass = New MyClass
End Sub

Sub objOtherClass_getFlag(by ref booFlag as boolean)
booFlag
End sub


MyClass.Flag

AfterInsert

I'm really not sure of what exactly you want to implement. What
you can't do in Access is trigger the insertion of a new record
in a table. For this, you will have to create a trigger e.g. in a
SQL Server database.
In Access, events can be triggered for Access objects as forms or
reports (or user defined classes), but not for tables or queries.
For a form, you can write an "event procedure" for the BeforeInsert
event. The event procedures are stored in a class module associated
with the form, so it makes the form a (visual) class that can be
instanciated (inheritance is not supported).
In effect, a datasheet of a table or query IS a form, therefore it
also supports events, but since it cannot contain a module, there
can't be event procedures for the events. Fortunately, there are
two other ways for us to capture events: macros and functions.
Surprisingly, these methods work in datasheets, too. Unfortunately,
you cannot configure these in design view of the table, but you can
alter the values in datasheet view. That means that such a datasheet
has to be opened via code. Here's an example that uses the Products
table from the northwind database:

DoCmd.OpenTable "Products"
With Screen.ActiveDatasheet
.OnCurrent = "=MsgBox(""Current Product: "" & Form!ProductName)"
.BeforeInsert = "=MsgBox(""You are entering a new record!"")"
End With

Of course you can use your own function instead of MsgBox.

The class module for a form or report cannot be created standalone,
it is associated with the form, and it contains invisible variables
declared with WithEvents for the form itself and it's controls.
When in design view of a form, you can view (and edit) this module by
clicking "Code" in the view menu or the Code button in the toolbar.

Your code sample seems to be in fact two class modules, and I don't
see anything that has to do with a datasheet or a BeforeInsert event.
You used the typename "boolean" as a variable that wasn't declared.
"by ref" has to be writte in one word as "ByRef".
booFlag cannot be executed like a if it were a procedure.
MyClass.Flag cannot be executed, because MaClass is a class and not
an object and because Flag isn't a method but an event. I guess you
meant something like objMyClass.getFlag.
But shat's that "AfterInsert"?
 

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