ItemAdded and ItemRemoved Event

14 May

From:    “Sofyan Efendi” <sofyan@madinah.cc>
Date:    Fri, 18 Aug 2006 16:04:26 +0700

Event ItemAdded terjadi saat reference VBA ditambah di dalam suatu project Visual Basic Access. Syntax:

Private Sub ReferencesName_ ItemAdded( ByVal Reference As _
Access.Reference)

Sedangkan event ItemRemoved terjadi saat reference VBA dihilangkan di dalam suatu project Visual Basic Access. Syntax:

Private Sub ReferencesName_ ItemAdded( ByVal Reference As _
Access.Reference)

Penjelasan:

– ReferencesName: Nama variable yang telah definisikan sebagai reference.

– Reference: Nama reference yang baru ditambah.

Contoh 1:

The following example includes event procedures for the ItemAdded and ItemRemoved events. To try this example, first create a new class module by clicking Class Module on the Insert menu. Paste the following code into the class module and save the module as RefEvents:

‘ Declare object variable to represent References collection.
Public WithEvents evtReferences As References

‘ When instance of class is created, initialize evtReferences
‘ variable.
Private Sub Class_Initialize( )
Set evtReferences = Application. References
End Sub

‘ When instance is removed, set evtReferences to Nothing.
Private Sub Class_Terminate( )
Set evtReferences = Nothing
End Sub

‘ Display message when reference is added.
Private Sub evtReferences_ ItemAdded( ByVal Reference As _
Access.Reference)
MsgBox “Reference to ” & Reference.Name & ” added.”
End Sub

‘ Display message when reference is removed.
Private Sub evtReferences_ ItemRemoved( ByVal Reference As _
Access.Reference)
MsgBox “Reference to ” & Reference.Name & ” removed.”
End Sub

Contoh 2:

The following Function procedure adds a specified reference. When a reference is added, the ItemAdded event procedure defined in the RefEvents class runs. For example, to set a reference to the calendar control, you could pass the string “C:\Windows\ System\Mscal. ocx”, if this is the correct location for the calendar control on your computer.

‘ Create new instance of RefEvents class.
Dim objRefEvents As New RefEvents

‘ Pass file name and path of type library to this procedure.
Function AddReference( strFileName As String) As Boolean
Dim ref As Reference

On Error GoTo Error_AddReference
‘ Create new reference on References object variable.
Set ref = objRefEvents. evtReferences. AddFromFile( strFileName)
AddReference = True

Exit_AddReference:
Exit Function

Error_AddReference:
MsgBox Err & “: ” & Err.Description
AddReference = False
Resume Exit_AddReference
End Function

Wassalaamu’alaikum Warahmatullahi Wabarakatuh,
Sofyan Efendi
– AccessWeb <http://www35.websamba.com/accessweb&gt;
– OPI-Homepage <http://www.geocities.com/opi1978&gt;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: