Creating a report/table in Visual C++

Hi,

I am writting an application for my boss.... Most of the application is done.  In his review he asked me for a report that has the following characteristics:

Rows of members of our organization, under each member is list of projects they are in charge of or assisting with.  If they are assistanting with it the project name is highlighed, if they are assisted by someone else the project is highlighted.

All of the data to create the report is available via a database I have opened through ODBC.

So the question is -- how do I go about doing this?  Or where can I look for info on this?

BTW:  I would prefer not to spend any additional funds


Nasa
LVL 1
nasaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ViralPurohitCommented:
I would suggest there are couple of ways to write a report,
and will explain them in brief and give you references to get started, Once i had to use all this methods in my application to implemented all reports which suited the particular the best

1) Directly printing to LPT port using fopen method.(example follows)
    If you want to bypass the printing framework of windows and not worry about page size and margins, then this is the best, for example a bill printing function.

2) Using report printing class or activex components like CEasyReports at
    http://www.codeproject.com/miscctrl/easyreports.asp
    Can design good simple reports using this class, see url for demo and code.

3) Using a grid control and adding rows, column and highlight colors accordingly(example follows)
  I am using "MFC Grid control - Chris Maunder" http://www.codeproject.com/miscctrl/gridctrl.asp

4) Crystal Reports  and other such third party solutions
    See this PDF "Getting Started with the Crystal Report Designer Component in
Microsoft Visual C++"
http://support.crystaldecisions.com/communityCS/TechnicalPapers/scr_rdc_cpp.pdf.asp



Here are the code snippets from my application, change variables according to your needs.


Example : Printing a grid/recordset to LPT port directly using fopen
// The form view below has a grid showing bill items and recordset associated with the bill details
void CBillView::OnBnClickedPrint()
{
      int iDet=15;
      CString tmp1,tmp2,tmp3,tmp4;

      FILE *fp = fopen("LPT1", "w");
      EjectPage(fp,true);
      int linecount=0;
      for(int x=1;x<m_Grid.GetRowCount();x++)
      {
            if(linecount==13)
            {
                  linecount=0;
                  EjectPage(fp);
            }
            tmp1.Format("%-22.22s",m_Grid.GetItemText(x,1));
            tmp2.Format("%5s",m_Grid.GetItemText(x,2));
            tmp3.Format("%10s",m_Grid.GetItemText(x,3));
            tmp4.Format("%10s",m_Grid.GetItemText(x,4));

            fprintf(fp,
                  tmp1//item
                  +tmp2+"" //qty
                  +tmp3+"\t\t  " //rate
                  +tmp4 //amt
                  +"\n"
                  );
            linecount++;
      }
      if(linecount==13)
      {
            linecount=0;
            EjectPage(fp);
      }
      fprintf(fp,"\n");      
      linecount++;
      if(linecount==13)
      {
            linecount=0;
            EjectPage(fp);
      }
      tmp1.Format("%-30.30s","");
      tmp2.Format("%-15.15s","Total Amount");
      tmp3.Format("%15s",CCrack::strVARIANT(m_rs.GetFieldValue("total")));
      fprintf(fp,tmp1+tmp2+tmp3+"\n");      
      linecount++;

      if(atof(CCrack::strVARIANT(m_rs.GetFieldValue("discount")))>0)
      {
            if(linecount==13)
            {
                  linecount=0;
                  EjectPage(fp);
            }
            tmp1.Format("%-30.30s","");
            tmp2.Format("%-15.15s","Discount");
            tmp3.Format("%15s",CCrack::strVARIANT(m_rs.GetFieldValue("discount")));
            fprintf(fp,tmp1+tmp2+tmp3+"\n");      
            linecount++;
      }

      if(linecount==13)
      {
            linecount=0;
            EjectPage(fp);
      }
      tmp1.Format("%-30.30s","");
      tmp2.Format("%-15.15s","V.A.T.");
      tmp3.Format("%15s",CCrack::strVARIANT(m_rs.GetFieldValue("vat")));
      fprintf(fp,tmp1+tmp2+tmp3+"\n");      
      linecount++;

      if(linecount==13)
      {
            linecount=0;
            EjectPage(fp);
      }
      tmp1.Format("%-30.30s","");
      tmp2.Format("%-15.15s","Grand Total");
      tmp3.Format("%15s",CCrack::strVARIANT(m_rs.GetFieldValue("grand_total")));
      fprintf(fp,tmp1+tmp2+tmp3+"\n");      
      linecount++;

      if(atof(CCrack::strVARIANT(m_rs.GetFieldValue("rnd_amt")))!=0)
      {
            if(linecount==13)
            {
                  linecount=0;
                  EjectPage(fp);
            }
            tmp1.Format("%-30.30s","");
            tmp2.Format("%-15.15s","Rnd Off Amount");
            tmp3.Format("%15s",CCrack::strVARIANT(m_rs.GetFieldValue("rnd_amt")));
            fprintf(fp,tmp1+tmp2+tmp3+"\n");      
            linecount++;
      }

      if(linecount==13)
      {
            linecount=0;
            EjectPage(fp);
      }
      CStringArray ars;
      GetKot(&ars);
      tmp4="";
      if(ars.GetSize()>1)
      {
            for(int y=0;y<ars.GetSize();y++)
            {
                  if(y==0)
                        tmp4="KOT No : ";
                  tmp4+=ars.GetAt(y)+",";
            }
            tmp4.TrimRight(",");
      }
      tmp1.Format("%-30.30s",tmp4);
      tmp2.Format("%-15.15s","Net Amount");
      tmp3.Format("%15s",CCrack::strVARIANT(m_rs.GetFieldValue("net_amount")));
      fprintf(fp,tmp1+tmp2+tmp3+"\n");      
      linecount++;

      while(linecount<=13)
      {
            fprintf(fp,"\n");
            linecount++;
      }
      fprintf(fp,"\n\n\n");
      fprintf(fp,"\t"+CCrack::strVARIANT(m_rs.GetFieldValue("cashier")));
      fprintf(fp,"\n\n\n\n\n");

    fclose(fp);

      if(was_add)
            SetBlank();
}

