SAMPLE: Adbdmod.exe Demonstrates Using ADO 2.0 VC++ Binding

ID: Q190961

The information in this article applies to:

SUMMARY

Adbdmod.exe is a sample file that demonstrates how to convert a dialog created by the ActiveX Data Objects (ADO) Data Bound Dialog Component into a dialog that allows adding, updating, and deleting records using the ADO C++ bindings.

MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

 ~ Adbdmod.exe (size: 49083 bytes) 

Release Date: 08-06-1998

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE : How to Obtain Microsoft Support Files from Online Services

With ActiveX Data Objects (ADO) 2.0, you can bind recordset data members to a C++ structure. ADO synchronizes the C++ structure with data from the current record when you move from record to record in the recordset. Visual C++ 6.0 has an ADO Data Bound Dialog Component that creates a read-only ADO data bound dialog for a particular select statement or table name.

You can also add new records and modify existing records with the ADO 2.0 C++ bindings.

In order to run the sample, you must modify the m_strConnection assignment in the dialog constructor in the RsCgDlg.cpp file:

   CRsCgDlg::CRsCgDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CRsCgDlg::IDD, pParent)
   {
   // TO DO: Change m_strConnection to point to location of
   // ADBDMod.mdb database file.
   m_strConnection = _T("Provider=Microsoft.JET.OLEDB.3.51;"
              "Data Source=C:\\ADBDMod\\ADBDMod.mdb;");

Change the Data Source to point to the Adbdmod.mdb file on your computer.

Use the following instructions to add a read-only ADO Data Bound Dialog to a Visual C++ project:

1. From the Project menu, choose Add To Project, and then select Components

   and Controls.

2. Open the "Visual C++ Components" folder and select the "ADO Data Bound
   Dialog".

3. Click Insert and then click OK.

4. Type in an ODBC or OLE DB connection string, or use the Build button to

   create a connection string using the Microsoft Data Links dialog box.

5. Click Next and type a SQL statement in the Command box or click the
   Table option button to choose from a list of tables in the database and
   then select Next.

6. Choose the Cursor Location and the Cursor Type desired. Note that it is
   easy to change these options later in the source code.

7. Choose Next and then Finish to complete the process.

You should now have a RsCgDlg.cpp file and a RsCgDlg.h file that contains all of the code to create an ADO bound dialog box. The wizard also adds a new dialog resource to your project that contains the text boxes and navigation buttons.

In order to allow updating of data, you need to make several modifications to the wizard generated class files.

Steps to Modify Class Files

1. Modify the ADO binding macros to allow updating of each field. All you

   need to do here is change the last parameter (the Modify parameter) of
   the field entry macro from FALSE to TRUE:

      // BEFORE
      ADO_FIXED_LENGTH_ENTRY(1, adInteger, m_lProductID,
         lProductIDStatus, FALSE)

      // AFTER (Changed Modify macro parameter to TRUE)
      ADO_FIXED_LENGTH_ENTRY(1, adInteger, m_lProductID,
         lProductIDStatus, TRUE)

2. Modify the OnInitDialog() method of the dialog class to open an
   updateable ADO recordset versus the read-only one created by the wizard.
   You will need to change the third and fourth parameters and possibly the
   fifth (last).

      // BEFORE
      m_pRs->Open((LPCTSTR)m_strCmdText, (LPCTSTR)m_strConnection,
         adOpenStatic, adLockReadOnly, adCmdUnknown);

      // AFTER (Changed adOpenStatic to adOpenKeyset and adLockReadOnly
      // to adLockOptimistic)
      m_pRs->Open((LPCTSTR)m_strCmdText, (LPCTSTR)m_strConnection,
         adOpenKeyset, adLockOptimistic, adCmdUnknown);

NOTE: Verify that the last parameter of Open is adCmdUnknown or adCmdText and not adCmdTableDirect.

3. Create a class member variable for an IADORecordBinding pointer (in the

   CRsCgDlg class) and use the class member pointer to set up the ADO
   binding. Note that the wizard initializes a local IADORecordBinding
   pointer named pAdoRecordBinding in the OnInitDialog() method. This
   sample uses the wizard generated code to initialize a class scope
   IADORecordBinding pointer named m_piAdoRecordBinding.

   You need to cache the IADORecordBinding pointer because you need this
   pointer to trigger Updates and AddNews to the recordset. Calling AddNew
   or Update on the ADO recordset pointer m_pRs will not update the
   recordset; you must use the AddNew and Update in the IADORecordBinding
   interface to make changes to the recordset.

4. Create a method to move all of the record data from the dialog members
   to the ADO binding members. The sample calls this method,
   PrepareBoundData(). Note the wizard binds the text boxes in the dialog
   to one set of member variables and binds the ADO recordset to another
   set of member variables. This gives you an extra buffer for comparing
   values in the dialog with the actual values in the ADO recordset. The
   sample adds an additional member function to the class called IsDirty,
   which demonstrates how to check the dialog buffer against the ADO
   binding buffer.

5. Next, create SaveRecord and AddRecord methods that call the
   PrepareBoundData() function to move the data from the dialog members
   into the ADO binding members and then call the appropriate
   IADORecordBinding interface method to add or update the record.

Additional query words: kbDatabase kbOLEDB kbtemplate kbDTL kbADO200
Version           : WINDOWS:2.0
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: August 11, 1998