Add Label to Access MDB Report with C#

HaoRan

New member
Joined
Aug 1, 2008
Messages
1
I have a situation where its necessary to open a Backend MDB and alter a report.

I need to add controls (in this case labels) and set their properties, then close.

Ive gotten up to the point of trying to set properties and cant get that to work. Heres what I have so far.

Code:
          Access.Application oAccess = new Access.ApplicationClass();
          object oMissing = System.Reflection.Missing.Value;
          oAccess.DBEngine.OpenDatabase("C:\\data\\test.mdb", false, false, ";PWD=12345");
          oAccess.OpenCurrentDatabase("C:\\data\\test.mdb", true);
          oAccess.DoCmd.OpenReport("paycheck", Access.AcView.acViewDesign, oMissing, oMissing );
          Access.Report rpt=null;
          foreach(Access.Report o in oAccess.Reports)
          {
            if (o.Name == "paycheck")
            {
              rpt = o;
              break;
            }
          }
          bool bTest = DoesControlExistInReport(rpt, "CompanyAddress2");
          Int16 intCompanyAddress =  ControlTopPosition(rpt, "CompanyAddress2");
          Int16 intEmployeeAddress = ControlTopPosition(rpt, "Text131");

          //Int16 intSectionHeight = PayCheckTopPosition(rpt); 

          MessageBox.Show("Company Address Top is " + intCompanyAddress.ToString());
          MessageBox.Show("Employee Address Top is " + intEmployeeAddress.ToString());

          bool oTesting;
          Int16 SecHeight = 0;
          SecHeight = rpt.get_Section(0).Height;
          Single sglStartPos = (Single)(SecHeight - 554.976);
          oTesting = AddChkIdentifier(oAccess, 1, rpt, "CheckNumber", "Chk. #:", sglStartPos, 1);    


private bool AddChkIdentifier(Access.Application oAA, int intY , Access.Report oReport, string TextControlSrc, string  LabelCaption,  Single TopStartPosition,  int IdCount) 
{
    Access.Control lblChkID;
    Access.Control txtChkID;

    Int16 SecHeight = 0;
    SecHeight = oReport.get_Section(0).Height; 

    if (IdCount == 4)
    {
        TopStartPosition = (Single)(SecHeight - 554.976); 
    }
    if (IdCount <= 3)
    {
        //Activator.CreateInstanceFrom
        lblChkID = (Access.Control)(oAA.CreateReportControl(oReport.Name, Access.AcControlType.acLabel, Access.AcSection.acDetail, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 0.3646 * 1440, TopStartPosition, 0.5104 * 1440, 184.992));
        //txtChkID = (Access.Control)(oAA.CreateReportControl("Paycheck", Access.AcControlType.acTextBox, Access.AcSection.acDetail, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 08958 * 1440, TopStartPosition, 0.9583 * 1440, 184.992)); 
        //Activator.CreateInstanceFrom(oReport.Name, "Label"); 
    }
    else
    {
        lblChkID = (Access.Control)(oAA.CreateReportControl("Paycheck", Access.AcControlType.acTextBox, Access.AcSection.acDetail, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1.8854 * 1440, TopStartPosition, 0.5104 * 1440, 184.992));
        txtChkID = (Access.Control)(oAA.CreateReportControl("Paycheck", Access.AcControlType.acTextBox, Access.AcSection.acDetail, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 2.4167 * 1440, TopStartPosition, 0.9583 * 1440, 184.992));
    }

    string oABC = (string)(lblChkID.GetType().InvokeMember("Caption", System.Reflection.BindingFlags.SetProperty, null, lblChkID,  new object[] {"test"} ));


    int PropCount; 
    PropCount = lblChkID.Properties.Count;
    object customProps = lblChkID.Properties;
    Type lblProps = customProps.GetType();
    try
    {
        object customProp = lblProps.InvokeMember("Caption", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.GetProperty, null, customProps, null);
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message); 
    }


    System.Reflection.PropertyInfo[] pi = lblChkID.GetType().GetProperties();
    foreach (System.Reflection.PropertyInfo prop in pi)
    {
        MessageBox.Show(pi.GetType().Name.ToString());
    }

    iLab.Reflector  oiLab; 
}


I appreciate any help anyone could be here.

Thanks in advance,
 
Back
Top