void CBillView::EjectPage(FILE *fp,bool isFirst)
{

      if(!isFirst)
      {
            fprintf(fp,"\n\n\n\n\n");
            fprintf(fp,"\t"+CCrack::strVARIANT(m_rs.GetFieldValue("cashier")));
            fprintf(fp,"\n\n\n\n");
      }
      CStringArray ars;
      GetKot(&ars);
      CString tmp1;
      if(ars.GetSize()>0)
            tmp1=ars.GetAt(0);

      int iHead=8;
      CTenuityApp *app = (CTenuityApp *)AfxGetApp();      
      CTime ct=CCrack::ToDate(app->m_date);
      CTime cts=CTime::GetCurrentTime();
      CString tmp;
      GetDlgItemText(IDC_EDIT2,tmp);
//      CString tmp=app->m_Session.m_strSessionDate;
      tmp.Replace("/20","/");

//      fprintf(fp,"\n");
      fprintf(fp,"\t"+CCrack::strVARIANT(m_rs.GetFieldValue("rest_name")));
      fprintf(fp,"\n\n\n");
      fprintf(fp,"\t"+CCrack::strVARIANT(m_rs.GetFieldValue("session")));
      fprintf(fp,"\n\n");
      fprintf(fp,"\t\t\t\t\t\t"+CCrack::strVARIANT(m_rs.GetFieldValue("bill_no")));
      fprintf(fp,"\n\n\n");
      
      fprintf(fp,tmp+"    "
            +cts.Format("%H:%M")+"\t"
            +CCrack::strVARIANT(m_rs.GetFieldValue("st_code"))+"\t"
            +CCrack::strVARIANT(m_rs.GetFieldValue("table_no"))+"\t     "
            +CCrack::strVARIANT(m_rs.GetFieldValue("pax"))+"\t\t"
            +tmp1+"\t"
            +"\n"
            );
            fprintf(fp,"\n");
            fprintf(fp,"\n");
}





