Adbdmod.exe Demonstrates Using ADO VC++ Binding (190961)



The information in this article applies to:

  • ActiveX Data Objects (ADO) 2.0
  • ActiveX Data Objects (ADO) 2.1
  • ActiveX Data Objects (ADO) 2.5
  • ActiveX Data Objects (ADO) 2.6
  • ActiveX Data Objects (ADO) 2.7

This article was previously published under Q190961

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 Download Center:
For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

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;");
				
You must change the version of the Jet provider to 4.0 (Provider=Microsoft.JET.OLEDB.4.0) if MDAC 2.0 has not been installed on your computer previously. If you are installing MDAC 2.6, and this is the first version of MDAC that you are applying, you may not have any Jet provider on your system because MDAC 2.6 does not ship Jet Components. Contact Microsoft Support to acquire a package for Jet Components.

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.

  1. 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.
  2. 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.
  3. 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.

Modification Type:MinorLast Reviewed:3/2/2005
Keywords:kbdownload kbDatabase kbDTL kbfile kbhowto kbSample kbtemplate KB190961 kbAudDeveloper