Vc ++ and ODBC (data base connection) | Create database in MS Access link to the database with vc++ through ODBS and perform ADDNEW, MODIFY, DELETE

OBJECTIVE :
To create a database in MS ACCESS and to link the database with VC++ through ODBC and to perform ADDNEW, MODIFY, DELETE records from a table in the database.
PROCEDURE:
PROCEDURE TO CREATE A DATABASE:
1) Start -> programs -> Microsoft office -> Microsoft Access.
2) Select new -> blank database ->type the name of the database as COLLEGE -> click create.
3) Double click on select create table in design view type the following field names .
Name text
Branch text
Age number
( select the data type by clicking on the cell )
4) Close the table with a table name student.

5) Give some sample data in the table student.

PROCEDURE TO CREATE ODBC CONNECTIVITY:
6) Start -> settings -> control panel ->administrative tools -> data sources (ODBC).
7) Double click on Data sources (ODBC) -> user DSN -> add.
8) Double click drive to Microsoft Access (mdb) -> ODBC Microsoft Access setup dialog will appear.
9) Type the data source name -> DNS COLLEGE.
10) Click select … select the college mdb (database) into its path and press ok.
11) Press ok and close the ODBC dialog now the ODBC connection has been established.

PROCEDURE TO CREATE THE PROGRAM:
12) Run the MFC AppWizard (exe) to generate c:\odbc.
13) In the step1 select single document interface.
14) In step 2 select database view without file support.
15) Select data source -> database options window will appear.
16) Select ODBC ->DNSCOLLEGE , record set type -> DYNASET ->ok.
17) Select student table.
18) Deselect printing and print preview, press FINISH.
19) Select -> resource view -> IDR_MAINFRAME
20) Add three submenus in record add new(ID_RECORD_ADDNEW), modify record(ID_RECORD_MODIFY), delete record (ID_RECORD_DELETE),
21) Select -> dialog -> IDD_ODBC_FORM.
22) Insert 3 edit controls on the dialogand a command ( with caption clear field )
23) Using class wizard map the command message of the CODBC view class with all the submenus ID_RECORD_ADDNEW, ID_RECORD_MODIFY, ID_RECORD_DELETE, map the BN_CLICKED with IDC_BUTTON1 and map On Move with CODBC View.
24) Using class wizard create member variable for IDC_EDIT1, IDC_EDIT2, IDC_EDIT3 by selecting the variable names from the popup m_pset->m_name, m_pset->m_branch, m_pset->m_age, with suitable data type CString, CString, long.
25) Now the data form the table will be linked with edit controls of the dialog.
26) Edit the CODBCView.cpp with the following information in OnMove, On Record Delete, On Record Modify, On Button1 functions.
void COdbc2View::OnRecordAddnew()
{
// TODO: Add your command handler code here
m_pSet->AddNew();
UpdateData(true);
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
if(!m_pSet->IsEOF())
{
m_pSet->MoveLast();
}
m_pSet->Requery();
UpdateData(false);
}
void COdbc2View::OnRecordDelete()
{
// TODO: Add your command handler code here
CRecordsetStatus status;
try
{
m_pSet->Delete();
}
catch(CDBException *c)
{
AfxMessageBox(c->m_strError);
c->Delete();
m_pSet->MoveFirst();
UpdateData(false);
return;
}
m_pSet->GetStatus(status);
if(status.m_lCurrentRecord==0)
{
m_pSet->MoveFirst();
}
else
{
m_pSet->MoveNext();
}
UpdateData(false);
}
void COdbc2View::OnRecordModify()
{
// TODO: Add your command handler code here
m_pSet->Edit();
UpdateData(true);
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
void COdbc2View::OnButton1()
{
// TODO: Add your control notification handler code here
m_pSet->SetFieldNull(NULL);
UpdateData(false);
}
BOOL COdbc2View::OnMove(UINT nIDMoveCommand)
{
// TODO: Add your specialized code here and/or call the base class
switch(nIDMoveCommand)
{
case ID_RECORD_PREV:
m_pSet->MovePrev();
if(!m_pSet->IsBOF())
break;
case ID_RECORD_FIRST:
m_pSet->MoveFirst();
break;
case ID_RECORD_NEXT:
m_pSet->MoveNext();
if(!m_pSet->IsEOF())
break;
if(!m_pSet->CanScroll())
{
m_pSet->SetFieldNull(NULL);
break;
}
break;
case ID_RECORD_LAST:
m_pSet->MoveLast();
break;
default:
ASSERT(false);
}
UpdateData(false);
return true;
return CRecordView::OnMove(nIDMoveCommand);
}

27) Compile, run and test the applications.