Example : Printing using grid ctrl
  I am using "MFC Grid control - Chris Maunder" http://www.codeproject.com/miscctrl/gridctrl.asp

void CGridReportView::ShowReport()
{
      COLORREF bg1,fg1,bg2,fg2;
      bg1=RGB(249,234,229); //light bg
      fg1=RGB(94,0,0);  //dark foreground

      bg2=RGB(229,249,239); //light bg
      fg2=RGB(0,94,0);  //dark foreground

      CString tmp;
      CTime ct;
      
      dlg.SetShow(TRUE,FALSE); //GetDate Range
      if(dlg.DoModal()==IDOK)
      {
            ct=dlg.GetFromTime();
      }
      else
            return;



      CString tmp1,tmp2;
      c2.GetWindowText(tmp2);
      c3.GetWindowText(tmp1);//session

      tmp=strReport.GetAt(c1.GetCurSel())+" as on "+ct.Format("%d-%b-%Y");
      tmp+="\nOutlet : "+tmp2+" - "+tmp1;
      m_Grid.SetReportPageLeft(tmp);
      GetDlgItem(IDC_TITLE)->SetWindowText(tmp);

      CString ses;
      if(tmp1!="All Sessions")
      {
            ses.Format(" and bill.session='%s'",tmp1);
      }
      CString sql;
      sql.Format("SELECT bill_date,session,last_update as user,bill_no,pax as covers,net_amount AS bill_amt,"
      "credit_to_room_amt AS room_amt, paid_credit_card "
      "AS cr_amt, paid_credit AS comp_amt, "
      "paid_cash AS cash_amt,is_void,void_reason "
      "FROM bill where bill_date=#%s# and "
      "rest_name='%s'%s order by session,bill_no",ct.Format("%m/%d/%Y"),tmp2,ses);
      m_Grid.use_decimal=false;

      try
      {
            if(m_rs1.IsOpen())
                  m_rs1.Close();
            m_rs1.Open(dbOpenDynaset,sql);
      }
      catch(CDaoException* e)
      {
            AfxMessageBox(e->
                  m_pErrorInfo->m_strDescription);
            e->Delete();
            return;
      }      
      if(m_rs1.IsBOF() && m_rs1.IsEOF())
      {
            MessageBox("No information to display");
            return;
      }

      m_Grid.SetRedraw(FALSE);
      m_Grid.DeleteAllItems();
      m_Grid.SetFixedRowCount(1);
      m_Grid.SetFixedColumnCount(1);
      m_Grid.SetRowCount(1);
      m_Grid.SetColumnCount(10);

      m_Grid.SetItemTextLastRow(0,"Bill No");
      m_Grid.SetItemTextLastRow(1,"Bill Amount");
      m_Grid.SetItemTextLastRow(2,"Cr. Card");
      m_Grid.SetItemTextLastRow(3,"Company");
      m_Grid.SetItemTextLastRow(4,"Room");
      m_Grid.SetItemTextLastRow(5,"Cash");
      m_Grid.SetItemTextLastRow(6,"User ID");
      m_Grid.SetItemTextLastRow(7,"Session");
      m_Grid.SetItemTextLastRow(8,"Void Amt");
      m_Grid.SetItemTextLastRow(9,"Void Reason");

      double tot[6],gtot[6];
      for(int x=0;x<6;x++)
            tot[x]=0;
      long paxtot=0;
      CString tmp1s,tmp2s;
      for(int x=0;x<6;x++)
            gtot[x]=0;
      while(!m_rs1.IsEOF())
      {
            tmp1s=CCrack::strVARIANT(m_rs1.GetFieldValue("session"));
            if(tmp1s!=tmp2s)
            {
                  if(gtot[0]>0)
                  {
                        m_Grid.InsertRow("TOTAL");
                        m_Grid.SetItemTextLastRow(1,gtot[0],true);
                        m_Grid.SetItemTextLastRow(2,gtot[1],true);
                        m_Grid.SetItemTextLastRow(3,gtot[2],true);
                        m_Grid.SetItemTextLastRow(4,gtot[3],true);
                        m_Grid.SetItemTextLastRow(5,gtot[4],true);
                        m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,1,bg1);
                        m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,2,bg1);
                        m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,3,bg1);
                        m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,4,bg1);
                        m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,5,bg1);
                  }
                  tmp2s=tmp1s;
                  gtot[0]=0; gtot[1]=0;gtot[2]=0; gtot[3]=0;gtot[4]=0;
            }
            m_Grid.InsertRow(CCrack::strVARIANT(m_rs1.GetFieldValue("bill_no")));
            m_Grid.SetItemTextLastRow(2,CCrack::strVARIANT(m_rs1.GetFieldValue("cr_amt")),true);
            m_Grid.SetItemTextLastRow(3,CCrack::strVARIANT(m_rs1.GetFieldValue("comp_amt")),true);
            m_Grid.SetItemTextLastRow(4,CCrack::strVARIANT(m_rs1.GetFieldValue("room_amt")),true);
            m_Grid.SetItemTextLastRow(5,CCrack::strVARIANT(m_rs1.GetFieldValue("cash_amt")),true);
            m_Grid.SetItemTextLastRow(6,CCrack::strVARIANT(m_rs1.GetFieldValue("user")));
            m_Grid.SetItemTextLastRow(7,CCrack::strVARIANT(m_rs1.GetFieldValue("session")));

            if(CCrack::strVARIANT(m_rs1.GetFieldValue("is_void"))=="TRUE")
            {
                  m_Grid.SetItemTextLastRow(8,CCrack::strVARIANT(m_rs1.GetFieldValue("bill_amt")),true);
                  m_Grid.SetItemTextLastRow(9,CCrack::strVARIANT(m_rs1.GetFieldValue("void_reason")));
                  m_rs1.MoveNext();
                  continue;
            }
            else
                  m_Grid.SetItemTextLastRow(1,CCrack::strVARIANT(m_rs1.GetFieldValue("bill_amt")),true);

            tot[0]+=atof(m_Grid.GetItemTextLastRow(1));
            tot[1]+=atof(m_Grid.GetItemTextLastRow(2));
            tot[2]+=atof(m_Grid.GetItemTextLastRow(3));
            tot[3]+=atof(m_Grid.GetItemTextLastRow(4));
            tot[4]+=atof(m_Grid.GetItemTextLastRow(5));

            gtot[0]+=atof(m_Grid.GetItemTextLastRow(1));
            gtot[1]+=atof(m_Grid.GetItemTextLastRow(2));
            gtot[2]+=atof(m_Grid.GetItemTextLastRow(3));
            gtot[3]+=atof(m_Grid.GetItemTextLastRow(4));
            gtot[4]+=atof(m_Grid.GetItemTextLastRow(5));

            m_rs1.MoveNext();
      }
      if(gtot[0]>0)
      {
            m_Grid.InsertRow("TOTAL");
            m_Grid.SetItemTextLastRow(1,gtot[0],true);
            m_Grid.SetItemTextLastRow(2,gtot[1],true);
            m_Grid.SetItemTextLastRow(3,gtot[2],true);
            m_Grid.SetItemTextLastRow(4,gtot[3],true);
            m_Grid.SetItemTextLastRow(5,gtot[4],true);
            m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,1,bg1);
            m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,2,bg1);
            m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,3,bg1);
            m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,4,bg1);
            m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,5,bg1);
      }
      m_Grid.InsertRow("TOTAL==>");

      tmp.Format("%.0f",tot[0]);
      m_Grid.SetItemTextLastRow(1,tmp,true);
      tmp.Format("%.0f",tot[1]);
      m_Grid.SetItemTextLastRow(2,tmp,true);
      tmp.Format("%.0f",tot[2]);
      m_Grid.SetItemTextLastRow(3,tmp,true);
      tmp.Format("%.0f",tot[3]);
      m_Grid.SetItemTextLastRow(4,tmp,true);
      tmp.Format("%.0f",tot[4]);
      m_Grid.SetItemTextLastRow(5,tmp,true);

      for(x=0;x<10;x++)
      {
            m_Grid.SetItemBkColour(m_Grid.GetRowCount()-1,x,bg1);
            m_Grid.SetItemFgColour(m_Grid.GetRowCount()-1,x,fg1);
      }
      m_rs1.Close();
      FormatGrid();
      m_Grid.SetRedraw(TRUE);
      m_Grid.Invalidate();      
      m_Grid.AutoSize();
