コード例 #1
0
        private void Show_Form()
        {
            // Shows the "Customer Labels Dialog" form in Northwind.mdb
            // and manipulates controls on the form.

            Access.Application oAccess = null;
            Access.Form        oForm   = null;
            Access.Controls    oCtls   = null;
            Access.Control     oCtl    = null;
            string             sDBPath = null; //path to Northwind.mdb
            string             sForm   = null; //name of form to show

            // Enable exception handler:
            try
            {
                sForm = "Customer Labels Dialog";

                // Start a new instance of Access for Automation:
                oAccess = new Access.ApplicationClass();

                // Make sure Access is visible:
                if (!oAccess.Visible)
                {
                    oAccess.Visible = true;
                }

                // Determine the path to Northwind.mdb:
                sDBPath = oAccess.SysCmd(Access.AcSysCmdAction.acSysCmdAccessDir,
                                         moMissing, moMissing).ToString();
                sDBPath = sDBPath + @"Samples\Northwind.mdb";

                // Open Northwind.mdb in shared mode:
                oAccess.OpenCurrentDatabase(sDBPath, false, "");
                // If using Access 10.0 object library, use this instead:
                //oAccess.OpenCurrentDatabase(sDBPath, false, null);

                // Close any forms that Northwind may have opened:
                while (oAccess.Forms.Count > 0)
                {
                    oForm = oAccess.Forms[0];
                    oAccess.DoCmd.Close(Access.AcObjectType.acForm,
                                        oForm.Name, Access.AcCloseSave.acSaveNo);
                    NAR(oForm);
                    oForm = null;
                }

                // Select the form name in the database window and give focus
                // to the database window:
                oAccess.DoCmd.SelectObject(Access.AcObjectType.acForm, sForm, true);

                // Show the form:
                oAccess.DoCmd.OpenForm(sForm, Access.AcFormView.acNormal, moMissing,
                                       moMissing, Access.AcFormOpenDataMode.acFormPropertySettings,
                                       Access.AcWindowMode.acWindowNormal, moMissing);

                // Use Controls collection to edit the form:

                oForm = oAccess.Forms[sForm];
                oCtls = oForm.Controls;

                // Set PrintLabelsFor option group to Specific Country:

                oCtl = (Access.Control)oCtls["PrintLabelsFor"];
                object[] Parameters = new Object[1];
                Parameters[0] = 2; //second option in option group
                oCtl.GetType().InvokeMember("Value", BindingFlags.SetProperty,
                                            null, oCtl, Parameters);
                NAR(oCtl);
                oCtl = null;

                // Put USA in the SelectCountry combo box:
                oCtl          = (Access.Control)oCtls["SelectCountry"];
                Parameters[0] = true;
                oCtl.GetType().InvokeMember("Enabled", BindingFlags.SetProperty,
                                            null, oCtl, Parameters);
                oCtl.GetType().InvokeMember("SetFocus", BindingFlags.InvokeMethod,
                                            null, oCtl, null);
                Parameters[0] = "USA";
                oCtl.GetType().InvokeMember("Value", BindingFlags.SetProperty,
                                            null, oCtl, Parameters);
                NAR(oCtl);
                oCtl = null;

                // Hide the Database Window:
                oAccess.DoCmd.SelectObject(Access.AcObjectType.acForm, sForm, true);
                oAccess.RunCommand(Access.AcCommand.acCmdWindowHide);

                // Set focus back to the form:
                oForm.SetFocus();

                // Release Controls and Form objects:
                NAR(oCtls);
                oCtls = null;
                NAR(oForm);
                oForm = null;

                // Release Application object and allow Access to be closed by user:
                if (!oAccess.UserControl)
                {
                    oAccess.UserControl = true;
                }
                NAR(oAccess);
                oAccess = null;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                // Release any Access objects and quit Access due to error:
                NAR(oCtl);
                oCtl = null;
                NAR(oCtls);
                oCtls = null;
                NAR(oForm);
                oForm = null;
                try // use try..catch in case oAccess is not set
                {
                    oAccess.Quit(Access.AcQuitOption.acQuitSaveNone);
                }
                catch { }
                NAR(oAccess);
                oAccess = null;
            }
        }
コード例 #2
0
        private void Preview_Report()
        {
            // Previews the "Summary of Sales by Year" report in Northwind.mdb.

            Access.Application oAccess = null;
            Access.Form        oForm   = null;
            string             sDBPath = null; //path to Northwind.mdb
            string             sReport = null; //name of report to preview

            // Enable exception handler:
            try
            {
                sReport = "Summary of Sales by Year";

                // Start a new instance of Access for Automation:
                oAccess = new Access.ApplicationClass();

                // Make sure Access is visible:
                if (!oAccess.Visible)
                {
                    oAccess.Visible = true;
                }

                // Determine the path to Northwind.mdb:
                sDBPath = oAccess.SysCmd(Access.AcSysCmdAction.acSysCmdAccessDir,
                                         moMissing, moMissing).ToString();
                sDBPath = sDBPath + @"Samples\Northwind.mdb";

                // Open Northwind.mdb in shared mode:
                oAccess.OpenCurrentDatabase(sDBPath, false, "");
                // If using Access 10.0 object library, use this instead:
                //oAccess.OpenCurrentDatabase(sDBPath, false, null);

                // Close any forms that Northwind may have opened:
                while (oAccess.Forms.Count > 0)
                {
                    oForm = oAccess.Forms[0];
                    oAccess.DoCmd.Close(Access.AcObjectType.acForm,
                                        oForm.Name, Access.AcCloseSave.acSaveNo);
                    NAR(oForm);
                    oForm = null;
                }

                // Select the report name in the database window and give focus
                // to the database window:
                oAccess.DoCmd.SelectObject(Access.AcObjectType.acReport, sReport, true);

                // Maximize the Access window:
                oAccess.RunCommand(Access.AcCommand.acCmdAppMaximize);

                // Preview the report:
                oAccess.DoCmd.OpenReport(sReport,
                                         Access.AcView.acViewPreview, moMissing, moMissing,
                                         Access.AcWindowMode.acWindowNormal, moMissing);
                // If using Access 10.0 object library, use this instead:
                //oAccess.DoCmd.OpenReport(sReport,
                //  Access.AcView.acViewPreview, moMissing, moMissing,
                //  Access.AcWindowMode.acWindowNormal, moMissing);

                // Maximize the report window:
                oAccess.DoCmd.Maximize();

                // Hide Access menu bar:
                oAccess.CommandBars["Menu Bar"].Enabled = false;
                // Also hide NorthWindCustomMenuBar if it is available:
                try
                {
                    oAccess.CommandBars["NorthwindCustomMenuBar"].Enabled = false;
                }
                catch { }

                // Hide Report's Print Preview menu bar:
                oAccess.CommandBars["Print Preview"].Enabled = false;

                // Hide Report's right-click popup menu:
                oAccess.CommandBars["Print Preview Popup"].Enabled = false;

                // Release Application object and allow Access to be closed by user:
                if (!oAccess.UserControl)
                {
                    oAccess.UserControl = true;
                }
                NAR(oAccess);
                oAccess = null;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                // Release any Access objects and quit Access due to error:
                NAR(oForm);
                oForm = null;
                try // use try..catch in case oAccess is not set
                {
                    oAccess.Quit(Access.AcQuitOption.acQuitSaveNone);
                }
                catch { }
                NAR(oAccess);
                oAccess = null;
            }
        }