home *** CD-ROM | disk | FTP | other *** search
- // MDBDoc.cpp : implementation of the CMDBDoc class
- //
-
- #include "stdafx.h"
- #include "MDB.h"
-
- #include "MDBDoc.h"
- #include "MDBView.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBDoc
-
- IMPLEMENT_DYNCREATE(CMDBDoc, CDocument)
-
- BEGIN_MESSAGE_MAP(CMDBDoc, CDocument)
- //{{AFX_MSG_MAP(CMDBDoc)
- ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBDoc construction/destruction
-
- CMDBDoc::CMDBDoc()
- {
- // TODO: add one-time construction code here
-
- }
-
- CMDBDoc::~CMDBDoc()
- {
- }
-
- BOOL CMDBDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
-
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
-
- POSITION pos = GetFirstViewPosition( );
- CTreeView * pView = ( CTreeView * ) GetNextView( pos );
- CTreeCtrl & tree = pView->GetTreeCtrl( );
- tree.DeleteAllItems( );
- SetModifiedFlag( FALSE );
-
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBDoc serialization
-
- void CMDBDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBDoc diagnostics
-
- #ifdef _DEBUG
- void CMDBDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
-
- void CMDBDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CMDBDoc commands
-
-
- BOOL CMDBDoc::SaveModified()
- {
- // TODO: Add your specialized code here and/or call the base class
- if ( ! IsModified( ) )
- return TRUE;
- int answer = AfxMessageBox( "Create database before closing?",
- MB_YESNOCANCEL | MB_ICONQUESTION );
- if ( IDCANCEL == answer )
- return FALSE;
- if ( IDYES == answer )
- {
- OnFileSaveAs( ); //This handler knows what to do
- return ! IsModified( ); //In case of exception
- }
- return TRUE; //No, don't save it
- }
-
- void CMDBDoc::OnFileSaveAs()
- {
- // TODO: Add your command handler code here
-
- POSITION pos = GetFirstViewPosition( );
- CMDBView * pView = ( CMDBView * ) GetNextView( pos );
- if ( 0 == pView->GetTreeCtrl( ).GetCount( ) ) //No tables in the tree
- if ( IDNO ==
- AfxMessageBox("Create empty database?",
- MB_YESNO | MB_ICONQUESTION ) )
- {
- SetModifiedFlag( FALSE );
- return;
- }
-
- CString strDatabaseName;
- CFileDialog dlg( FALSE, "mdb", NULL, OFN_HIDEREADONLY,
- "Access database|*.mdb||" );
-
- if ( IDOK != dlg.DoModal( ) ) //User cancelled, nothing to do
- return;
-
- CString strTableName, strFieldName;
- int nFieldType, nTextFieldLength;
- short nType;
- HTREEITEM hTableItem;
-
- try
- {
- m_daoDB.Create( dlg.GetFileName( ) ); //Create and open database
-
- while ( NULL != //While there is a table
- ( hTableItem = pView->TableNameIterator( strTableName ) ) )
- {
- CDaoTableDef daoTblDef( & m_daoDB );
- daoTblDef.Create( strTableName );
-
- while ( NULL != //While there is a field
- pView->FieldInfoIterator( strFieldName, nFieldType, nTextFieldLength, hTableItem ) )
- {
- nType =
- nFieldType == 0 //text field
- ?
- dbText
- :
- nFieldType == 1 //integer field
- ?
- dbLong
- : //Must be a floating point
- dbDouble;
- daoTblDef.CreateField(
- strFieldName, nType, nTextFieldLength );
-
- } //End while there is a field
-
- daoTblDef.Append( ); //Add the completed table to the database
- daoTblDef.Close( ); //Close the tabledef
- }//End while there is a table
-
- m_daoDB.Close( ); //Close the database
- SetModifiedFlag( FALSE );
- }
- catch ( CDaoException * ex )
- {
- ex->ReportError( );
- try
- {
- m_daoDB.Close( ); //Close the database
- if ( 3204 != ex->m_pErrorInfo->m_lErrorCode ) //Database already exists error
- CFile::Remove( dlg.GetFileName( ) );
- }
- catch ( CException * exInner )
- {
- exInner->ReportError( );
- exInner->Delete( );
- }
- ex->Delete( );
- }
- }
-
-
-