适用于:Access 2013、Office 2013
此简短 Microsoft Visual J++ 示例显示如何将您自己的函数与特定事件关联。
// BeginEventExampleVJ
import com.ms.wfc.data.*;
public class EventExampleVJ
{
ConnectionEventHandler handler = new ConnectionEventHandler(this,"onConnectComplete");
public void onConnectComplete(Object sender,ConnectionEvent e)
{
if (e.adStatus == AdoEnums.EventStatus.ERRORSOCCURRED)
System.out.println("Connection failed");
else
System.out.println("Connection completed");
return;
}
public static void main (String[] args)
{
EventExampleVJ Class1 = new EventExampleVJ();
Connection conn = new Connection();
conn.addOnConnectComplete(Class1.handler); // Enable event support.
conn.open("DSN=Pubs");
conn.close();
conn.removeOnConnectComplete(Class1.handler); // Disable event support.
}
}
// EndEventExampleVJ
首先,通过创建新的 ConnectionEventHandler 对象并将 onConnectComplete 函数分配给该对象,使类方法 onConnectionComplete 与 ConnectionComplete 事件关联。
然后,main 函数创建 Connection 对象,并通过调用 addOnConnectComplete 方法然后将 handler 函数的地址传递给它来启用事件处理。