The following code to populate a List Control Box with data from a database when a button is pressed works successfully when the List Control Box is placed on the main IDD_DIALOG. However when the List Control Box and button is placed on a tab, eg IDD_TAB1 the program hangs when the button is pressed.
(The header files "afxdb.h" and "odbcinst.h" are included in both the mainDlg.cpp and Tab1Dlg.cpp)
Any ideas as to why this is happening?
Any help is greatly appreciated.
Aoife
***********************************************************
void CInterfaceDlg::OnMainRead()
{
CDatabase database;
CString SqlString;
CString sNumber, sSender, sDate, sMessage;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "c:\\THE PROJECT\\Interface\\Project_Databases.mdb";
int iRec =0;
//Build ODBC connection string
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
//Open the Database
database.Open(NULL,false,false,sDsn);
//Allocte the Record set
CRecordset recset (&database);
//Build the SQL statement
SqlString = "SELECT Number, Sender, Date, Message "
"FROM pc_Inbox";
//Execute the query
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
//Populate the Grids
ListView_SetExtendedListViewStyle(m_listControl,LVS_EX_GRIDLINES);
//Column width and heading
m_listControl.InsertColumn(0, "SMS No.", LVCFMT_LEFT, -1,0);
m_listControl.InsertColumn(1, "Sender", LVCFMT_LEFT, -1,1);
m_listControl.InsertColumn(2, "Date", LVCFMT_LEFT, -1,2);
m_listControl.InsertColumn(3, "Message", LVCFMT_LEFT, -1,3);
m_listControl.SetColumnWidth(0, 100);
m_listControl.SetColumnWidth(1, 100);
m_listControl.SetColumnWidth(2, 100);
m_listControl.SetColumnWidth(3, 200);
//Loop through each record
while (!recset.IsEOF())
{
//Copy each column into a variable
recset.GetFieldValue("Number",sNumber);
recset.GetFieldValue("Sender",sSender);
recset.GetFieldValue("Date",sDate);
recset.GetFieldValue("Message",sMessage);
//Insert values into the list control
iRec = m_listControl.InsertItem(0,sNumber,0);
m_listControl.SetItemText(0,1,sSender);
m_listControl.SetItemText(0,2,sDate);
m_listControl.SetItemText(0,3,sMessage);
//Go to the next record
recset.MoveNext();
}
//Close the Database
database.Close();
}
CATCH(CDBException, e)
{
//If a database exception occured, show an error message
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}
(The header files "afxdb.h" and "odbcinst.h" are included in both the mainDlg.cpp and Tab1Dlg.cpp)
Any ideas as to why this is happening?
Any help is greatly appreciated.
Aoife
***********************************************************
void CInterfaceDlg::OnMainRead()
{
CDatabase database;
CString SqlString;
CString sNumber, sSender, sDate, sMessage;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "c:\\THE PROJECT\\Interface\\Project_Databases.mdb";
int iRec =0;
//Build ODBC connection string
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
//Open the Database
database.Open(NULL,false,false,sDsn);
//Allocte the Record set
CRecordset recset (&database);
//Build the SQL statement
SqlString = "SELECT Number, Sender, Date, Message "
"FROM pc_Inbox";
//Execute the query
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
//Populate the Grids
ListView_SetExtendedListViewStyle(m_listControl,LVS_EX_GRIDLINES);
//Column width and heading
m_listControl.InsertColumn(0, "SMS No.", LVCFMT_LEFT, -1,0);
m_listControl.InsertColumn(1, "Sender", LVCFMT_LEFT, -1,1);
m_listControl.InsertColumn(2, "Date", LVCFMT_LEFT, -1,2);
m_listControl.InsertColumn(3, "Message", LVCFMT_LEFT, -1,3);
m_listControl.SetColumnWidth(0, 100);
m_listControl.SetColumnWidth(1, 100);
m_listControl.SetColumnWidth(2, 100);
m_listControl.SetColumnWidth(3, 200);
//Loop through each record
while (!recset.IsEOF())
{
//Copy each column into a variable
recset.GetFieldValue("Number",sNumber);
recset.GetFieldValue("Sender",sSender);
recset.GetFieldValue("Date",sDate);
recset.GetFieldValue("Message",sMessage);
//Insert values into the list control
iRec = m_listControl.InsertItem(0,sNumber,0);
m_listControl.SetItemText(0,1,sSender);
m_listControl.SetItemText(0,2,sDate);
m_listControl.SetItemText(0,3,sMessage);
//Go to the next record
recset.MoveNext();
}
//Close the Database
database.Close();
}
CATCH(CDBException, e)
{
//If a database exception occured, show an error message
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
}