Click here to Skip to main content
15,882,163 members
Articles / Desktop Programming / MFC

CODBCAccess: a CDatabase and CRecordset Wrapper

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
12 Mar 200312 min read 208.9K   4.3K   55  
A class to wrap the use of CDatabase and CRecordset into one object to communicate with databases
// NamePrompt.cpp : implementation file
//

#include "stdafx.h"
#include "ODBCExample.h"
#include "NamePrompt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNamePrompt dialog


CNamePrompt::CNamePrompt(CWnd* pParent /*=NULL*/)
	: CDialog(CNamePrompt::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNamePrompt)
	m_strName = _T("");
	m_strSQL = _T("");
	//}}AFX_DATA_INIT
}


void CNamePrompt::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNamePrompt)
	DDX_Control(pDX, IDC_TABLE, m_ctlTable);
	DDX_Text(pDX, IDC_NAME, m_strName);
	DDX_Text(pDX, IDC_SQL, m_strSQL);
	DDX_Control(pDX, IDC_USESQL, m_ctlRadioSQL);
	DDX_Control(pDX, IDC_USETABLE, m_ctlRadioTable);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CNamePrompt, CDialog)
	//{{AFX_MSG_MAP(CNamePrompt)
	ON_BN_CLICKED(IDC_USESQL, OnUsesql)
	ON_BN_CLICKED(IDC_USETABLE, OnUsetable)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNamePrompt message handlers

void CNamePrompt::OnOK() 
{
	UpdateData(true);
	int useSQL = m_ctlRadioSQL.GetCheck();
	if(m_strName == _T(""))
	{
		MessageBox("You must enter a name to continue.", "No Name Entered", MB_ICONEXCLAMATION);
		return;
	}
	if(useSQL == 1 && m_strSQL == _T(""))
	{
		MessageBox("Please enter an SQL statement if you want to use one for the set results.",
			"No SQL Statement Entered", MB_ICONEXCLAMATION);
		return;
	}

	//Set table name if applies:
	if(useSQL == 0)
	{
		//BUG Fix: spotted by Yuval Detinis:
		//If m_ctlTable has no current selection, it will ASSERT -->

		if(m_ctlTable.GetCurSel() < 0)
		{
			//Alert the user and return:
			MessageBox("You must select a table to open!  Please select a table and then try again.", "No Table Selected", MB_ICONEXCLAMATION);
			return;
		}

		m_ctlTable.GetLBText(m_ctlTable.GetCurSel(), sTable);
	}
	m_bUseSQL = useSQL;

	//Finished validating:
	CDialog::OnOK();
}

void CNamePrompt::OnUsesql() 
{
	UpdateData(true);
	BOOL use = m_ctlRadioSQL.GetCheck();
	GetDlgItem(IDC_SQL)->EnableWindow(use);
	GetDlgItem(IDC_TABLE)->EnableWindow(!use);
}

void CNamePrompt::OnUsetable() 
{
	UpdateData(true);
	BOOL use = m_ctlRadioTable.GetCheck();
	GetDlgItem(IDC_SQL)->EnableWindow(!use);
	GetDlgItem(IDC_TABLE)->EnableWindow(use);
}

BOOL CNamePrompt::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//Get the tables from the open database:
	db->QueryOpenTables();

	//Get pointer to it:
	CTables* table;
	db->GetTables(table);

	//Loop through to add them to the combo box:
	for(int h = 0; h < table->GetRecordCount(); h++)
	{
		//Add this one:
		m_ctlTable.AddString(table->m_strTableName);

		//Move to the next table name:
		table->MoveNext();
	}

	//The tables set is automatically closed when Disconnect is called OR it is closed
	//and reopened when QueryOpenTables is called again

	//Disable or enable the correct controls:
	m_ctlRadioSQL.SetCheck(1);
	m_ctlRadioTable.SetCheck(0);
	OnUsesql();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
I work with C++, C#, Perl, PHP, HTML (with a lot of CSS), and a little bit of ASP.NET. I have been programming for 7 years.

I also enjoy translating Latin, and especially reading authors whose primary language was Latin, including Virgil (or Vergil for the Latinate spelling), Julius Caesar, Catullus, Cicero, and some others (like Martial). I enjoy classical music, most especially Mozart and Bach.

Comments and Discussions