//      m_Grid.SetColumnWidth(1,50);
}


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nasaAuthor Commented:
Wow,

That is a seriously awesome post -- It helps me solve problems I wasn't trying to solve.  But the problem I am trying to solve may or maynot be able to be solved with one of the above free solutions (I am still looking at the code to determine if they can or can't) -- maybe you would know :}   -- the report I am trying to create would look like what I havep posted below:


 ----------          -----------         ------------
 |  name |        |  name  |        | name |
 -----------        ------------         ---------

prg 1                 prg 3              prg 4
prg 2                 prg 4              prg 5


 ----------          -----------         ------------
 |  name |        |  name  |        | name |
 -----------        ------------         ---------

prg 1                 prg 3              prg 4
prg 2                 prg 4              prg 5


Any ideas how to do this?


Nasa
0
DanRollinsCommented:
A solution that  ViralPurohit did no mention -- but is much easier to implement --  is to generate an HTML file and load/print it with a webbrowser .  For instance, the example you showed would look like this:

<html><head>
<title>The Table For my Boss, "Mr. Professor" </title>
</head>
<body>
<table border=2 bodercolor=white>
<tr>
    <td bordercolor=red>Name</td>    
    <td bordercolor=red>Name</td>    
    <td bordercolor=red>Name</td>
