Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hello,

I am creating MFC project in vc++ 6.0. I choose access databse at the time of creation of new project following steps:

1] New ->MFC AppWizard(exe) -> Multiple document -> Database view with file support
2]Data Source (select) -> ODBC ->MS Access Database -> Database.accdb -> select table.

My project is created successfully . But now I want to change that access database to other access database in the same VC++ 6.0 project. Which are the steps for it. Please help
Posted
Updated 2-Jan-16 23:17pm
v2
Comments
Arthur V. Ratz 3-Jan-16 6:04am    
I think that, you have to find the connection string declared in the code generated by Visual Studio Project Creation Wizard and manually change the path to the database file
Kishor-KW 3-Jan-16 7:11am    
Thank you...It helps
Arthur V. Ratz 3-Jan-16 7:12am    
Really, I'm ready to help you again. Thanks for your opinion.

For example, Here you'll have to change DBQ=C:\\Users\\Comp\\Desktop\\DB1.mdb; to DBQ=C:\\Users\\Comp\\Desktop\\DB_NEW.mdb;

C++
CString CMFCApplication1Set::GetDefaultConnect()
{
return _T("DBQ=C:\\Users\\Comp\\Desktop\\DB1.mdb;DefaultDir=C:\\Users\\Comp\\Desktop;Driver={Driver do Microsoft Access (*.mdb)};DriverId=25;FIL=MS Access;FILEDSN=C:\\Users\\Comp\\Documents\\TestDS_NEW.dsn;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;");
}


Also, if you need to provide access to multiple databases, you'll have to manually add similar functionality to the one generated by VSS's project wizard.

Also if you need here's link to ODBC API Reference
 
Share this answer
 
v3
Comments
Kishor-KW 3-Jan-16 7:10am    
CString CProductSet::GetDefaultConnect()
{
// return _T("ODBC;DSN=Sample Data");

return _T("DBQ=D:\\Shifting\\down\\57(bookeg)\\Chapter 20\\DBSample");
}

CString CProductSet::GetDefaultSQL()
{
return _T("[Products]");
}

above is my connectionstring
(return _T("ODBC;DSN=Sample Data");
(this is old one which I commented)
Is this right because it is giving error (abort,retry,ignore type)
Kishor-KW 3-Jan-16 7:10am    
What changes should I do if database is SQL?
Arthur V. Ratz 3-Jan-16 7:11am    
Have you tried what I recommended ?
Kishor-KW 3-Jan-16 7:15am    
yes I did, error is Debug Assertion Failed
Arthur V. Ratz 3-Jan-16 7:16am    
And what error in particular ?
C++
#define LVCOLUMNS 3

static LVCOLUMN lpSampleDBListColumns[] = 
	{ { LVCF_TEXT | LVCF_WIDTH, 0, 30, _T("CustID"),   256, 0 },
	  { LVCF_TEXT | LVCF_WIDTH, 0, 30, _T("CustName"), 256, 0 },
	  { LVCF_TEXT | LVCF_WIDTH, 0, 30, _T("CustQty"),  256, 0 } };

BOOL CSampleDBAppDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	CListCtrl* pListCtrl = NULL;
	if ((pListCtrl = (CListCtrl*)GetDlgItem(IDC_SAMPLEDBLIST)) != NULL)
	{
		CRect lvRect; pListCtrl->GetClientRect(&lvRect);
		int nColumnsCount = sizeof(lpSampleDBListColumns) / sizeof(LVCOLUMN);
		for (int iIndex = 0; iIndex != nColumnsCount; iIndex++)
		{
			lpSampleDBListColumns[iIndex].cx = lvRect.Width() / nColumnsCount;
			pListCtrl->InsertColumn(iIndex, &lpSampleDBListColumns[iIndex]);
		}
	}

	CDatabase* pdb = new CDatabase();
	if (!pdb->OpenEx(_T("DBQ=E:\\SampleDB.mdb;DefaultDir=E:\\;Driver={Driver do Microsoft Access (*.mdb)}; \
		DriverId=25;FIL=MS Access;FILEDSN=E:\\TestDB0.dsn;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;")))
	{
		TRACE0("Unable to connect to database using the data source");
	}

	CRecordset* pRecordSet = new CRecordset(pdb);
	if (!pRecordSet->Open(AFX_DB_USE_DEFAULT_TYPE, _T("SELECT * FROM CUSTOMERS")))
	{
		TRACE0("Unable to open recordset");
	}

	pRecordSet->MoveFirst();
	for (int iRow = 0; !pRecordSet->IsEOF(); iRow++)
	{
		CDBVariant varField[3];
		pRecordSet->GetFieldValue((short)0, varField[0]);

		_TCHAR szField0[256] = _T("\0");
		wsprintf(szField0, _T("%d"), varField[0].m_iVal);
		pListCtrl->InsertItem(LVIF_TEXT | LVIF_STATE, iRow, szField0, 0, 0, 0, 0);

		pRecordSet->GetFieldValue((short)1, varField[1]);
		pListCtrl->SetItemText(iRow, 1, static_cast<const>(*varField[1].m_pstringW));

		pRecordSet->GetFieldValue((short)2, varField[2]);

		_TCHAR szField2[256] = _T("\0");
		wsprintf(szField2, _T("%d"), varField[2].m_iVal);
		pListCtrl->SetItemText(iRow, 2, szField2);

		pRecordSet->MoveNext();
	}

	pRecordSet->Close();
	pdb->Close();

	return TRUE;  // return TRUE  unless you set the focus to a control
}</const>
 
Share this answer
 
Like this:

C++
pRecordSet->MoveFirst();
for (int iRow = 0; !pRecordSet->IsEOF(); iRow++)
{
     CDBVariant varField[3];
     pRecordSet->GetFieldValue((short)0, varField[0]);

     _TCHAR szField0[256] = _T("\0");
     wsprintf(szField0, _T("%d"), varField[0].m_iVal);
     pListCtrl->InsertItem(LVIF_TEXT | LVIF_STATE, iRow, szField0, 0, 0, 0, 0);

     for (short iField = 1; iField < 3; iField++)
     {
	  pRecordSet->GetFieldValue((short)iField, varField[iField]);
	  pListCtrl->SetItemText(iRow, iField, 
            static_cast<const>(*varField[iField].m_pstringW));
     }

     pRecordSet->MoveNext();
}</const>
 
Share this answer
 
Comments
CHill60 5-Mar-16 7:34am    
If someone with a similar problem was to come across this, which of your three answers should they look at?
It's very confusing when members do this. Use the Improve solution link instead ... or are you just points-mining?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900