ActiceX Data Objects (ADO) can be used in host programming environments that support the Component Object Model. The examples below illustrate ADO in the environments for Visual Basic Script/Internet Explorer, Visual Basic Script /ActiveX Server, Visual Basic and Visual Basic for Applications, Visual C++, and Java (Visual J++).
VBS in IE3.0:
You must use the HTML <object>...</object> tag to create an ADO object. You must have the class GUID (CLSID) for the object you want to create.
<object id=rs clsid="clsid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"> </object> <script language="vbs"> rs.Open parameters while not rs.EOF ' Insert your processing code here. rs.MoveNext wend rs.Close </script>
VBS in ActiveX Server:
Use the CreateObject( ProgID ) function on the server object to create new ADO objects.
ADO constants must be declared explicitly because they are not loaded automatically from the typelib. The ADO constants are declared in the adovbs.inc file and can be manually inserted into your script.
set rs = server.CreateObject("ADODB.Recordset" ) rs.Open parameters while not rs.EOF ' Insert your processing code here. rs.MoveNext wend rs.Close
VBA/VB5:
ADO objects can be created with the DIM statement and/or the CreateObject( ProgID ) function. ProgID names the desired class. If ADODB is defined in the References dialog of the Tools menu, the ProgID can be used as a type name in the DIM statement.
Suppose you want to create a recordset object. If ADODB is not defined in the References dialog, you must code:
DIM rs as OBJECT set rs = CreateObject("ADODB.Recordset")
If ADODB is defined, you may code either:
DIM rs as ADODB.Recordset set rs = CreateObject("ADODB.Recordset")
or
DIM rs as New ADODB.Recordset
Here is a more inclusive example.
DIM rs as ADODB.Recordset set rs = CreateObject("ADODB.Recordset") rs.Open parameters while not rs.EOF ' Insert your processing code here. rs.MoveNext wend rs.Close
C++:
You can explicitly acquire pointers to the ADO Automation interfaces with the CoCreateInstance() function.
#include <ole2.h> #include "adoint.h" #include "adoid.h" void MyFunction(void) { IADORecordset *pRS = NULL; CoCreateInstance( CLSID_IADORecordset, NULL, CLSCTX_INPROC_SERVER, IID_IADORecordset, ((LPVOID *) &pRS ); pRS->Open( parameters ); VARIANT var; VariantInit( &var ); while( SUCCEEDED( pRS->get_EOF( &var ) ) && var.boolVal != TRUE ) { // Insert your processing code here. pRS->MoveNext(); } pRS->Close(); pRS->Release(); }
JAVA:
The Javatlb.exe utility can be used to create a .class file from the ADODB typelib. Import the ADODB classes, then create ADO objects with the new operator.
import ADODB.*; class MyClass { void MyMethod() { IADORecordset rs = (IADORecordset) new ADODB.Recordset; rs.Open( parameters ); while( ! rs.getEOF().getBoolean() ) { // Insert your processing code here. rs.MoveNext(); } rs.Close(); } }