ADBLib index

ADBLib the new Visual C® library for database applications

Description | Brief description of ADBLib
Install | Install hints
Programming | Programming technique
Reference | Adblib reference
ADBWizard | ADBWizard help
Form usage | Form commands
System req. | System requirements
License | License agreements


Description

ADBLib.dll provide some classes and routines to develop DAO® database applications quickly using Microsoft® Visual C 6.0, the important feature of this library is that beside the power and coding speed, you can still access DAO derived objects from your programs allowing you to accomplish also complex tasks when needed, for more details see programming.

To simplify your task when creating modeless form it has been added a simple tool: ADBWizard it will automate the process of base class substitution, so that you can create forms and add them to your application very quikly.


Install

To use ADBLib.dll in your program follow the next steps:

  1. Create a framework for a multiple document application form App MFC AppWizard (exe), in database support choose Headers files only
  2. Copy all Adblib files in your app and rename ADBLibD.dll to ADBLib.dll moving it into your Debug directory, if Debug directory is not persent or you create it or you build your app once then move the file.
  3. Insert at the beginning of your "resource.h" the following line
    #include "resourceADBLib.h"
  4. Open the project settings dialog box, insert the library ADBLib.lib in the linker pane Object/Library modules, for debug version use ADBLibD.lib
  5. Define a CDAODatabaseobject somewhere in the source file (not in the body of a function because it will be deleted at the exit)
    CDaoDatabase db;
  6. In the InitInstance() function of the application add the following code ( assumed that you want to access the dbase.mdb database).
    HINSTANCE hDLL = LoadLibrary("ADBLibDLL");
    db.Open( "C:\\dbase.mdb" );
    SetMainDatabase(&db);
  7. Make the header file ADBLib.h available to the various source file (put it in the main .H file so you don't have to add it to every form, e.g. if your app name is test.exe put it in test.h).

Now installation should be ok so you are ready for programming.


Programming

In this section you will learn how to add forms to your application and access data with ADBLib controls :

Adding forms : how to add and use CDBForm the most important class of the library.

Opening tables : how to open tables in CDBForm.

Adding edit controls : how to add edit controls in CDBForm

Adding combo boxes : how to add combo boxes in CDBForm

Adding grid : how to add grid controls in CDBForm.


Adding forms

Assumed that you already followed Install instructions, follow next steps to add forms to your app:

  1. Create a new dialog resource, no border, child styles, delete Ok and Cancel buttons.
  2. Arrange the controls you want to use with the table of your database
  3. Create the class with ClassWizard deriving the new form from CFormView.
  4. Add the OnInitialUpdate() message handler and the following code:(in the following example you access the fields IdCust and Name of the table Customers)
    OpenCursor( "SELECT * FROM [Customers]");
    AddEditCtrl( IDC_EDIT_ID, "IdCust", FLAG_EDIT_R_ONLY);
    AddEditCtrl( IDC_EDIT_NAME, "Name");
    CDBForm::OnInitialUpdate();
  5. Change the base class of the form from CFormView to CDBForm(see ADBWizard )
  6. Change the default constructor of the new class of the form from CFormView to CDBForm(see ADBWizard )
  7. Change the referring class of the message handler from CformView to CDBForm (see ADBWizard )
  8. Insert a new menu item in the main menu.
  9. Create the regular handler in the application.
  10. Make the header file of the new class available to the application.
  11. Insert the following code in it (assumed the new form is CformTest).
    OpenForm( RUNTIME_CLASS(CFormTest), "Title");
  12. Compile and link.

IMPORTANT: follow the previous steps in the right order or Class Wizard will generate a wrong message list.


Opening tables

To use the ADBLib library you have to open at least one table and the first one will be used to navigate, add, delete, and modify data. When closing the form, the tables will be automatically closed and the Cursors deleted, so what you have to do is only opening them; to open more than one you just add as many opening commands "OpenCursor" as you like.

NOTE: the open cursors can be accessed via the function Cursor(n).

The following example open the table customers.

OpenCursor( "SELECT * FROM [Customers]");


Adding edit controls

If you want to add to your form an edit control referencing a field in the main table use the function AddEditCtrl fromOnInitialUpdate() function of the form.

The following example associate the edit control IDC_EDIT_ID with the field IdCust:

OpenCursor( "SELECT * FROM [Orders]");
AddEditCtrl( IDC_EDIT_ID, "IdCust")


Adding combo boxes

If you want to add to your form a combobox referencing another table follow next steps in the OnInitialUpdate() function:

  1. Open the table of the combo box as a cursor selecting as first field the one that will fill the list shown, and as second the hidden Id that will replace the field of the main table
  2. Associate the combobox to the field using AddComboCtrl function
  3. Fill the combobox with FillCombo.

The following example will show the name of the customers in the list and will update the CustId of the Orders table:

OpenCursor( "SELECT * FROM [Orders]");
OpenCursor( "SELECT Name,IdCust FROM [Customers] ORDER BY Name");
AddComboCtrl( IDC_CBO_CUST, "IdCust");
FillCombo( IDC_CBO_CUST, 1);

NOTE: the Id field must be numeric in both tables, the main form table and the combo box table.


Adding grid controls

To add a grid to your form follow next steps:

  1. Arrange a ListControl in your form.
  2. Set the following styles for the grid: View: Report, Align: top, Sort: none, Single selection, Owner draw fixed
  3. In OnInitialUpdate(), open the cursor of the table for the grid specifying the parameter for filtering the records.
  4. Add the columns you need to the grid specifying fields and width.

The following example will edit Desc, Quant and Price fields of the table Orders_r filtering the records using IdOrder, this field is present in both tables master and rows table.

OpenCursor( "SELECT * FROM [Orders]");

OpenCursor( "SELECT * FROM [Orders_r]", "IdOrder = 1");

AddListCtrl( IDC_LIST_ROW, "IdOrd", 2, "IdOrder");

AddListCol( IDC_LIST_ROW, "Desc", 100);

AddListCol( IDC_LIST_ROW, "Quant", 50);

AddListCol( IDC_LIST_ROW, "Price", 50);

The following example adds rows to the grid whose Id is IDC_LIST_ROW.

void CFormOrders::OnButAddRow()

{

CDBGrid * pDBGrid = (CDBGrid *) GetDlgItem(IDC_LIST_ROW);
ASSERT_VALID( pDBGrid );
pDBGrid->AddRecord();

}


Reference

Class reference for ADBLib library:

GeneralGeneral routines

CDBFormBase class for form

CDBrsBase class for cursor

CDBComboBase class for combo boxes

CDBChildFrame Base class for frame form

CDBEdit Base class for edit

CDBGridBase class for grid


General

General routines:

  • void OpenForm( CRuntimeClass* prt , CString Title )

this function is used to open new forms in the application.

  • void SetMainDatabase( CDaoDatabase * pDB )

with this function you set the database you want as the one to be used by your app.


CDBForm

This class is derived by CFormView and is the core of the library, usually in the OnInitialUpdate() function you open cursors and set data sources, by doing this you have the form working with its navigation/modify capabilities.

Class member functions:

void AddCheckCtrl( UINT Id, CString field, UINT flags = FLAG_EDIT );
Associates the Idcheck box to the field of the first opened table.
parameters:
Id = identifyer of the checkbox control (it must be a logic field)
field = the field you want to edit
flags = the editing status (FLAG_EDIT=edit, FLAG_EDIT_R_ONLY=read only)
void AddComboCtrl( UINT Id, CString field, UINT flags = FLAG_EDIT );
Associates the Id combo box to the field of the first opened table.
parameters:
Id = identifyer of the combobox control (it must be a long integer field)
field = the field you want to edit
flags = the editing status (FLAG_EDIT=edit, FLAG_EDIT_R_ONLY=read only)
void AddEditCtrl( UINT Id, CString field, UINT flags = FLAG_EDIT );
Associate the Id edit control to the field of the first opened table.
parameters:
Id = identifyer of the edit control
field = the field you want to edit
flags = the editing status (FLAG_EDIT=edit, FLAG_EDIT_R_ONLY=read only)
void AddListCol( UINT Id, CString field, int width, UINT flags = FLAG_EDIT );
Add a column to the specifyed grid
parameters:
Id = identifyer of the grid
Field = the field you want to edit
width = width of the column in pixels.
flags = the editing status (FLAG_EDIT=edit, FLAG_EDIT_R_ONLY=read only)
void AddListColCbo( UINT Id, CString field, int width, int nCur, UINT flags = FLAG_EDIT );
Add a column to the specifyed grid
parameters:
Id = identifyer of the grid.
Field = the field you want to edit.
width = width of the column in pixels.
nCur = combo box cursor number.
flags = the editing status (FLAG_EDIT=edit, FLAG_EDIT_R_ONLY=read only)
CDBrs * Cursor( int i = 0 );
return the pointer of an open cursor, remember that this is derived from CDaoRecordset so you can use all the functions of the base class.
BOOL Editing( void );
Return the editing status.
void EnableEditing( BOOL E );
Enable / disable edit status (for internal use).
void FillCombo( UINT Id, int nCur);
Fill the combo box with an open cursor, the cursor must have the first field set as the description you want to appear in the list, and as second field the one associated to the item (must be a long integer).
parameters:
Id = identifyer of the combobox control
NCur = Cursor number of the filling recordset
int NCursor( void );
return the number of open cursors.
void OpenCursor( UINT Id, int nCur);
Open specifyed cursor to use in the form
Id = identifyer of the combobox control
NCur = Cursor number of the filling recordset
void PrintPreview( CString s);
You can easily customize this command to run the desired report, e.g. Crystal Report â


CDBrs

This class is derived by CDAORecordsetso you can use all the functions of the base class to accomplish every task you want, to open a CDBrs, you should call the OpenCursor Function of CDBForm, because the form will take care of closing it and deleting the allocated memory, if you want to access it you can use the Cursor Function.


CDBcombo

This class is derived by CComboBox and is used by the framework to select a record of a table, usually you should not need to handle it directly and like all the rest of controls you set its parameters in the OnInitialUpdate().

void FillCombo( CDBrs * rs);

Used by the framework, fills the combo box with the desired recordset, the recordset must respect certain rules:
  • the first field must be a char thpe
  • the second field must be a numeric type whose value will be the index for the control.

void LocateOnValue(CString v)

Used by the framework, select the item passed as string.


CDBChildFrame

This Calss is used by the framework to provide the command buttons used to navigate and edit the your table, usually you should not need to handle it directly because it is automatically created by the function OpenForm( CRuntimeClass* prt, CString Title).


CDBEdit

This class is derived by CEdit and is used by the framework to edit values of a field in a table, it has no particolar member functions to be used and usually you should not need to handle it directly and like all the rest of controls you set its parameters in the OnInitialUpdate() .


CDBGrid

Derived by CListCtrl, CDBGrid is the base class for the grid control, usually you should not need to handle it directly and like all the rest of controls you set parameters in the OnInitialUpdate() .

Usage: you can use this control component arranging a ListCtrl in the newly created form and using the AddListCtrl then adding columns with AddListCol.


ADBWizard

This is a simple program designed to help you changing the base class of the newly created form, from CformView to CDBForm, to accomplish this task follow the next steps:

copy ADBWizard.exe in your project directory.

Run ADBWizard.

Press Refresh button (in the list box are shown the files containing the word CformView).

Select the files you want to change.

Press Process button.

Now in the source files ".h" and ".cpp" you selected the class "CFormView" is changed to "CDBForm".

NOTE: perform this action after creating the function OnInitialUpdate().


Form Usage

Here is a list of the commands and buttons for CDBForm and grid.

FORM COMMANDS
Command | Description
 | Go to the first record
 | Go to the previous record
 | Go to the next record
 | Go to the Last record
 | Add a record
 | Delete a record
 | Edit record
 | Save changes
 | Undo changes
 | To be used to print reports (you should provide the handler)
 | close form

GRID COMMANDS
Key | Description
Arrows | Move up-down-left-right.
Enter | when in edit mode activate control: edit, combo etc.
Esc | when editing, undo the changes.
Ctrl+Del | Delete current row in edit mode.


System requirements

ADBLib is a library designed for Microsoftâ Visual C 6.0 and DAO library, no additional requirements.


License agreements

ADBLib.dll is a Braccini Alessandro property, it is free for personal use, but it can be distributed with your commercial applications only if regularly purchased via internet at the address http://centroin.it/~ale-b/indexE.html.

NOTE : any damage derived by improper usage or malfunction will not be refound.