Click here to Skip to main content
15,891,777 members
Articles / Desktop Programming / MFC

MsAccess MdbTools with MFC and .NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (9 votes)
13 Jan 2012LGPL310 min read 69.2K   9.9K   49  
Viewer of MsAccess databases directly from MFC and .NET - Repair corrupt databases
// MFCMdbToolsTestAppDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCMdbToolsTestApp.h"
#include "MFCMdbToolsTestAppDlg.h"
#include "afxdialogex.h"
#include <vector>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCMdbToolsTestAppDlg dialog




CMFCMdbToolsTestAppDlg::CMFCMdbToolsTestAppDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMFCMdbToolsTestAppDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCMdbToolsTestAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CBOTABLES, CboTables);
	DDX_Control(pDX, IDC_DATABASE, TxtDatabase);
	DDX_Control(pDX, IDC_LSTDATA, LstData);
}

BEGIN_MESSAGE_MAP(CMFCMdbToolsTestAppDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDCANCEL, &CMFCMdbToolsTestAppDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDOK, &CMFCMdbToolsTestAppDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDOK2, &CMFCMdbToolsTestAppDlg::OnBnClickedOk2)
	ON_CBN_SELCHANGE(IDC_CBOTABLES, &CMFCMdbToolsTestAppDlg::OnCbnSelchangeCbotables)
END_MESSAGE_MAP()


// CMFCMdbToolsTestAppDlg message handlers

BOOL CMFCMdbToolsTestAppDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	LoadPath();
	LoadedDatabase = FALSE;
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMFCMdbToolsTestAppDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMFCMdbToolsTestAppDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CMFCMdbToolsTestAppDlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	if (LoadedDatabase)
		mdbfile.Close();
	exit(0);
}


void CMFCMdbToolsTestAppDlg::OnBnClickedOk()
{
	CMFCMdbTable* mdbt;
	CString cad;

	TxtDatabase.GetWindowTextA(cad);

	if (LoadedDatabase)
		mdbfile.Close();

	BOOL isOK = mdbfile.Open(cad);

	if (!isOK)
	{
		AfxMessageBox("Cannot open database file");
		return;
	}
	mdbfile.LoadTables();
		
	CboTables.ResetContent();
	int index = 0;

	mdbfile.MoveFirst();

	do 
	{
		mdbt = (CMFCMdbTable*)mdbfile.GetItem();
		CboTables.InsertString(index, mdbt->Name);
		index++;
	} while (mdbfile.MoveNext());
	LoadedDatabase = TRUE;
}
void CMFCMdbToolsTestAppDlg::LoadPath()
{
	TCHAR temp[200];
	GetModuleFileName(AfxGetApp()->m_hInstance,temp,200);
	CString strDir = temp;
	strDir = strDir.Trim();
	CString strApp = AfxGetAppName();
	strDir = strDir.Left(strDir.GetLength() - 4 - strApp.GetLength());
	AppPath = L"";
	AppPath.Append(strDir);
}

void CMFCMdbToolsTestAppDlg::OnBnClickedOk2()
{
	CString file;
	DWORD		dwFlags =	OFN_EXPLORER | OFN_LONGNAMES | OFN_FILEMUSTEXIST |
							OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
				MAXFILE = 2562; //2562 is the max
	
	CFileDialog	dlg(TRUE, NULL, file, dwFlags, "Archivos mdb|*.mdb|All files (*.*)|*.*||");
	dlg.m_ofn.lpstrInitialDir = AppPath;
	dlg.m_ofn.lpstrTitle = "Seleccione el archivo a abrir";
	
	int erg = dlg.DoModal();
	if( erg != IDOK )
		return;
	
	CString NombreDb = DarNomDb(dlg.m_ofn.lpstrFile);
	TxtDatabase.SetWindowTextA(NombreDb);
}
CString CMFCMdbToolsTestAppDlg::DarNomDb(CString NomArch)
{
	if (NomArch.Right(4) != ".mdb") NomArch.Append(".mdb");
	return NomArch;

}

void CMFCMdbToolsTestAppDlg::OnCbnSelchangeCbotables()
{
	CString cad;
	CboTables.GetWindowTextA(cad);
		
	CMFCMdbTable* t = (CMFCMdbTable*)mdbfile.GetItemByName(cad);

	t->ClearTableColumns();
	t->LoadTableColumns();

	LstData.SetRedraw(FALSE);

	//Delete all items
	LstData.DeleteAllItems();

	//Delete all Columns
	while(LstData.DeleteColumn(0));

	CMFCMdbColumn* col;
	int i = 0;

	t->MoveFirst();

	do
	{
		col = (CMFCMdbColumn*)t->GetItem();
		LstData.InsertColumn(i, col->Name,0,150);
		i++;
	} while (t->MoveNext());

	t->CursorMoveFirst();

	int pos;
	i = 0;
	while (t->CursorNextItem())
	{
		int j = -1;
		t->MoveFirst();
		do 
		{
			col = (CMFCMdbColumn*)t->GetItem();
			CString fvalue = FormatValue(t->GetValue(col->Name), col);
			if (j == -1)
				pos = LstData.InsertItem(i, fvalue);
			else
				LstData.SetItem(pos,j+1, LVIF_TEXT, fvalue, 0, 0, 0, 0);

			j++;
		} while (t->MoveNext());
		i++;
	}
	t->ClearTableColumns();
	LstData.SetRedraw(TRUE);
}
CString CMFCMdbToolsTestAppDlg::FormatValue(CMFCMdbValue* pvalue, CMFCMdbColumn* pcol)
{
	CString cad;
	if (pcol->IsInt())
	{
		cad.Format("%d", pvalue->GetIntValue());
		return cad;
	}

	if (pcol->IsBinary())
	{
		if (pcol->GetType() == MdbLib_MEMO)
			return pvalue->Value;
		else
		{
			cad.Format("Bin Len %d", pvalue->GetLen());
			return cad;
		}
	}

	if (pcol->IsNumDec())
	{
		cad.Format("%f", pvalue->GetDoubleValue());
		return cad;
	}

	return pvalue->Value;
}

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, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
Argentina Argentina
System developer from Argentina.

Programmed in VB 5,6,.NET, C#, Java, PL-SQL, Transac-SQL, C, C++ and even some "calculator" language.

Love to build small, useful applications.
Usually building big and complicated apps based on solid, reliable components.

Hobbies: reading, photography, chess, paddle, running.

Comments and Discussions