PROGRAM :
SOURCE FILES :
Odbcview.cpp :
// odbc2View.cpp : implementation of the COdbc2View class
//
#include "stdafx.h"
#include "odbc2.h"
#include "odbc2Set.h"
#include "odbc2Doc.h"
#include "odbc2View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COdbc2View
IMPLEMENT_DYNCREATE(COdbc2View, CRecordView)
BEGIN_MESSAGE_MAP(COdbc2View, CRecordView)
//{{AFX_MSG_MAP(COdbc2View)
ON_COMMAND(ID_RECORD_ADDNEW, OnRecordAddnew)
ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
ON_COMMAND(ID_RECORD_MODIFY, OnRecordModify)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COdbc2View construction/destruction
COdbc2View::COdbc2View()
: CRecordView(COdbc2View::IDD)
{
//{{AFX_DATA_INIT(COdbc2View)
m_pSet = NULL;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
COdbc2View::~COdbc2View()
{
}
void COdbc2View::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COdbc2View)
DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_name, m_pSet);
DDX_FieldText(pDX, IDC_EDIT2, m_pSet->m_age, m_pSet);
DDX_FieldText(pDX, IDC_EDIT3, m_pSet->m_branch, m_pSet);
//}}AFX_DATA_MAP
}
BOOL COdbc2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWindow(cs);
}
void COdbc2View::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_odbc2Set;
CRecordView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// COdbc2View diagnostics
#ifdef _DEBUG
void COdbc2View::AssertValid() const
{
CRecordView::AssertValid();
}
void COdbc2View::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
COdbc2Doc* COdbc2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COdbc2Doc)));
return (COdbc2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COdbc2View database support
CRecordset* COdbc2View::OnGetRecordset()
{
return m_pSet;
}
/////////////////////////////////////////////////////////////////////////////
// COdbc2View message handlers
void COdbc2View::OnRecordAddnew()
{
// TODO: Add your command handler code here
m_pSet->AddNew();
UpdateData(true);
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
if(!m_pSet->IsEOF())
{
m_pSet->MoveLast();
}
m_pSet->Requery();
UpdateData(false);
}
void COdbc2View::OnRecordDelete()
{
// TODO: Add your command handler code here
CRecordsetStatus status;
try
{
m_pSet->Delete();
}
catch(CDBException *c)
{
AfxMessageBox(c->m_strError);
c->Delete();
m_pSet->MoveFirst();
UpdateData(false);
return;
}
m_pSet->GetStatus(status);
if(status.m_lCurrentRecord==0)
{
m_pSet->MoveFirst();
}
else
{
m_pSet->MoveNext();
}
UpdateData(false);
}
void COdbc2View::OnRecordModify()
{
// TODO: Add your command handler code here
m_pSet->Edit();
UpdateData(true);
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
void COdbc2View::OnButton1()
{
// TODO: Add your control notification handler code here
m_pSet->SetFieldNull(NULL);
UpdateData(false);
}
BOOL COdbc2View::OnMove(UINT nIDMoveCommand)
{
// TODO: Add your specialized code here and/or call the base class
switch(nIDMoveCommand)
{
case ID_RECORD_PREV:
m_pSet->MovePrev();
if(!m_pSet->IsBOF())
break;
case ID_RECORD_FIRST:
m_pSet->MoveFirst();
break;
case ID_RECORD_NEXT:
m_pSet->MoveNext();
if(!m_pSet->IsEOF())
break;
if(!m_pSet->CanScroll())
{
m_pSet->SetFieldNull(NULL);
break;
}
break;
case ID_RECORD_LAST:
m_pSet->MoveLast();
break;
default:
ASSERT(false);
}
UpdateData(false);
return true;
return CRecordView::OnMove(nIDMoveCommand);
}

Odbcdoc.cpp :
// odbc2Doc.cpp : implementation of the COdbc2Doc class
//
#include "stdafx.h"
#include "odbc2.h"
#include "odbc2Set.h"
#include "odbc2Doc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COdbc2Doc
IMPLEMENT_DYNCREATE(COdbc2Doc, CDocument)
BEGIN_MESSAGE_MAP(COdbc2Doc, CDocument)
//{{AFX_MSG_MAP(COdbc2Doc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COdbc2Doc construction/destruction
COdbc2Doc::COdbc2Doc()
{
// TODO: add one-time construction code here
}
COdbc2Doc::~COdbc2Doc()
{
}
BOOL COdbc2Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// COdbc2Doc diagnostics
#ifdef _DEBUG
void COdbc2Doc::AssertValid() const
{
CDocument::AssertValid();
}
void COdbc2Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COdbc2Doc commands

HEADER FILES :

Odbcview.h :
// odbc2View.h : interface of the COdbc2View class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_ODBC2VIEW_H__C865C844_0839_466B_B4AA_484940B18BE2__INCLUDED_)
#define AFX_ODBC2VIEW_H__C865C844_0839_466B_B4AA_484940B18BE2__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class COdbc2Set;
class COdbc2View : public CRecordView
{
protected: // create from serialization only
COdbc2View();
DECLARE_DYNCREATE(COdbc2View)
public:
//{{AFX_DATA(COdbc2View)
enum { IDD = IDD_ODBC2_FORM };
COdbc2Set* m_pSet;
//}}AFX_DATA
// Attributes
public:
COdbc2Doc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(COdbc2View)
public:
virtual CRecordset* OnGetRecordset();
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual BOOL OnMove(UINT nIDMoveCommand);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual void OnInitialUpdate(); // called first time after construct
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~COdbc2View();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(COdbc2View)
afx_msg void OnRecordAddnew();
afx_msg void OnRecordDelete();
afx_msg void OnRecordModify();
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in odbc2View.cpp
inline COdbc2Doc* COdbc2View::GetDocument()
{ return (COdbc2Doc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_ODBC2VIEW_H__C865C844_0839_466B_B4AA_484940B18BE2__INCLUDED_)

Odbdoc.h :
// odbc2Doc.h : interface of the COdbc2Doc class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_ODBC2DOC_H__6FE5F421_C65C_44B3_818A_CD4C12718C8E__INCLUDED_)
#define AFX_ODBC2DOC_H__6FE5F421_C65C_44B3_818A_CD4C12718C8E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "odbc2Set.h"
class COdbc2Doc : public CDocument
{
protected: // create from serialization only
COdbc2Doc();
DECLARE_DYNCREATE(COdbc2Doc)
// Attributes
public:
COdbc2Set m_odbc2Set;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(COdbc2Doc)
public:
virtual BOOL OnNewDocument();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~COdbc2Doc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(COdbc2Doc)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_ODBC2DOC_H__6FE5F421_C65C_44B3_818A_CD4C12718C8E__INCLUDED_)


Screen shot output

Vc ++ and ODBC (data base connection) | Create database in MS Access link to the database with vc++ through ODBS and perform ADDNEW, MODIFY, DELETE

Related post