I think I am having a db closure issue. I am running a query from a dialog interface and inputing the info into a file and then displaying into a list box. This works fine, until a I use another statment execution from another dialog to a different list box. The data remains the same. I am accessing the same db table for the different info, am I just getting the same query data over and over? Here's a sample of the code being used when clicking different buttons in the same dialog.
Code:
void FCHVMUNIT::OnButton3()
{
//declare variables
CRecordset rec;
CDatabase db;
CString sz_statement;
CString s_name;
CString s_id;
CString s_department;
CString s_equipType;
CString s_equipNumber;
CString s_equipName;
CString s_status;
CString s_employee;
CString s_employeeId;
CString s_dateTime;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "C:\\HEBMH.mdb";
CString singlequote = "'";
CString percentquote = "%'";
CString selectStatmentEquip= "SELECT * from Equipment WHERE equiptype like 'HVM%' and status = 'a'";
sDsn.Format("ODBC;DRIVER={%s};DSN=HEBMH;DBQ=%s",sDriver,sFile);
try
{
db.Open(sDsn);
}
catch(CDBException *e)
{
e->Delete ();
}
rec.m_pDatabase = &db;
ofstream fout;
fout.open ("C:\\mydbresults.txt", ios::app);
rec.m_pDatabase = &db;
s_id ="";
s_id += selectStatmentEquip;
sz_statement += s_id;
try
{
rec.Open(CRecordset::forwardOnly, sz_statement);
while(!rec.IsEOF())
{
rec.GetFieldValue("EQUIPTYPE", s_equipType);
fout<<s_equipType<<" \t";
rec.GetFieldValue("EQUIPNUMBER", s_equipNumber);
fout<<s_equipNumber<<" \t";
rec.GetFieldValue("EQUIPNAME", s_equipName);
fout<<s_equipName<<" \t";
rec.GetFieldValue("STATUS", s_status);
fout<<s_status<<" \t";
rec.GetFieldValue("EMPLOYEE", s_employee);
fout<<s_employee<<" \t";
rec.GetFieldValue("EMPLOYEEID", s_employeeId);
fout<<s_employeeId<<"\t";
rec.GetFieldValue("TIMESTAMP", s_dateTime);
fout<<s_dateTime<<"\n";
rec.MoveNext();
}
rec.Close();
}
catch (CDBException *e)
{
e->Delete ();
}
db.Close();
// CListBoxSafe::LoadList(m_List1,"C:\\mydbresults.txt",0,0);
ifstream file;
char x;
CString buffer;
//Open file
file.open("C:\\mydbresults.txt", ios::in); //What ever your file is
//Check for file exist
if(!file)
{
CString facts = "File not found";
MessageBox(facts,NULL,MB_OK);
}
//Read data
while(file.get(x))
{
if(x=='\n')
{
m_List1.AddString(buffer);
//MessageBox(buffer,NULL,MB_OK); //Test buffer
buffer="";
}
else
buffer = buffer + x;
}
m_List1.AddString(buffer);
UpdateData(FALSE);
//delete old file
file.close();
DeleteFile ("C:\\mydbresults.txt");
}
Thanks as always, everyone on this site has been super helpful for a newbie like me!