Let us start creating the first real COM object, which will be used in those ActiveX programs.
Specific steps are as follows:
Start Visual Basic;
We will see a "New Project" dialog where there are several option icons related to ActiveX. Let's explain it:
ActiveX DLL - Create a .dll program that contains classes, which is the option we will adopt;
ActiveX EXE - Create a .exe program that contains classes, will discuss this type later;
ActiveX Control - Add a project that allows yourself to create controls used in Toolbox. Don't discuss it here.
ActiveX Document EXE - Create a web page-based .exe program, not discuss here.
ActiveX Document DLL - Create a web page-based .dll program, because of our topic, you will not discuss it.
By the way, if there is no above option in the project list, you may use Visual Basic's learning version.
Of course, in the above multi-selected option, we are really interested in ActiveX DLLS and Active Exes. Waiting, then discuss the last type, let's take a look at the first!
Select the "ActiveX DLL" item;
Click the [OK] button;
In this way, an ActiveX DLL project is established. We create an ActiveX DLL is ready to use it as a front-end server, which is based on the Customers table in the Northwind database (the Northwind database is released with Visual Basic, located in the VB98 folder).
This means that we will definitely handle Customers information, but then we only need to call the class, instead of staying in the entanglement of the data processing code.
Of course, we have to create this class first. Although COM can make programming more easily, this class is still more difficult. Despite this, the name in the ActiveX program will be modified before going deep into the database.
Change the name of the Name property to "Customers";
Select the "Project" -> "Project Properties" menu, in the pop-up dialog box, change the engineering name to "Northwind";
Now let the class are connected to the database:
Select "Project" -> "References" menu;
In the pop-up dialog box, select "Microsoft ActiveX Data Objects 2.1 Library", click the [OK] button; this "reference" allows the user to handle a database, of course, now use COM objects to handle it. The following will be expanded around the corresponding recordset:
Add the following code in our class:
DIM RS as Recordset
This is a recordset object for accessing the database.
Of course, when other starts use this class, we want to record the set objects and databases, while when the class is used, the connection to the database can be disconnected. Based on this idea, its code is as follows:
In the code window, change the current "General" item in the Object combination box to "Class";
In the combo box on the right, make sure the current item is "Initialize";
The code window will appear:
Private sub coplass_initialize () End Sub When the class is just start, all the code in this will be executed, similar to the Form_Load event in the form.
Type the following code in the "Initialize" event:
SET RS = New RecordSet Rs.ActiveConnection = "Province = Microsoft." & _ "Jet. OleDb.4.0; Data Source = C: / Program Files /" & _ "Microsoft Visual Studio / VB98 / NWIND.MDB;" & _ "Persist security info = false" rs.open "Select * from customer, AdoPenkeySet, Adopoptimistic
There is no need for any related code related to this class, which is just using the Visual Basic's universal ADO database processing code, which is used to establish a connection with the database.
It should be noted that if the NorthWind database nwind.mdb is not in the C: / Program Files / Microsoft Visual Studio / VB98 folder, you must modify the ActiveConnection string content!
When the class begins, the RS object is responsible for establishing a connection with the database, but when the class object is completed or when the program turns off it, we should make the connection disconnect.
When programming, we use the Terminate event, which is very similar to Form_unload. From the name, they have a "n", but more similar places are excited when the corresponding object is turned off.
Let's add the code to close the database:
Select "Class" from the Object Combination box, select "Terminate" from the Procedure Combination box;
Add the following code in the "Terminate" event:
rs.close set = Nothing
This is another simple code we added, it just simply shut down the database, and then sets the RS to Nothing. In this way, RS will be effectively deleted.
Ok, this section is here. In the next section, we will continue to add code to handle records in the data.