问题
Could you advise how to use the Microsoft.Office.Interop.Access
in C# to get all controls of just the header section of a specific Access form?
Thank you.
回答1:
All forms will have a Detail
Section and forms will have additional Sections like FormHeader
, FormFooter
, PageHeaderSection
, PageFooterSection
, etc. if those elements exist in the form. Each of those Sections will have a Controls collection. Here is an example:
static void Main(string[] args)
{
// this code requires the following COM reference in the project:
// Microsoft Access 14.0 Object Library
//
var objAccess = new Microsoft.Office.Interop.Access.Application();
objAccess.OpenCurrentDatabase(@"C:\Users\Public\Database1.accdb");
string formName = "ClientForm";
objAccess.DoCmd.OpenForm(formName, Microsoft.Office.Interop.Access.AcFormView.acDesign);
Microsoft.Office.Interop.Access.Form frm = objAccess.Forms[formName];
Console.WriteLine(String.Format("The FormHeader section of form [{0}] contains the following controls:", formName));
foreach (Microsoft.Office.Interop.Access.Control ctl in frm.Section["FormHeader"].Controls)
{
Console.WriteLine();
Console.WriteLine(String.Format(" [{0}]", ctl.Name));
Console.WriteLine(String.Format(" {0}", ctl.GetType()));
}
objAccess.DoCmd.Close(Microsoft.Office.Interop.Access.AcObjectType.acForm, formName);
objAccess.CloseCurrentDatabase();
objAccess.Quit();
Console.WriteLine();
Console.WriteLine("Done.");
Console.ReadKey();
}
Console output:
The FormHeader section of form [ClientForm] contains the following controls:
[Auto_Logo0]
Microsoft.Office.Interop.Access.ImageClass
[Auto_Header0]
Microsoft.Office.Interop.Access.LabelClass
Done.
来源:https://stackoverflow.com/questions/22020398/using-interop-access-to-get-controls-in-just-header-section-or-body-or-footer