how to do everything with microsoft office access 2003 phần 8 pps

56 278 0
how to do everything with microsoft office access 2003 phần 8 pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

15 CHAPTER 15: Automate with Macros 369 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Chapter 15 The first argument in the DateAdd function, “yyyy,” indicates that the interval you want to increment is the year part of the date value. The second argument is the number to add; the third names the control that contains the original date. Set Control Properties You can set many of the properties of forms, reports, and controls by running a macro. For example, you can hide a control from view on the form or disable it so the user can’t enter data in it. You can also change colors, fonts, and other appearance properties. As an example of setting a property with a macro, disable the Drivers License control if the subject of the Alpha Card report is younger than 16. To do this, set the Enabled property to No. When a control is disabled, it still appears on the screen but is dimmed; you can’t reach it by pressing TAB or by clicking it. To ensure that you enter the correct identifier, you can use the Expression Builder. After adding the SetValue action to the macro, click Build (…) next to the Item argument to open the Expression Builder, then do the following: 1. Double-click on the Forms folder, then double-click on the All Forms folder in the left panel to open the list of forms in the current database. 2. Choose the Alpha Card form. A list of all controls and labels in the form appears in the center panel. 3. Choose Drivers License. A list of all the properties that apply to the Drivers License text box control appears in the right panel. 4. Choose Enabled and click Paste. When you click OK, the expression is placed in the Item argument box. To complete the macro: 1. Enter No in the Expression argument. 2. Add a condition to the Action row that runs the macro only if the Age value is less than 16. P:\010Comp\HowTo8\938-1\ch15.vp Friday, August 08, 2003 10:58:05 AM Color profile: Generic CMYK printer profile Composite Default screen 3. Attach the macro to the Age control’s After Update event property. You probably will want to add another macro to reenable the Drivers License when you move to the next record. If you want to hide a control, set its Visible property to No. If the property value is a string expression, enclose it in quotation marks in the Expression argument box. Change the Flow of Operations Adding conditions that determine whether a macro action is carried out is one way to control the flow of operations. You can add the MsgBox function to a macro condition to let the user decide which action to carry out. The MsgBox function is similar to the MsgBox action with the exception that the function returns a value, depending on which button the user clicks in the message box. The MsgBox function displays a dialog box containing the message and waits for the user to click a button indicating the user’s choice. Several arrangements of buttons are available in the dialog box. The MsgBox function has three main arguments and two additional arguments; only the first one is required: ■ Prompt A string expression displayed in the dialog box. You can display up to 1,024 characters, depending on the font size. ■ Button A number equal to the sum of three values, which specify the visual characteristics of the message box such as the number and type of buttons, the default button, the icon style, and the modality of the message box. ■ Title A string expression that is displayed in the dialog box title bar. Two additional arguments can specify a Help file and context number in the file where you can find context sensitive help. You can display seven different buttons in various arrangements, plus a choice of four icons. You also can specify which of the buttons is the default. Each button arrangement and dialog box feature has a numeric value. These values are totaled and placed in the Button argument. The six arrangements of the seven buttons and their values are: ■ 0 displays only OK ■ 1 displays OK with Cancel 370 How to Do Everything with Microsoft Office Access 2003 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Chapter 15 P:\010Comp\HowTo8\938-1\ch15.vp Friday, August 08, 2003 10:58:06 AM Color profile: Generic CMYK printer profile Composite Default screen 15 CHAPTER 15: Automate with Macros 371 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Chapter 15 ■ 2 displays Abort, Retry, and Ignore ■ 3 displays Yes, No, and Cancel ■ 4 displays Yes and No ■ 5 displays Retry and Cancel Add 16 to the button sum to show the Critical Message icon or 32 for the Warning Query, 48 for the Warning Message, or 64 for the Information Message. Finally, you can add to the sum to specify which button is the default. The default button activates if you press ENTER. For example, to display the Yes, No, and Cancel buttons in that order, add 3 to the Button sum. If you want to display the Critical Message icon, add 16 to the sum. To set the No button as the default, add 256 to the sum. For these features, enter 275 as the Button argument in the MsgBox function. See the Help topic “MsgBox Function” for a complete list of all the button arrangements and dialog box features. When you use the MsgBox function in a macro condition, you can compare the returned value to a specific number and carry out the action if the comparison is True. For example, you can use the MsgBox function to display a confirmation message before deleting a record. The box contains three buttons: Yes, No, and Cancel. When the user clicks one of the buttons, the MsgBox function returns a value depending on which button was clicked: 1 for OK, 2 for Cancel, 3 for Abort, 4 for Retry, 5 for Ignore, 6 for Yes, and 7 for No. For example, if the user clicks the Yes button, the function returns 6; so if any other value is returned, the user did not click Yes. Figure 15-3 shows a macro using the MsgBox function in a condition that evaluates to True if the function returned any value except 6 (Yes). If the value is FIGURE 15-3 Using the MsgBox function in a macro condition P:\010Comp\HowTo8\938-1\ch15.vp Friday, August 08, 2003 10:58:06 AM Color profile: Generic CMYK printer profile Composite Default screen 372 How to Do Everything with Microsoft Office Access 2003 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Chapter 15 not 6, the deletion event is canceled. You could add other conditions that carry out actions as a result of the other button selections. The Button argument in the MsgBox function in Figure 15-3 is 291, which is the sum of the Yes, No, and Cancel button arrangement (3), the Warning Query icon (32), and setting the second button (No) as the default (256). After you save the Verify Deletion macro, attach it to the form’s Before Del Confirm event property. The message box displays when you select a record and press DEL. In Figure 15-3, the user selected the Alpha Entry record for Index 24 before pressing DEL. You can see that it has been deleted from the Form view but has not yet been confirmed. If you click No in the box, the record is returned. If you respond by clicking Yes, Access deletes the record. If the deletion will result in cascade deletions of other records or interfere in some other way with the relationships in the database, Access displays another confirmation message. Filter Records You can create a macro to limit the records you want to print by adding a Where Condition to the OpenReport action. For example, suppose that you want to preview the Alpha Entry records for all incidents with a Code in the danger range, 11000 to 19999. Start a new macro in the macro design window and do the following: 1. Choose OpenReport in the Action column. 2. In the Report Name argument, select Alpha Entries from the list of available reports. 3. Choose Print Preview as the View argument. 4. Enter [Alpha Entry]![Code] Between 11000 And 19999 in the Where Condition argument or click the Build button to get help from the Expression Builder. Don’t use an equal sign in the Where Condition argument. 5. Click Run. You can see in the Print Preview that only three of the incidents reported fall in the danger range. P:\010Comp\HowTo8\938-1\ch15.vp Friday, August 08, 2003 10:58:07 AM Color profile: Generic CMYK printer profile Composite Default screen 15 If you want to see a fancy example of filtering records by using macro conditions, open the Northwind sample database’s Customer Phone List macro. Create an AutoExec Macro You can create a special macro that runs when you first open a database. The AutoExec macro can carry out such actions as opening a form for data entry, displaying a message box prompting the user to enter his or her name, or playing a sound greeting. All you need to do is create the macro with the actions you want carried out at startup and save it with the name AutoExec. A database can have only one macro named AutoExec. When you open a database, all the startup options you have set in the Startup dialog box take place first. You can see these by choosing Tools | Startup. Access looks for a macro named AutoExec and executes the actions in it. You can bypass both the startup options and the AutoExec macro by pressing SHIFT when you open the database. Many of the same options can be set in the AutoExec macro as in the Startup dialog box. Be careful not to include conflicting settings in the macro. See Chapter 13 for information about the startup settings. Create a Macro Group If you have created several macros that apply to controls on the same form or report, you can group them together as one file. There are two advantages to using macro groups: ■ It reduces the number of macro names in the Database window. ■ You can find all the macros for a single form or report in one place where they are easy to edit. An example of using grouped macros is the Choose Report dialog box that asks you to select the report you are interested in and then decide to print or preview the report. See Chapter 17 for details of this form. CHAPTER 15: Automate with Macros 373 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Chapter 15 P:\010Comp\HowTo8\938-1\ch15.vp Friday, August 08, 2003 10:58:08 AM Color profile: Generic CMYK printer profile Composite Default screen 374 How to Do Everything with Microsoft Office Access 2003 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Chapter 15 To create a macro group: 1. Open the macro sheet as usual. 2. Click the Macro Names button or choose View | Macro Names. 3. Add a macro to the sheet and enter a name for it in the Macro Name column of the first row of the macro. 4. Add the rest of the actions to the macro. 5. To add another macro, enter the name in the Macro Name column and add the actions you want to occur. Figure 15-4 shows the completed Choose Report macro group. 6. Save and close the macro window. When Access runs a macro in a group, it begins with the action in the row that contains the macro name and continues until it finds no more actions or encounters another macro name. After adding all the macros to the group, close and save it as usual with the group name. You will find the macros in a group will be much easier to read if you leave at least one blank row between the macros. FIGURE 15-4 Grouping macros P:\010Comp\HowTo8\938-1\ch15.vp Friday, August 08, 2003 10:58:08 AM Color profile: Generic CMYK printer profile Composite Default screen 15 When you assign macros from a group to an event property, you must use the group name as well as the macro name. In the property sheet for a control, the drop-down list in an event property shows compound names for all the macros in a group and the names of all the single macros. The group name and the macro name both appear separated by a period: macrogroupname.macroname. Assign AutoKeys Access offers a special macro group named AutoKeys in which you can assign an action or set of actions to a specific key or key combination. Pressing the key or combination of keys carries out the action you specify. You can add as many individual macros to the group as you need, each one named with the key or key combination that will run it. For example, the following macro opens the Alpha Card form when the user presses CTRL-F. The SendKey syntax form is used as the macro name. The carat symbol (^) represents CTRL and the plus sign (+) represents SHIFT. Function keys and other key names are enclosed in curly brackets. See the Help topic “AutoKey Combinations” for a list of key combinations and their SendKey syntax for the macro name. If you assign a key combination that is already used by Access (for example, CTRL-C), the Access assignment will no longer work unless you change it manually or reset to default. Be warned, though, that resetting to the default will remove all custom assignments. CHAPTER 15: Automate with Macros 375 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Chapter 15 P:\010Comp\HowTo8\938-1\ch15.vp Friday, August 08, 2003 10:58:09 AM Color profile: Generic CMYK printer profile Composite Default screen HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Front Matter Blind Folio FM:ii P:\010Comp\Build8\769-9\fm.vp Tuesday, January 07, 2003 1:28:08 PM Color profile: Generic CMYK printer profile Composite Default screen This page intentionally left blank HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Chapter 16 blind folio 377 Chapter 16 Customize Menus and Toolbars P:\010Comp\HowTo8\938-1\ch16.vp Friday, August 08, 2003 11:21:16 AM Color profile: Generic CMYK printer profile Composite Default screen 378 How to Do Everything with Microsoft Office Access 2003 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 222938-1 / Chapter 16 How to… ■ Customize Access command bars ■ Build custom command bars ■ Attach a customized command bar to an object ■ Modify and delete custom command bars ■ Restore built-in command bars In Access 97, Microsoft blended the three types of user interaction tools into a single global concept: the command bar. Although the terms toolbars, menu bars, and shortcut menus are still valid in Access 2003 and do describe differing implementations, the methods used to create and modify custom command bars are the same for all types. The purpose is to make them more consistent and easier to use and customize. You have worked extensively with all of these interaction tools in the previous chapters of this book and when running other Office programs. In this chapter, you will see how to work with and customize built-in command bars and create custom command bars for an application. Use Access Command Bars The basic element of toolbars, menu bars, and shortcut menus is the command the user chooses to cause an action such as printing a report or running a query. You reach a command by clicking a toolbar button or choosing from a hierarchy of menus and submenus. Toolbar button Command bar Menu Submenu Menu commands P:\010Comp\HowTo8\938-1\ch16.vp Friday, August 08, 2003 11:21:16 AM Color profile: Generic CMYK printer profile Composite Default screen [...]... you want to build a new menu that is not a copy of one that Access offers, you can use the New Menu tool and customize it to fit your needs To add a custom menu to a toolbar or menu bar, create it in place on the bar To add a new custom menu: 1 With the toolbar or menu bar showing, open the Customize dialog box and click the Commands tab 16 388 How to Do Everything with Microsoft Office Access 2003 FIGURE... and showing or hiding the bar, whichever was the toolbar or menu bar’s original state To restore a toolbar or menu bar: 1 Open the Customize dialog box and click the Toolbars tab 16 3 98 How to Do Everything with Microsoft Office Access 2003 2 Click Properties to open the Properties dialog box In the Selected Toolbar box, select the name of the toolbar or menu bar you want restored Notice that the Toolbar... a built-in toolbar, the command keeps all the pointers to Access Help topics Moving a button from one toolbar to another removes it from the source toolbar You must have both toolbars showing to move or copy a button The Customize dialog box can be open or closed To move a button from another toolbar: ■ If the Customize dialog box is open, drag the button to the new toolbar ■ If the Customize dialog... I-beam, drop the button on the toolbar 3 Continue to add other buttons to the toolbar You can drop a button between two buttons already in the toolbar The bar will expand as you add buttons To move a button already in place, drag it to the desired position If another toolbar has a button you can use, you can move or copy it to the new toolbar Often it is easier to use an existing button than to start from... described earlier 16 384 How to Do Everything with Microsoft Office Access 2003 2 Click the Toolbars tab and click New, then type a name for the new toolbar in the New Toolbar dialog box 3 Click OK A tiny, empty toolbar appears in front of the Customize dialog box and the toolbar name, Alpha Card, appears at the bottom of the list in the Toolbars box 4 On the Toolbars tab, click Properties The Toolbar Properties... clear the check mark next to the button name To restore the button to the toolbar, check it in the list again The last item in the list is Reset Toolbar, which restores the toolbar to its original default button set Move and Resize Command Bars A command bar is docked if it is fixed to one edge of the window Menu bars and toolbars normally appear docked at the top of the window You can drag them away... drop-down list If the toolbar is not the default for this view, 380 How to Do Everything with Microsoft Office Access 2003 the name will also be removed from the drop-down list Default toolbar names remain on that list even though they are not showing Three additional toolbars, which are not defaults for any view, appear in the Toolbars list in the Customize dialog box: ■ The Source Code Control toolbar... and Toolbar Options In addition to repositioning and resizing a built-in menu or toolbar, you can use the Options tab of the Customize dialog box to set other features The upper pane sets options for the menus and 16 382 How to Do Everything with Microsoft Office Access 2003 FIGURE 16-2 Toolbar Options list for the Table Datasheet toolbar toolbars as personalized by Access The lower pane contains options... the Toolbars list Instead, they are listed as menu items in the Custom category on the Shortcut Menus menu bar Show and Hide Toolbar Buttons You don’t have to display all the default toolbar buttons on a toolbar Using the Toolbar Options button, you can choose which buttons to display Click the Toolbar Options button (actually a gray area rather than a conventional button) on the right end of the toolbar... controls, and reports 16 392 How to Do Everything with Microsoft Office Access 2003 To specify a custom menu bar or custom shortcut menu as the default for the entire database or application, you must change the settings in the Startup dialog box To set global command bars to replace the defaults: 1 Choose Tools | Startup to open the Startup dialog box 2 Click the arrow next to the Menu Bar box and select . down CTRL-ALT while you drag the button. 386 How to Do Everything with Microsoft Office Access 2003 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 2229 38- 1. over the empty box. 388 How to Do Everything with Microsoft Office Access 2003 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 2229 38- 1 / Chapter 16 FIGURE. Microsoft Office Access 2003 HowTo-Tght (8) / How to Do Everything with Microsoft Office Access 2003 / Andersen / 2229 38- 1 / Chapter 15 P:10CompHowTo89 38- 1ch15.vp Friday, August 08, 2003 10: 58: 06

