Ms Access 简明教程
MS Access - Macros
在本章中,我们将介绍 Access 中宏的基础知识。宏是一种工具,允许您自动执行任务并向表单、报表和控件添加功能。
In this chapter, we will cover the basics of Macros in Access. A Macro is a tool that allows you to automate tasks and add functionality to your forms, reports, and controls.
-
Macros in Access work a bit different from Macros in Word or Excel, where you essentially record a series of keystrokes and play them back later.
-
Access Macros are built from a set of predefined actions, allowing you to automate common tasks, and add functionality to controls or objects.
-
Macros can be standalone objects viewable from the Navigation pane, or embedded directly into a Form or Report. Once you have created database objects like tables, forms and reports, Macros can provide a quick and easy way to tie all those objects together to create a simple database application that anyone can use or even modify, with relatively little training.
-
Macros provide a way to run commands without the need to write or even know VBA code, and there is a lot that you could achieve just with Macros.
Creating a Macro
让我们开始创建当单击命令按钮时打开表单的非常简单的宏。为此,我们需要打开数据库和 frmEmployeeData 表单,其中我们创建了两个选项卡。
Let us start be creating a very simple Macro that opens a form when a command button is clicked. For this, we need to open your database and frmEmployeeData form in which we have created two tabs.
在该表单中,我们可以添加一个按钮,允许用户打开所有职位信息。
In this form, we can add a button allowing users to open up all of the job information.
现在让我们转到该表单的设计视图,并从“控件”菜单中添加按钮表单。释放鼠标后,您将看到“命令按钮向导”对话框。
Let us now go to the Design View of this form and add button form the Controls menu. When you release your mouse, you will see the Command Button Wizard dialog box.
创建宏操作的方法有很多,但最简单的方法是仅使用“命令按钮向导”。
There is a couple of ways to build that Macro action, but the simplest way is to simply use the Command Button Wizard.
对于常见操作(如打开表单),请从“类别”列表中选择“窗体操作”,然后从“操作”列表中选择“打开窗体”,然后像上面屏幕截图中所示单击“下一步”。
For common actions like opening a form, select Form Operations from the Categories list and then select Open Form from the Actions list and click Next as in the above screenshot.
您需要指定要使用命令按钮打开哪个表单。现在,让我们选择 frmJobs 并单击 Next 。
You need to specify which form you would like to open with the command button. For now, let us select frmJobs and click Next.
在这个屏幕中我们有两个选项,我们可以 open the form and display a very specific record ,或者我们可以 open the form and show all the records 。让我们选择第二个选项,然后像上面屏幕截图中所示单击下一步。
In this screen we have two options, we can open the form and display a very specific record, or we can open the form and show all the records. Let us select the second option and click Next as in the above screenshot.
命令按钮本身可以显示图片,或者可以选择“显示文本”。在此处,我们希望显示文本“查看职位”,现在单击“下一步”。
We could have the command button itself display a picture or you can select the Display Text. Here, we want the text View Jobs to display and now click Next.
现在,您可以像上面屏幕截图中所示向命令按钮提供一个有意义的名称。这可以在其他代码或其他宏中使用。我们来称此为 cmdViewJobs 并单击 Finish 。
You can now provide a meaningful name to your command button as in the above screenshot. This can be used in other codes or other Macros. Let us call this cmdViewJobs and click Finish.
现在转到“窗体视图”。
Now go to the Form View.
现在您将在表单中看到一个“查看职位”按钮。让我们单击它。
You will now see a View Jobs button on your form. Let us click on it.
现在您打开了一个表单,但您不会查看任何信息。让我们返回到 frmEmployeeData 表单设计视图。确保命令按钮已选择,然后单击“属性表”中的“事件”选项卡。
Now you have a form open, but you will not be viewing any information. Let us go back to the frmEmployeeData form Design view. Make sure that the command button is selected and click on the Event tab on the Property Sheet.
点击后,您将看到向导创建的嵌入式宏。如果您现在想要修改此宏,请单击…按钮来打开向导生成的宏。
Upon clicking, you will see an embedded Macro created by the Wizard. If you now want to modify this Macro, click on the … button to open up the Macro generated by the Wizard.
这是宏设计器,在右侧您将看到操作目录。您的所有操作都将在此存储在文件夹中。您有数据输入选项、数据导入/导出等等,在主区域左侧您有另一个宏。它只包含一个操作,通过单击该操作,您可以查看该特定操作的其他属性。
This is the Macro Designer and on the right you will see the Action Catalog. This is where all of your actions will live in folders. You have the Data Entry options, Data Import/Export and so on, and on the left in the main area you have another Macro. It only contains one action, and clicking on that one action you can view other properties for that specific action.
您将看到表单名称,您可以点击该下拉箭头以查看数据库中可用的表单。您可以更改该表单的查看方式,可以选择以表单视图、设计视图、打印预览打开它。您可以应用筛选器名称或 Where 条件。在这里,我们要更改数据模式,因为 frmJobs 被设置为仅允许添加新记录的“添加模式”。我们可以通过将其更改为编辑模式在此宏中覆盖它。
You will see the form name and you can hit that drop-down arrow to view the forms available in your database. You can change how that form is viewed, you can have it open to Form view, Design view, Print Preview at your choice. You can apply a filter name or a Where condition. Here we want to change the Data Mode because frmJobs is set to the Add Mode which only allows the addition of new records. We can override this here in this Macro by changing it to the Edit Mode.
现在保存您的宏,然后关闭宏设计器并返回到表单视图。
Now save your Macro, and then close the Macro Designer and go back to the Form View.
让我们再次单击“查看工作”。
Let us click on View Jobs again.
您现在可以看到它打开您的 frmJobs 表单,并允许您滚动浏览我们数据库中所有可用的工作。
You can now see that it opens your frmJobs form and allows you to scroll through all of the available jobs in our database.