</tr>
<tr>
    <td bordercolor=white>prg1<br>prg2</td>    
    <td bordercolor=white>prg3<br>prg4</td>    
    <td bordercolor=white>prg4<br>prg5</td>    
</tr>
<tr>
    <td bordercolor=red>Name</td>    
    <td bordercolor=red>Name</td>    
    <td bordercolor=red>Name</td>
</tr>
<tr>
    <td bordercolor=white>prg1<br>prg2</td>    
    <td bordercolor=white>prg3<br>prg4</td>    
    <td bordercolor=white>prg4<br><b>prg5</b></td>    
</tr>
</table>

</body>
</html>
And to make certain item bold, just surround them with <b>...</b> as in the last  prg5 example.

Just select that text, copy it to the clipboard, use Notepad to create a new file named test.htm and paste it in there.  Then double-click that file in the Explorer.

=-=-=-=-=-=-=-=-=--=
Now it's not clear to me if you are having trouble generating the table in general, or if the trouble is in using database functions to learn what data to put in the table and where to put it ... when :)

There is no way anyone here can tell you how to do that becasue we do not know your database schema.

- Dan
0
nasaAuthor Commented:
Ok,

I have selected easyreport to make my reports with.  So far I have been able to use it to create a view, but when I try to print/print preview with it I just get blank pages.  

Basically, each report is created within it's own class, using easyreport.  The actual report is kept in a variable with the Doc class.  What would I need to do with this type of architecture to get this to print?


Nasa
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.