Ngày đăng: 14/08/2014, 08:22

Từ khóa liên quan

Mục lục

  • PART III Improve the Access 2003 Workplace

    • CHAPTER 15 Automate with Macros

      • Create Other Commonly Used Macros

        • Change the Flow of Operations

        • Filter Records

        • Create an AutoExec Macro

        • Create a Macro Group

          • Assign AutoKeys

          • CHAPTER 16 Customize Menus and Toolbars

            • Use Access Command Bars

              • Show and Hide Built-in Toolbars

              • Show and Hide Toolbar Buttons

              • Move and Resize Command Bars

              • Change Menu and Toolbar Options

              • Customize Command Bars

                • Create a Global Toolbar

                • Create Custom Toolbars and Menu Bars

                • Attach a Custom Command Bar to an Object

                • Specify Global Command Bars

                • Delete a Custom Command Bar

                • Modify Command Bars

                  • Move Controls

                  • Add and Delete Controls

                  • Edit Buttons and Menu Commands

                  • Restore Built-in Command Bars

                  • CHAPTER 17 Create Custom Switchboards and Dialog Boxes

                    • Create Switchboards

                      • Use the Switchboard Manager to Create Switchboards

                      • Modify the Switchboard

                      • Create a Custom Dialog Box

                        • Design the Form

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan