This is a class definition from the uploaded database: Class
'------------------------------------------------------
Option Compare Database
Private l As Single
Private w As Single
Public Event AllowedAreaExceeded(ByVal area As Single)
Public Function calcArea(ByVal l As Single, ByVal w As Single)
Dim a As Single
a = l * w
If a > 100 Then
RaiseEvent AllowedAreaExceeded(a)
End If
calcArea = a
End Function
'------------------------------------------------------------
This class has two members:
Public function calcArea
Public event AllowedAreaExceeded
When the function is executed, it calculates the area of a rectangle, then compares the result with 100, if greater then you want an event to happen . You achive this by issuing the command
RaiseEvent AllowedAreaExceeded(a)
It is raised, allowed to happen, when a>100
Compare it to a Command1_click event, which occurs when we click a button.
This is the code behind the form:
Option Compare Database
Private WithEvents myClass1 As Class1
Private Sub Command3_Click()
Dim a As Single
a = myClass1.calcArea(10, 11)
End Sub
Private Sub Form_Load()
Set myClass1 = New Class1
End Sub
Private Sub myClass1_AllowedAreaExceeded(ByVal a As Single)
MsgBox ("Area " & a & " exceeds allowed")
End Sub
Here are some helping notes which I taught today for a Visual Basic class. Command1, Command2, and so on are the default names access gives for command buttons when newly created.
1) Access helps us to create event-driven applications
2) Event driven means the application is idle and is waiting for an event to drive it (let it work)
3) The form has few controls with many events (actions) like button clicked, textbox changed, form clicked, form changed size, and mouse is hovering over an image.
4) Of all these events we select the suitable ones, when happen, to drive the application (event driven application)
5) As an example we select the click event of a command button (Command1_Click)
6) We need a procedure which will execute when the event occurs: In access a procedure that starts like,
Private Sub Command1_Click (&..) will execute when Command1 control is clicked.
This procedure is called an event handler.
This procedure is a must to drive the application
Code has to be inserted in this procedure to do useful work.
One statement may be to call a sub procedure to calculate the average of 3 numbers
Another statement may assign the result of a function to a variable
7) Other procedures should be included to cater for the previous commands
8) Command1_Click is an event handler, and the other procedure is "I call it an order or command handler, it handles the order or the command to do something"
9) There are situations when you need your own event to occur, like the area exceeds 100 units square.
10) You declare this type of event in a Class, and it will occur (when you issue RaiseEvent statement) in the procedures when the area > 100.
11) Command buttons, textboxes, and other controls are defined by access as classes. When you click on a button icon and draw a button on a form, access creates an object of the class called Command1 as default. The class has events and occur when we click or hover or change size and so on)
12) When you select click event of a button and write code it will handle the click event.
13) For our defined event, we write the procedure using the name of the event to handle the event when it occurs (Raised).