Ms Access 简明教程
Ms Access - Overview
Microsoft Access 是 Microsoft 的数据库管理系统 (DBMS),将关系 Microsoft Jet Database Engine 与图形用户界面和软件开发工具结合到了一起。它是 Microsoft Office 套件应用程序的一部分,包含在专业版和更高版本中。
Microsoft Access is a Database Management System (DBMS) from Microsoft that combines the relational Microsoft Jet Database Engine with a graphical user interface and softwaredevelopment tools. It is a member of the Microsoft Office suite of applications, included in the professional and higher editions.
-
Microsoft Access is just one part of Microsoft’s overall data management product strategy.
-
It stores data in its own format based on the Access Jet Database Engine.
-
Like relational databases, Microsoft Access also allows you to link related information easily. For example, customer and order data. However, Access 2013 also complements other database products because it has several powerful connectivity features.
-
It can also import or link directly to data stored in other applications and databases.
-
As its name implies, Access can work directly with data from other sources, including many popular PC database programs, with many SQL (Structured Query Language) databases on the desktop, on servers, on minicomputers, or on mainframes, and with data stored on Internet or intranet web servers.
-
Access can also understand and use a wide variety of other data formats, including many other database file structures.
-
You can export data to and import data from word processing files, spreadsheets, or database files directly.
-
Access can work with most popular databases that support the Open Database Connectivity (ODBC) standard, including SQL Server, Oracle, and DB2.
-
Software developers can use Microsoft Access to develop application software.
Microsoft Access 存储称为数据库的信息。要使用 MS Access,您需要遵循以下四个步骤:
Microsoft Access stores information which is called a database. To use MS Access, you will need to follow these four steps −
-
Database Creation − Create your Microsoft Access database and specify what kind of data you will be storing.
-
Data Input − After your database is created, the data of every business day can be entered into the Access database.
-
Query − This is a fancy term to basically describe the process of retrieving information from the database.
-
Report (optional) − Information from the database is organized in a nice presentation that can be printed in an Access Report.
Architecture
-
Access calls anything that can have a name an object. Within an Access desktop database, the main objects are tables, queries, forms, reports, macros, data macros, and modules.
-
If you have worked with other database systems on desktop computers, you might have seen the term database used to refer to only those files in which you store data.
-
But, in Access, a desktop database (.accdb) also includes all the major objects related to the stored data, including objects you define to automate the use of your data.
Ms Access - RDBMS
Microsoft Access 与其他 Microsoft Office 产品的观感并无太大差异,布局和导航方面也是如此。然而,Access 是一个数据库,更具体地说,是一个关系数据库。
Microsoft Access has the look and feel of other Microsoft Office products as far as its layout and navigational aspects are concerned, but MS Access is a database and, more specifically, a relational database.
-
Before MS Access 2007, the file extension was .mdb, but in MS Access 2007 the extension has been changed to .accdb extension.
-
Early versions of Access cannot read accdb extensions but MS Access 2007 and later versions can read and change earlier versions of Access.
-
An Access desktop database (.accdb or .mdb) is a fully functional RDBMS.
-
It provides all the data definition, data manipulation, and data control features that you need to manage large volumes of data.
-
You can use an Access desktop database (.accdb or .mdb) either as a standalone RDBMS on a single workstation or in a shared client/server mode across a network.
-
A desktop database can also act as the data source for data displayed on webpages on your company intranet.
-
When you build an application with an Access desktop database, Access is the RDBMS.
Data Definition
现在让我们了解一下数据定义是什么 −
Let us now understand what Data Definition is −
-
In document or a spreadsheet, you generally have complete freedom to define the contents of the document or each cell in the spreadsheet.
-
In a document, you can include paragraphs of text, a table, a chart, or multiple columns of data displayed with multiple fonts.
-
In spreadsheet, you can have text data at the top to define a column header for printing or display, and you might have various numeric formats within the same column, depending on the function of the row.
-
An RDBMS allows you to define the kind of data you have and how the data should be stored.
-
You can also usually define rules that the RDBMS can use to ensure the integrity of your data.
-
For example, a validation rule might ensure that the user can’t accidentally store alphabetic characters in a field that should contain a number.
Data Manipulation
在 RDBMS 中操作数据与在文字处理或电子表格程序中操作数据有很大不同。
Working with data in RDBMS is very different from working with data in a word processing or spreadsheet program.
-
In a word processing document, you can include tabular data and perform a limited set of functions on the data in the document.
-
You can also search for text strings in the original document and, with ActiveX controls, include tables, charts, or pictures from other applications.
-
In a spreadsheet, some cells contain functions that determine the result you want, and in other cells, you enter the data that provides the source information for the functions.
-
You can search a single table for information or request a complex search across several related tables.
-
You can update a single field or many records with a single command.
-
You can write programs that use RDBMS commands to fetch data that you want to display and allow the user to update the data.
Access 使用强大的 SQL 数据库语言以处理表格中的数据。使用 SQL,你可以定义解决特定问题所需的信息集,包括来自许多表格的数据。
Access uses the powerful SQL database language to process data in your tables. Using SQL, you can define the set of information that you need to solve a particular problem, including data from perhaps many tables.
Data Control
电子表格和文字处理文档非常适合解决单用户问题,但当多个人需要共享数据时,它们很难使用。
Spreadsheets and word processing documents are great for solving single-user problems, but they are difficult to use when more than one person needs to share the data.
-
When you need to share your information with others, RDBMS gives you the flexibility to allow multiple users to read or update your data.
-
An RDBMS that is designed to allow data sharing also provides features to ensure that no two people can change the same data at the same time.
-
The best systems also allow you to group changes (which is also known as transaction) so that either all the changes or none of the changes appear in your data.
-
You might also want to be sure that no one else can view any part of the order until you have entered all of it.
-
Because you can share your Access data with other users, you might need to set some restrictions on what various users are allowed to see or update.
Ms Access - Objects
MS Access 使用“对象”来帮助用户列出和组织信息,以及准备专门设计的报表。当你创建一个数据库时,Access 为你提供了表、查询、表单、报表、宏和模块。Access 中的数据库由许多对象组成,但以下是最主要的几个对象 -
MS Access uses “objects" to help the user list and organize information, as well as prepare specially designed reports. When you create a database, Access offers you Tables, Queries, Forms, Reports, Macros, and Modules. Databases in Access are composed of many objects but the following are the major objects −
-
Tables
-
Queries
-
Forms
-
Reports
这些对象共同允许你输入、存储、分析和汇编你的数据。以下是 Access 数据库中主要对象的摘要;
Together, these objects allow you to enter, store, analyze, and compile your data. Here is a summary of the major objects in an Access database;
Table
表是用于定义和存储数据的对象。当你创建一个新表时,Access 会要求你定义字段,也称为列标题。
Table is an object that is used to define and store data. When you create a new table, Access asks you to define fields which is also known as column headings.
-
Each field must have a unique name, and data type.
-
Tables contain fields or columns that store different kinds of data, such as a name or an address, and records or rows that collect all the information about a particular instance of the subject, such as all the information about a customer or employee etc.
-
You can define a primary key, one or more fields that have a unique value for each record, and one or more indexes on each table to help retrieve your data more quickly.
Query
提供来自一个或多个表的数据的自定义视图的对象。查询是一种从小表或大表搜索和编译数据的方式。
An object that provides a custom view of data from one or more tables. Queries are a way of searching for and compiling data from one or more tables.
-
Running a query is like asking a detailed question of your database.
-
When you build a query in Access, you are defining specific search conditions to find exactly the data you want.
-
In Access, you can use the graphical query by example facility or you can write Structured Query Language (SQL) statements to create your queries.
-
You can define queries to Select, Update, Insert, or Delete data.
-
You can also define queries that create new tables from data in one or more existing tables.
Form
表单是桌面数据库中的一个对象,主要用于数据输入或显示,或用于控制应用程序执行。你使用表单自定义应用程序从查询或表中提取的数据的演示文稿。
Form is an object in a desktop database designed primarily for data input or display or for control of application execution. You use forms to customize the presentation of data that your application extracts from queries or tables.
-
Forms are used for entering, modifying, and viewing records.
-
The reason forms are used so often is that they are an easy way to guide people toward entering data correctly.
-
When you enter information into a form in Access, the data goes exactly where the database designer wants it to go in one or more related tables.
Report
报告是桌面数据库中的对象,用于对所选数据进行格式化、计算、打印和总结。
Report is an object in desktop databases designed for formatting, calculating, printing, and summarizing selected data.
-
You can view a report on your screen before you print it.
-
If forms are for input purposes, then reports are for output.
-
Anything you plan to print deserves a report, whether it is a list of names and addresses, a financial summary for a period, or a set of mailing labels.
-
Reports are useful because they allow you to present components of your database in an easy-to-read format.
-
You can even customize a report’s appearance to make it visually appealing.
-
Access offers you the ability to create a report from any table or query.
Other MS Access Objects
现在让我们来看一下其他 MS Access 对象。
Let us now take a look at other MS Access objects.
Macro
此对象是一个或多个操作的结构化定义,您希望 Access 在响应定义的事件时执行这些操作。Access Macro 是执行某些工作的脚本。例如,要创建一个打开报告的按钮,您可以使用一个会触发 OpenReport 操作的宏。
This object is a structured definition of one or more actions that you want Access to perform in response to a defined event. An Access Macro is a script for doing some job. For example, to create a button which opens a report, you could use a macro which will fire OpenReport action.
-
You can include simple conditions in macros to specify when one or more actions in the macro should be performed or skipped.
-
You can use macros to open and execute queries, to open tables, or to print or view reports.
-
You can also run other macros or Visual Basic procedures from within a macro.
-
Data macros can be attached directly to table events such as inserting new records, editing existing records, or deleting records.
-
Data macros in web apps can also be stand-alone objects that can be called from other data macros or macro objects.
Module
模块是桌面数据库中的一个对象,它包含您使用 Visual Basic 编码的自定义过程。模块提供了更加离散的操作流程,并允许您捕获错误。
Module is an object in desktop databases containing custom procedures that you code using Visual Basic. Modules provide a more discrete flow of actions and allow you to trap errors.
-
Everything that can be done in a macro can also be done in a module, but you don’t get the macro interface that prompts you what is needed for each action.
-
Modules are far more powerful, and are essential if you plan to write code for a multi-user environment, because macros cannot include error handling.
-
Modules can be standalone objects containing functions that can be called from anywhere in your application, or they can be directly associated with a form or a report to respond to events on the associated form or report.
Ms Access - Create Database
在本章中,我们将介绍启动 Access 和创建数据库的基本过程。本章还将说明如何使用模板创建桌面数据库以及如何从头开始构建数据库。
In this chapter, we will be covering the basic process of starting Access and creating a database. This chapter will also explain how to create a desktop database by using a template and how to build a database from scratch.
要从模板创建数据库,我们首先需要打开 MS Access,您将看到以下屏幕,其中显示不同的 Access 数据库模板。
To create a database from a template, we first need to open MS Access and you will see the following screen in which different Access database templates are displayed.
要查看所有可能的数据库,您可以向下滚动,也可以使用搜索框。
To view the all the possible databases, you can scroll down or you can also use the search box.
让我们在搜索框中输入项目并按 Enter 键。您将看到与项目管理相关的数据库模板。
Let us enter project in the search box and press Enter. You will see the database templates related to project management.
选择第一个模板。您将看到有关此模板的更多信息。
Select the first template. You will see more information related to this template.
在选择与您的要求相关的模板之后,在 File name 字段中输入名称,如果您愿意,还可以为您的文件指定另一个位置。
After selecting a template related to your requirements, enter a name in the File name field and you can also specify another location for your file if you want.
现在,按创建选项。Access 将下载该数据库模板,并打开一个新的空白数据库,如下面的屏幕截图所示。
Now, press the Create option. Access will download that database template and open a new blank database as shown in the following screenshot.
现在,单击左侧的导航窗格,您将看到随此数据库附带的所有其他对象。
Now, click the Navigation pane on the left side and you will see all the other objects that come with this database.
点击导航“项目”,并在菜单中选择对象类型。
Click the Projects Navigation and select the Object Type in the menu.
现在您将会看到所有的对象类型——表、查询等。
You will now see all the objects types — tables, queries, etc.
Create Blank Database
有时数据库要求非常具体,以至于使用并修改现有模板需要比仅仅从头创建一个数据库更多的工作。在这种情况下,我们将使用空白的数据库。
Sometimes database requirements can be so specific that using and modifying the existing templates requires more work than just creating a database from scratch. In such case, we make use of blank database.
Step 1 − 让我们从打开 MS Access 开始。
Step 1 − Let us now start by opening MS Access.
Step 2 − 选择空白桌面数据库。输入名称,并点击“创建”按钮。
Step 2 − Select Blank desktop database. Enter the name and click the Create button.
Step 3 − Access 将创建一个新的空白数据库,并会打开一个表格,其中也是完全空白的。
Step 3 − Access will create a new blank database and will open up the table which is also completely blank.
Ms Access - Data Types
表格中的每个字段都有属性,这些属性定义了字段的特征和行为。字段最重要的属性是它的数据类型。字段的数据类型决定了它可以存储哪种类型的数据。MS Access 支持不同类型的数据,每种类型都有特定用途。
Every field in a table has properties and these properties define the field’s characteristics and behavior. The most important property for a field is its data type. A field’s data type determines what kind of data it can store. MS Access supports different types of data, each with a specific purpose.
-
The data type determines the kind of the values that users can store in any given field.
-
Each field can store data consisting of only a single data type.
以下是你将在典型的 Microsoft Access 数据库中发现使用的一些最常见的数据类型。
Here are some of the most common data types you will find used in a typical Microsoft Access database.
Type of Data |
Description |
Size |
Short Text |
Text or combinations of text and numbers, including numbers that do not require calculating (e.g. phone numbers). |
Up to 255 characters. |
Long Text |
Lengthy text or combinations of text and numbers. |
Up to 63, 999 characters. |
Number |
Numeric data used in mathematical calculations. |
1, 2, 4, or 8 bytes (16 bytes if set to Replication ID). |
Date/Time |
Date and time values for the years 100 through 9999. |
8 bytes |
Currency |
Currency values and numeric data used in mathematical calculations involving data with one to four decimal places. |
8 bytes |
AutoNumber |
A unique sequential (incremented by 1) number or random number assigned by Microsoft Access whenever a new record is added to a table. |
4 bytes (16 bytes if set to Replication ID). |
Yes/No |
Yes and No values and fields that contain only one of two values (Yes/No, True/False, or On/Off). |
1 bit. |
-
If you use previous versions of Access, you will notice a difference for two of those data types.
-
In Access 2013, we now have two data types — short text and long text. In previous versions of Access these data types were called text and memo.
-
The text field is referred to as short text and your memo field is now called long text.
以下是 Access 中一些其他更专门的数据类型,您可以从其中进行选择。
Here are some of the other more specialized data types, you can choose from in Access.
Data Types |
Description |
Size |
Attachment |
Files, such as digital photos. Multiple files can be attached per record. This data type is not available in earlier versions of Access. |
Up to about 2 GB. |
OLE objects |
OLE objects can store pictures, audio, video, or other BLOBs (Binary Large Objects) |
Up to about 2 GB. |
Hyperlink |
Text or combinations of text and numbers stored as text and used as a hyperlink address. |
Up to 8,192 (each part of a Hyperlink data type can contain up to 2048 characters). |
Lookup Wizard |
The Lookup Wizard entry in the Data Type column in the Design view is not actually a data type. When you choose this entry, a wizard starts to help you define either a simple or complex lookup field. A simple lookup field uses the contents of another table or a value list to validate the contents of a single value per row. A complex lookup field allows you to store multiple values of the same data type in each row. |
Dependent on the data type of the lookup field. |
Calculated |
You can create an expression that uses data from one or more fields. You can designate different result data types from the expression. |
You can create an expression that uses data from one or more fields. You can designate different result data types from the expression. |
这是在 Microsoft Access 表中创建字段时可以选择的所有不同数据类型。
These are all the different data types that you can choose from when creating fields in a Microsoft Access table.
Ms Access - Create Tables
当您创建数据库时,您将您的数据存储在表中。因为其他数据库对象在很大程度上依赖表,所以您应该始终通过创建其所有表然后创建任何其他对象来启动数据库的设计。在创建表之前,请仔细考虑您的需求并确定您需要的所有表。
When you create a database, you store your data in tables. Because other database objects depend so heavily on tables, you should always start your design of a database by creating all of its tables and then creating any other object. Before you create tables, carefully consider your requirements and determine all the tables that you need.
让我们尝试并创建第一个表,该表将存储有关员工的基本联系信息,如下表所示 −
Let us try and create the first table that will store the basic contact information concerning the employees as shown in the following table −
Field Name |
Data Type |
EmployeelD |
AutoNumber |
FirstName |
Short Text |
LastName |
Short Text |
Address1 |
Short Text |
Address2 |
Short Text |
City |
Short Text |
State |
Short Text |
Zip |
Short Text |
Phone |
Short Text |
Phone Type |
Short Text |
现在让我们将简短文本作为所有这些字段的数据类型,并在 Access 中打开一个空白数据库。
Let us now have short text as the data type for all these fields and open a blank database in Access.
在上次的基础上继续。我们创建了数据库,然后 Access 会自动为表单打开这张表-单张数据表视图。
This is where we left things off. We created the database and then Access automatically opened up this table-one-datasheet view for a table.
现在,让我们转到“字段”选项卡,您将看到它也已自动创建。ID 是一个“自动编号”字段,用作我们的唯一标识符,并且是该表的“主键”。
Let us now go to the Field tab and you will see that it is also automatically created. The ID which is an AutoNumber field acts as our unique identifier and is the primary key for this table.
ID 字段已经创建,现在我们想要重命名它以满足我们的条件。这是一个“员工”表,并且它将成为我们员工的唯一标识符。
The ID field has already been created and we now want to rename it to suit our conditions. This is an Employee table and this will be the unique identifier for our employees.
在功能区中单击 Name & Caption 选项,您会看到以下对话框。
Click on the Name & Caption option in the Ribbon and you will see the following dialog box.
将此字段的名称更改为 EmployeeID ,以使其更适用于此表格。如果需要,输入其他可选信息,然后单击“确定”。
Change the name of this field to EmployeeID to make it more specific to this table. Enter the other optional information if you want and click Ok.
现在,我们有了标题为“员工 ID”的员工 ID 字段。此字段自动设置为“自动编号”,因此实际上我们无需更改数据类型。
We now have our employee ID field with the caption Employee ID. This is automatically set to auto number so we don’t really need to change the data type.
现在,让我们通过单击 click to add 添加更多字段。
Let us now add some more fields by clicking on click to add.
选择 Short Text 作为字段。当选择“短文本”时,Access 将自动突出显示该字段名称,并且您所需要做的就是键入字段名称。
Choose Short Text as the field. When you choose short text, Access will then highlight that field name automatically and all you have to do is type the field name.
键入 FirstName 作为字段名称。同样,添加所有必需的字段,如下图所示。
Type FirstName as the field name. Similarly, add all the required fields as shown in the following screenshot.
在添加完所有字段后,单击“保存”图标。
Once all the fields are added, click the Save icon.
您现在将看到 Save As 对话框,在其中可以为表输入表名。
You will now see the Save As dialog box, where you can enter a table name for the table.
在“表名”字段中输入表的名称。此处的 tbl 前缀表示表。让我们单击“确定”,您将在导航窗格中看到您的表。
Enter the name of your table in the Table Name field. Here the tbl prefix stands for table. Let us click Ok and you will see your table in the navigation pane.
Table Design View
鉴于我们已经使用 Datasheet View 创建了一个表。我们现在将使用 Table Design View 创建另一个表。我们将在该表中创建以下字段。这些表将存储有关各种书籍项目的部分信息。
As we have already created one table using Datasheet View. We will now create another table using the Table Design View. We will be creating the following fields in this table. These tables will store some of the information for various book projects.
Field Name |
Data Type |
Project ID |
AutoNumber |
ProjectName |
Short Text |
ManagingEditor |
Short Text |
Author |
Short Text |
PStatus |
Short Text |
Contracts |
Attachment |
ProjectStart |
Date/Time |
ProjectEnd |
Date/Time |
Budget |
Currency |
ProjectNotes |
Long Text |
让我们现在转至“创建”选项卡。
Let us now go to the Create tab.
在“表格”组中,单击“表格”,您会看到它与数据透视图完全不同。在此视图中,可以看到 field name 和 data type 并排。
In the tables group, click on Table and you can see this looks completely different from the Datasheet View. In this view, you can see the field name and data type side by side.
我们现在需要将 ProjectID 设为该表格的主键,因此让我们选择 ProjectID ,然后单击功能区中的 Primary Key 选项。
We now need to make ProjectID a primary key for this table, so let us select ProjectID and click on Primary Key option in the ribbon.
现在,您会看到一个小键图标,它将显示在那列字段旁边。这表明该列字段是表的主键的一部分。
You can now see a little key icon that will show up next to that field. This shows that the field is part of the table’s primary key.
让我们保存此表并为其起个名字。
Let us save this table and give this table a name.
单击“确定”,您现在可以看到此表在数据透视图中的样子。
Click Ok and you can now see what this table looks like in the Datasheet View.
让我们单击功能区左上角的数据透视图按钮。
Let us click the datasheet view button on the top left corner of the ribbon.
如果您想对该表或任何特定字段进行更改,不一定要返回到设计视图来进行更改,也可以从数据透视图进行更改。让我们按照以下屏幕截图所示更新 PStatus 字段。
If you ever want to make changes to this table or any specific field, you don’t always have to go back to the Design View to change it. You can also change it from the Datasheet View. Let us update the PStatus field as shown in the following screenshot.
单击“确定”,您将看到所做的更改。
Click Ok and you will see the changes.
Ms Access - Adding Data
Access 数据库与 Microsoft Office Word 文档或 Microsoft Office PowerPoint 具有相同意义的文件不同。相反,Access 数据库是由表、窗体、报表、查询等对象组成的集合,这些对象必须共同工作才能使数据库正常运行。我们现在已经创建了两个包含数据库中所有必要字段和字段属性的表。要查看、更改、插入或删除 Access 表中的数据,可以使用该表的数据透视图。
An Access database is not a file in the same sense as a Microsoft Office Word document or a Microsoft Office PowerPoint are. Instead, an Access database is a collection of objects like tables, forms, reports, queries etc. that must work together for a database to function properly. We have now created two tables with all of the fields and field properties necessary in our database. To view, change, insert, or delete data in a table within Access, you can use the table’s Datasheet View.
-
A datasheet is a simple way to look at your data in rows and columns without any special formatting.
-
Whenever you create a new web table, Access automatically creates two views that you can start using immediately for data entry.
-
A table open in Datasheet View resembles an Excel worksheet, and you can type or paste data into one or more fields.
-
You do not need to explicitly save your data. Access commits your changes to the table when you move the cursor to a new field in the same row, or when you move the cursor to another row.
-
By default, the fields in an Access database are set to accept a specific type of data, such as text or numbers. You must enter the type of data that the field is set to accept. If you don’t, Access displays an error message −
让我们通过打开我们创建的 Access 数据库为表格添加一些数据。
Let us add some data into your tables by opening the Access database we have created.
在功能区中选择 Views → Datasheet 视图选项,并按照以下屏幕截图所示添加一些数据。
Select the Views → Datasheet View option in the ribbon and add some data as shown in the following screenshot.
同样,在第二个表中也添加一些数据,如下面的屏幕截图所示。
Similarly, add some data in the second table as well as shown in the following screenshot.
您现在可以看到,在数据表视图中插入新数据和更新现有数据非常简单,就像在电子表格中工作一样。但是,如果您想删除任何数据,则需要先选择整行,如下面的屏幕截图所示。
You can now see that inserting a new data and updating the existing data is very simple in Datasheet View as working in spreadsheet. But if you want to delete any data you need to select the entire row first as shown in the following screenshot.
现在按下删除按钮。这将显示确认消息。
Now press the delete button. This will display the confirmation message.
单击 Yes ,您将看到所选记录已删除。
Click Yes and you will see that the selected record is deleted now.
Ms Access - Query Data
查询是请求数据结果和对数据执行操作的请求。您可以使用查询来回答简单的问题、执行计算、组合来自不同表的数据,甚至添加、更改或删除表数据。
A query is a request for data results, and for action on data. You can use a query to answer a simple question, to perform calculations, to combine data from different tables, or even to add, change, or delete table data.
-
As tables grow in size they can have hundreds of thousands of records, which makes it impossible for the user to pick out specific records from that table.
-
With a query you can apply a filter to the table’s data, so that you only get the information that you want.
-
Queries that you use to retrieve data from a table or to make calculations are called select queries.
-
Queries that add, change, or delete data are called action queries.
-
You can also use a query to supply data for a form or report.
-
In a well-designed database, the data that you want to present by using a form or report is often located in several different tables.
-
The tricky part of queries is that you must understand how to construct one before you can actually use them.
Create Select Query
如果您只想查看表格中特定字段中的数据,或同时查看来自多个表格的数据,或者可能只是根据特定条件查看数据库,则可以使用 Select 查询。让我们现在来看一个简单的示例,其中我们将创建一个从 tblEmployees 表检索信息的简单查询。打开数据库并单击 Create 选项卡。
If you want to review data from only certain fields in a table, or review data from multiple tables simultaneously or maybe just see the databased on certain criteria, you can use the Select query. Let us now look into a simple example in which we will create a simple query which will retrieve information from tblEmployees table. Open the database and click on the Create tab.
单击 Query Design 。
Click Query Design.
在 Tables 选项卡上的 Show Table 对话框中,双击 tblEmployees 表,然后 Close 对话框。
In the Tables tab, on the Show Table dialog, double-click the tblEmployees table and then Close the dialog box.
在 tblEmployees 表中,双击您希望作为查询结果看到的那些字段。按照以下屏幕截图所示将这些字段添加到查询设计网格。
In the tblEmployees table, double-click all those fields which you want to see as result of the query. Add these fields to the query design grid as shown in the following screenshot.
现在,单击 Design 选项卡上的 Run ,然后单击 Run 。
Now click Run on the Design tab, then click Run.
查询运行,并且仅显示查询中指定字段中的数据。
The query runs, and displays only data in those field which is specified in the query.
Ms Access - Query Criteria
查询条件可帮助您从 Access 数据库中检索特定项目。如果某个项目与您输入的所有条件匹配,它将显示在查询结果中。当您想基于字段中的值来限制查询结果时,可以使用查询条件。
Query criteria helps you to retrieve specific items from an Access database. If an item matches with all the criteria you enter, it appears in the query results. When you want to limit the results of a query based on the values in a field, you use query criteria.
-
A query criterion is an expression that Access compares to query field values to determine whether to include the record that contains each value.
-
Some criteria are simple, and use basic operators and constants. Others are complex, and use functions, special operators, and include field references.
-
To add some criteria to a query, you must open the query in the Design View.
-
You then identify the fields for which you want to specify criteria.
Example
让我们来看一个简单的示例,其中我们在查询中使用条件。首先打开您的 Access 数据库,然后转到“创建”选项卡,然后单击“查询设计”。
Let’s look at a simple example in which we will use criteria in a query. First open your Access database and then go to the Create tab and click on Query Design.
在“显示表”对话框的“表”选项卡中,双击 tblEmployees 表,然后关闭对话框。
In the Tables tab on Show Table dialog, double-click on the tblEmployees table and then close the dialog box.
现在让我们向查询网格中添加一些字段,例如 EmployeeID、FirstName、LastName、JobTitle 和 Email,如下面的屏幕截图所示。
Let us now add some field to the query grid such as EmployeeID, FirstName, LastName, JobTitle and Email as shown in the following screenshot.
现在让我们运行您的查询,您将仅看到这些字段作为查询结果。
Let us now run your query and you will see only these fields as query result.
如果您只想看到 JobTitle 为 Marketing Coordinator 的那些字段,那么您需要为此添加条件。让我们再次转到“查询设计”,然后在 JobTitle 的“条件”行中输入 Marketing Coordinator。
If you want to see only those whose JobTitle are Marketing Coordinator then you will need to add the criteria for that. Let’s go to the Query Design again and in Criteria row of JobTitle enter Marketing Coordinator.
现在让我们再次运行您的查询,您将看到仅检索了 JobTitle 为 Marketing Coordinator 的字段。
Let us now run your query again and you will see that only Job title of Marketing Coordinators are retrieved.
如果您想为多个字段添加条件,只需在多个字段中添加条件。假设我们只想检索“Marketing Coordinator”和“Accounting Assistant”的数据;我们可以指定 OR 行运算符,如下面的屏幕截图所示 −
If you want to add criteria for multiple fields, just add the criteria in multiple fields. Let us say we want to retrieve data only for “Marketing Coordinator” and “Accounting Assistant”; we can specify the OR row operator as shown in the following screenshot −
现在让我们再次运行您的查询,您将看到以下结果。
Let us now run your query again and you will see the following results.
如果您需要使用 AND 运算符的功能,则必须在“条件”行中指定另一个条件。假设我们想检索所有 Accounting Assistants,但只检索 JobTitle 为“Pollard”的 Marketing Coordinators。
If you need to use the functionality of the AND operator, then you have to specify the other condition in the Criteria row. Let us say we want to retrieve all Accounting Assistants but only those Marketing Coordinator titles with “Pollard” as last name.
现在让我们再次运行您的查询,您将看到以下结果。
Let us now run your query again and you will see the following results.
Ms Access - Action Queries
在 MS Access 和其他 DBMS 系统中,查询不仅可以显示数据,还可以针对数据库中的数据执行各种操作。
In MS Access and other DBMS systems, queries can do a lot more than just displaying data, but they can actually perform various actions on the data in your database.
-
Action queries are queries that can add, change, or delete multiple records at one time.
-
The added benefit is that you can preview the query results in Access before you run it.
-
Microsoft Access provides 4 different types of Action Queries − Append Update Delete Make-table
-
An action query cannot be undone. You should consider making a backup of any tables that you will update by using an update query.
Create an Append Query
您可以使用附加查询从一个或多个表中检索数据,并将该数据添加到另一个表中。让我们创建一个新表,其中我们将从 tblEmployees 表中添加数据。这将是用于演示目的的临时表。
You can use an Append Query to retrieve data from one or more tables and add that data to another table. Let us create a new table in which we will add data from the tblEmployees table. This will be temporary table for demo purpose.
让我们称之为 TempEmployees ,其中包含如下图所示的字段。
Let us call it TempEmployees and this contains the fields as shown in the following screenshot.
在 Tables 选项卡上的“显示表”对话框中,双击 tblEmployees 表,然后关闭对话框。双击您要显示的字段。
In the Tables tab, on the Show Table dialog box, double-click on the tblEmployees table and then close the dialog box. Double-click on the field you want to be displayed.
让我们先运行您的查询来显示数据。
Let us run your query to display the data first.
现在让我们回到查询设计并选择 Append 按钮。
Now let us go back to Query design and select the Append button.
在“查询类型”中,选择“附加”选项按钮。这将显示以下对话框。
In the Query Type, select the Append option button. This will display the following dialog box.
从下拉列表中选择表名,然后单击确定。
Select the table name from the drop-down list and click Ok.
在查询网格中,您可以在 Append To 行中看到默认情况下所有字段都被选中,除了 Address1 。这是因为 TempEmployee 表中没有 Address1 字段。因此,我们需要从下拉列表中选择字段。
In the Query grid, you can see that in the Append To row all the field are selected by default except Address1. This because that Address1 field is not available in the TempEmployee table. So, we need to select the field from the drop-down list.
我们来看一下 Address 字段。
Let us look into the Address field.
现在让我们运行您的查询,您将看到以下确认消息。
Let us now run your query and you will see the following confirmation message.
单击 Yes 以确认您的操作。
Click Yes to confirm your action.
当您打开 TempEmployee 表时,您将看到 tblEmployees 中的所有数据都被添加到 TempEmployee 表中。
When you open the TempEmployee table, you will see all the data is added from the tblEmployees to the TempEmployee table.
Ms Access - Create Queries
让我们了解在本节中如何创建查询。
Let us understand how to create queries in this chapter.
Create an Update Query
您可以使用更新查询来更改表中的数据,并且可以使用更新查询输入条件以指定应更新哪些行。更新查询为您提供了一个机会,让您在执行更新操作之前检查更新后的数据。我们再次转到“创建”选项卡,然后单击查询设计。
You can use an Update Query to change the data in your tables, and you can use an update query to enter criteria to specify which rows should be updated. An update query provides you an opportunity to review the updated data before you perform the update. Let us go to the Create tab again and click Query Design.
在“表”选项卡上的“显示表”对话框中,双击 tblEmployees 表,然后关闭对话框。
In the Tables tab, on the Show Table dialog box, double-click on the tblEmployees table and then close the dialog box.
在 Design 选项卡上的“查询类型”组中,单击更新并在其中更新值的字段上双击。让我们说我们想将 “Rex” 的 FirstName 更新为 “Max”。
On the Design tab, in the Query Type group, click Update and double-click on the field in which you want to update the value. Let us say we want to update the FirstName of “Rex” to “Max”.
在 Update 行 Design 网格中,输入更新的值,在条件行中添加想要更新的原始值,并运行查询。这将显示确认信息。
In the Update row of the Design grid, enter the updated value and in Criteria row add the original value which you want to be updated and run the query. This will display the confirmation message.
单击 Yes 并转到数据表视图,您将看到第一条记录 — FirstName 现已更新为“Max”。
Click Yes and go to Datasheet View and you will see the first record — FirstName is updated to “Max” now.
Create a Delete Query
您可以使用删除查询从表格中删除数据,并且可以使用删除查询输入条件来指定应删除哪些行。删除查询为您提供了机会来检查在执行删除之前将要删除的行。让我们再次转到 Create 选项卡,然后单击 Query Design 。
You can use a delete query to delete data from your tables, and you can use a delete query to enter criteria to specify which rows should be deleted. A Delete Query provides you an opportunity to review the rows that will be deleted before you perform the deletion. Let us go to the Create tab again and click Query Design.
在“显示表”对话框的“表格”选项卡上,双击 tblEmployees 表,然后关闭此对话框。
In the Tables tab on the Show Table dialog box, double-click the tblEmployees table and then close the dialog box.
在 Design 选项卡的 Query Type 组中,单击 Delete 并双击 EmployeeID 。
On the Design tab, in the Query Type group, click Delete and double-click on the EmployeeID.
在设计网格的条件行中,输入 11。在这里,我们想要删除 EmployeeID 为 11 的员工。
In the Criteria row of the Design Grid, type 11. Here we want to delete an employee whose EmployeeID is 11.
现在,我们运行查询。此查询将显示确认信息。
Let us now run the query. This query will display the confirmation message.
单击 Yes 并转到数据表视图,您将看到指定的员工记录现已删除。
Click Yes and go to your Datasheet View and you will see that the specified employee record is deleted now.
Create a Make Table Query
您可以使用制作表格查询根据存储在其他表格中的数据创建新表格。让我们再次转到 Create tab 并单击 Query Design 。
You can use a make-table query to create a new table from data that is stored in other tables. Let us go to the Create tab again and click Query Design.
在“表格”选项卡,“显示表”对话框上,双击 tblEmployees 表,然后关闭此对话框。
In the Tables tab, on the Show Table dialog box, double-click the tblEmployees table and then close the dialog box.
选择所有想要复制到其他表格的字段。
Select all those fields which you want to copy to another table.
在查询类型中,选择 Make Table 选项按钮。
In the Query Type, select the Make Table option button.
您将看到以下对话框。输入您想要创建的新表格的名称,然后单击“确定”。
You will see the following dialog box. Enter the name of the new table you want to create and click OK.
现在运行您的查询。
Now run your query.
您现在将看到以下消息。
You will now see the following message.
单击 Yes ,您将看到导航窗格中创建了一个新表。
Click Yes and you will see a new table created in the navigation pane.
Ms Access - Parameter Queries
关于查询的最佳部分是您可以保存并一次又一次地运行同一查询,但是当您仅通过更改条件反复运行同一查询时,您可能考虑使查询接受参数。
The best part about queries is that you can save and run the same query again and again, but when you run the same query again and again by only changing the criteria then you might consider the query to accept parameters.
-
If you frequently want to run variations of a particular query, consider using a parameter query
-
Parameter query retrieves information in an interactive manner prompting the end user to supply criteria before the query is run.
-
You can also specify what type of data a parameter should accept.
-
You can set the data type for any parameter, but it is especially important to set the data type for numeric, currency, or date/time data.
-
When you specify the data type that a parameter should accept, users see a more helpful error message if they enter the wrong type of data, such as entering text when currency is expected.
-
If a parameter is set to accept text data, any input is interpreted as text, and no error message is displayed.
Example
现在,我们通过创建参数查询来了解一个简单的示例。让我们打开数据库,然后在“创建表”选项卡中选择“查询设计”。
Let us now take a look at a simple example by creating a parameter query. Let us open your database and select Query Design in the Create table tab.
双击 tblProjects 然后关闭 Show 对话框。
Double-click on the tblProjects and close the Show dialog box.
选择您希望作为查询结果显示的字段,如下面的屏幕截图所示。
Select the field you want to see as a query result as shown in the following screenshot.
在查询设计网格中,在 ProjectStart 列的“条件”行中键入 [Enter a project start data] 。字符串 [Enter a project start data] 是您的参数提示。方括号表示您希望查询要求输入,而文本 Enter a project start data 是显示的参数提示。
In the query design grid, in the Criteria row of the ProjectStart column, type [Enter a project start data]. The string [Enter a project start data] is your parameter prompt. The square brackets indicate that you want the query to ask for input, and the text is Enter a project start data is the parameter prompt displays.
现在,我们运行您的查询,您将看到以下提示。
Let us now run your query and you will see the following prompt.
现在,让我们输入以下日期。
Let us now enter the following date.
单击“确定”以确认。
Click OK to confirm.
结果,您将看到该项目在 2/1/2007 开始的详细信息。让我们转到设计视图并再次运行查询。
As a result, you will see the details of the project which started on 2/1/2007. Let us go to the Design View and run the query again.
按上面屏幕截图所示输入日期,然后单击“确定”。现在,您将看到该项目在 5/1/2008 开始的详细信息。
Enter the date as in the above screenshot and click Ok. You will now see the details of the project which started on 5/1/2008.
Ms Access - Alternate Criteria
查询有很多优势。您可以保存并多次运行相同的查询,并且很多时候您想要添加备选条件。
Queries come in with many advantages. You can save and run the same query again and again, and a lot of times you want to add alternate criteria.
您可以通过以下两种方式添加备选条件:
You can add alternate criteria in the following two ways −
-
You can use the OR operator to combine two sets of criteria.
-
You can also use the query design grid, but instead of specifying criteria on the same line, you will need to separate it in multiple row.
Example
让我们看一个备选条件的简单示例。打开数据库,然后在“创建”选项卡中选择 Query Design 。
Let us look at a simple example of alternate criteria. Open database and in the Create tab select Query Design.
双击 tblEmployee 然后关闭“显示表”对话框。
Double-click on tblEmployee and close the Show Table dialog box.
双击您希望作为查询结果查看的所有字段。
Double-click on all the field you want to see as query result.
您现在可以看到备用条件在 LastName 字段的不同行中指定。当您运行此查询时,您将看到姓氏为 Pollard 或 Manning 的员工。现在让我们来运行此查询。
You can now see that alternate criterion is specified in different rows of the LastName field. When you run this query, you will see the employees whose last name is either Pollard or Manning. Let us now run this query.
如您所见,由于备用条件,只有两名员工被检索到。
As you can see that only two employees have been retrieved, because of the alternate criteria.
如果您希望在多个字段中添加备用条件,那么您将必须对所有字段使用不同的行。现在,让我们添加另一个备用条件,我们可以在其中检索姓氏为 Pollard 或 Manning 或职称为 Accounting Assistant 的员工的信息。
If you want to add alternate criteria in multiple fields then you will have to use different rows for all the fields. Let us now add another alternate criterion where we can retrieve information for employees whose last name is either Pollard or Manning or the job title is Accounting Assistant.
现在让我们来运行此查询。
Let us now run this query.
您现在将看到以下结果。
You will now see the following result.
Ms Access - Relating Data
在本章中,我们将了解有关关联数据的基础知识。在讨论并创建不同数据之间的关系之前,让我们回顾一下我们为什么要这样做。这一切都归结于标准化。
In this chapter, we will understand the basics of relating data. Before talking about and creating relationships between different data, let us review why we need it. It all goes back to normalization.
Normalization
数据库标准化,或简称标准化,是组织关系数据库的列(属性)和表(关系)以最大程度地减少数据冗余的过程。这是将数据拆分为多个表以提高整体性能、完整性和生命周期的过程。
Database normalization, or simply normalization, is the process of organizing columns (attributes) and tables (relations) of a relational database to minimize data redundancy. It is the process of splitting data across multiple tables to improve overall performance, integrity and longevity.
-
Normalization is the process of organizing data in a database.
-
This includes creating tables and establishing relationships between those tables according to rules designed both to protect the data and to make the database more flexible by eliminating redundancy and inconsistent dependency.
现在让我们看看下表,其中包含数据,但问题是这些数据相当冗余,这增加了在数据输入期间出现错别字和措辞不一致的可能性。
Let us now look into the following table which contains data, but the problem is that this data is quite redundant which increases the chances of typo and inconsistent phrasing during data entry.
CustID |
Name |
Address |
Cookie |
Quantity |
Price |
Total |
1 |
Ethel Smith |
12 Main St, Arlington, VA 22201 S |
Chocolate Chip |
5 |
$2.00 |
$10.00 |
2 |
Tom Wilber |
1234 Oak Dr., Pekin, IL 61555 |
Choc Chip |
3 |
$2.00 |
$6.00 |
3 |
Ethil Smithy |
12 Main St., Arlington, VA 22201 |
Chocolate Chip |
5 |
$2.00 |
$10.00 |
为了解决此问题,我们需要重新构建我们的数据并将其分解为多个表以消除一些冗余,如下面的三张表所示。
To solve this problem, we need to restructure our data and break it down into multiple tables to eliminate some of those redundancy as shown in the following three tables.
在这里,我们有一张用于客户的表,第二张用于订单,第三张用于曲奇。
Here, we have one table for Customers, the 2nd one is for Orders and the 3rd one is for Cookies.
这里的问题是,仅将数据拆分为多个表并不能说明一张表中的数据如何与另一张表中的数据相关。要连接多张表中的数据,我们必须向 Orders 表中添加外键。
The problem here is that just by splitting the data in multiple tables will not help to tell how data from one table relates to data in another table. To connect data in multiple tables, we have to add foreign keys to the Orders table.
Defining Relationships
关系通过匹配关键列中的数据来工作,通常是两张表中具有相同名称的列。在大多数情况下,该关系将一张表的主键(为每行提供唯一标识符)与另一张表中外键中的条目匹配。表之间有三种类型的关系。创建的关系类型取决于如何定义相关列。
A relationship works by matching data in key columns usually columns with the same name in both the tables. In most cases, the relationship matches the primary key from one table, which provides a unique identifier for each row, with an entry in the foreign key in the other table. There are three types of relationships between tables. The type of relationship that is created depends on how the related columns are defined.
现在,让我们深入了解三种关系类型−
Let us now look into the three types of relationships −
One-to-Many Relationships
一对多的关系是最常见的关系类型。在这种关系类型中,表 A 中的一行可以在表 B 中有许多匹配的行,但表 B 中的一行只能在表 A 中有一行匹配的行。
A one-to-many relationship is the most common type of relationship. In this type of relationship, a row in table A can have many matching rows in table B, but a row in table B can have only one matching row in table A.
例如,“客户”表和“订单”表具有一个一对多的关系:每个客户可以下许多订单,但每个订单只能来自一个客户。
For example, the Customers and Orders tables have a one-to-many relationship: each customer can place many orders, but each order comes from only one customer.
Many-to-Many Relationships
在多对多关系中,表 A 中的一行可以有许多在表 B 中匹配的行,反之亦然。
In a many-to-many relationship, a row in table A can have many matching rows in table B, and vice versa.
您可以通过定义一个称为 junction table 的第三个表来创建这样的关系,其主键由表 A 和表 B 的外键组成。
You create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.
例如,“客户”表和“Cookie”表具有多对多关系,该关系由这两个表中与“订单”表的每个一对多关系定义。
For example, the Customers table and the Cookies table have a many-to-many relationship that is defined by a one-to-many relationship from each of these tables to the Orders table.
One-to-One Relationships
在一对一的关系中,表 A 中的一行最多只能在表 B 中有一行匹配的行,反之亦然。如果两个相关列都是主键或具有唯一约束,则会创建一对一的关系。
In a one-to-one relationship, a row in table A can have no more than one matching row in table B, and vice versa. A one-to-one relationship is created if both the related columns are primary keys or have unique constraints.
这种关系类型不常见,因为这样相关的大多数信息都将存储在一张表中。您可以将单一对一关系用于以下目的:
This type of relationship is not common because most information related in this way would be all in one table. You might use a one-to-one relationship to −
-
Divide a table into many columns.
-
Isolate part of a table for security reasons.
-
Store data that is short-lived and could be easily deleted by simply deleting the table.
-
Store information that applies only to a subset of the main table.
Ms Access - Create Relationships
在本章中,我们将了解创建相关表之间关系的必要性。良好的数据库设计目标之一是消除数据冗余。
In this chapter, we will understand the need to create relationships between related tables. One of the goals of good database design is to remove data redundancy.
-
To achieve that goal, you divide your data into many subject-based tables so that each fact is represented only once.
-
To do this, all the common fields which are related to each other are placed in one table.
-
To do this step correctly, you must first understand the relationship between your tables, and then specify these relationships in your Access database.
Why Create Table Relationships?
当您需要在数据库对象中使用表时,MS Access 使用表关系连接表。在创建其他数据库对象(例如窗体、查询、宏和报表)之前,您应该创建表关系的原因有多个。
MS Access uses table relationships to join tables when you need to use them in a database object. There are several reasons why you should create table relationships before you create other database objects, such as forms, queries, macros, and reports.
-
To work with records from more than one table, you often must create a query that joins the tables.
-
The query works by matching the values in the primary key field of the first table with a foreign key field in the second table.
-
When you design a form or report, MS Access uses the information it gathers from the table relationships you have already defined to present you with informed choices and to prepopulate property settings with appropriate default values.
-
When you design a database, you divide your information into tables, each of which has a primary key and then add foreign keys to related tables that reference those primary keys.
-
These foreign key-primary key pairings form the basis for table relationships and multi-table queries.
现在,让我们使用表设计在您的数据库中添加另一个表,并将其命名为 tblHRData ,如下面的屏幕截图所示。
Let us now add another table into your database and name it tblHRData using Table Design as shown in the following screenshot.
单击 Save 图标,如上面的屏幕截图所示。
Click on the Save icon as in the above screenshot.
输入 tblHRData 作为表名,然后单击 Ok 。
Enter tblHRData as table name and click Ok.
tblHRData 现在已创建并包含数据。
tblHRData is now created with data in it.
Ms Access - One-To-One Relationship
现在,让我们了解 MS Access 中的一对一关系。此关系用于将一个表中的一条记录与另一个表中的一条且仅一条记录关联起来。
Let us now understand One-to-One Relationship in MS Access. This relationship is used to relate one record from one table to one and only one record in another table.
现在,让我们转到 Database Tools 选项卡。
Let us now go to the Database Tools tab.
单击 Relationships 选项。
Click on the Relationships option.
选择 tblEmployees 和 tblHRData ,然后单击“添加”按钮,将它们添加到我们的视图中,然后关闭 Show Table 对话框。
Select tblEmployees and tblHRData and then click on the Add button to add them to our view and then close the Show Table dialog box.
若要在这两个表之间创建关系,请使用鼠标,单击并按住 tblEmployees 中的 EmployeeID 字段,在将鼠标悬停在 tblHRData 中的 EmployeeID 上时,将该字段拖放到我们想要关联的字段上。当你释放鼠标按钮,Access 就会打开以下窗口 −
To create a relationship between these two tables, use the mouse, and click and hold the EmployeeID field from tblEmployees and drag and drop that field on the field we want to relate by hovering the mouse right over EmployeeID from tblHRData. When you release your mouse button, Access will then open the following window −
上面的窗口将 tblEmployees 的 EmployeeID 与 tblHRData 的 EmployeeID 关联起来。现在让我们单击 Create 按钮,现在这两个表已经相关。
The above window relates EmployeeID of tblEmployees to EmployeeID of tblHRData. Let us now click on the Create button and now these two tables are related.
现在关系会自动保存,无需单击“保存”按钮。既然我们已经创建了最基本的关系,现在让我们转到表侧以了解此关系发生了什么。
The relationship is now saved automatically and there’s no real need to click on the Save button. Now that we have the most basic of relationships created, let us now go to the table side to see what has happened with this relationship.
让我们打开 tblEmployees 表。
Let us open the tblEmployees table.
此处,在每条记录的左侧,默认情况下你将看到一个小的加号。当您创建关系时,Access 会自动向该表添加一个子数据表。
Here, on the left-hand side of each and every record, you will see a little plus sign by default. When you create a relationship, Access will automatically add a sub-datasheet to that table.
让我们单击加号按钮,您将看到与此记录相关的信息位于 tblHRData 表上。
Let us click on the plus sign and you will see the information that is related to this record is on the tblHRData table.
单击 Save 图标并打开 tblHRData ,您将看到我们输入的数据已经在此处。
Click on the Save icon and open tblHRData and you will see that the data we have entered is already here.
Ms Access - One-To-Many Relationship
您的大多数关系很可能都是一对多的关系,其中一个表中的一个记录有可能与另一个表中的许多记录相关联。
The vast majority of your relationships will more than likely be this one to many relationships where one record from a table has the potential to be related to many records in another table.
创建多对一关系的过程与创建一对一关系的过程完全相同。
The process to create one-to-many relationship is exactly the same as for creating a one-to-one relationship.
我们首先通过单击 Clear Layout 中的 Design tab 选项来清除布局。
Let us first clear the layout by clicking on the Clear Layout option on the Design tab.
我们首先将添加另一个表 tblTasks ,如下面的屏幕截图所示。
We will first add another table tblTasks as shown in the following screenshot.
单击 Save 图标,输入 tblTasks 作为表名,然后转到 Relationship 视图。
Click on the Save icon and enter tblTasks as the table name and go to the Relationship view.
单击 Show Table 选项。
Click on the Show Table option.
添加 tblProjects 和 tblTasks 并关闭 Show Table 对话框。
Add tblProjects and tblTasks and close the Show Table dialog box.
我们可以再次运行相同的过程以关联这些表。单击并按住 tblProjects 中的 ProjectID,然后将其一直拖到 tblTasks 中的 ProjectID。此外,当您松开鼠标时,将弹出一个关系窗口。
We can run through the same process once again to relate these tables. Click and hold ProjectID from tblProjects and drag that all the way over to the ProjectID from tblTasks. Further, a relationships window pops up when you release the mouse.
单击创建按钮。我们现在已经创建了一个非常简单的关系。
Click the Create button. We now have a very simple relationship created.
Ms Access - Many-To-Many Relationship
在本章中,让我们了解多对多关系。要表示多对多关系,您必须创建一个第三张表(通常称为交集表),该表将多对多关系分解为两个一对多关系。为此,我们还需要添加一个交集表。让我们首先添加另一张表 tblAuthers 。
In this chapter, let us understand Many-to-Many Relationship. To represent a many-tomany relationship, you must create a third table, often called a junction table, that breaks down the many-to-many relationship into two one-to-many relationships. To do so, we also need to add a junction table. Let us first add another table tblAuthers.
现在让我们创建一个 many-to-many relationship 。我们有多个作者在从事多个项目,反之亦然。如您所知,我们在 tblProjects 中有一个 Author 字段,因此我们为其创建了一个表。我们不再需要此字段。
Let us now create a many-to-many relationship. We have more than one author working on more than one project and vice versa. As you know, we have an Author field in tblProjects so, we have created a table for it. We do not need this field any more.
选择 Author 字段并按删除按钮,您将看到以下消息。
Select the Author field and press the delete button and you will see the following message.
单击 Yes 。现在我们必须创建一个交集表。此交集表包含两个外键,如下面的屏幕截图所示。
Click Yes. We will now have to create a junction table. This junction table have two foreign keys in it as shown in the following screenshot.
这些外键字段将是连接在一起的两个表的主键,即 tblAuthers 和 tblProjects 。
These foreign key fields will be the primary keys from the two tables that were linked together — tblAuthers and tblProjects.
要在 Access 中创建复合键,请选择这两个字段,然后在 table tools design 选项卡中,您可以直接单击该主键,这将标记这两个字段,而不仅仅是一个字段。
To create a composite key in Access, select both these fields and from the table tools design tab, you can click directly on that primary key and that will mark not one but both of these fields.
这两个字段的组合是 tables’ unique identifier 。现在让我们将此表另存为 tblAuthorJunction 。
The combination of these two fields is the tables’ unique identifier. Let us now save this table as tblAuthorJunction.
将多对多关系结合在一起的最后一步是返回到该 relationships view ,然后通过单击 Show Table 来创建这些关系。
The last step in bringing the many-to-many relationships together is to go back to that relationships view and create those relationships by clicking on Show Table.
选择上面突出显示的三个表,然后单击添加按钮,然后关闭此对话框。
Select the above three highlighted tables and click on the Add button and then close this dialog box.
单击并拖动 tblAuthors 中的 AuthorID 字段,然后将其放在 tblAuthorJunction 表 AuthorID 上。
Click and drag the AuthorID field from tblAuthors and place it on top of the tblAuthorJunction table AuthorID.
您正在创建的关系是 Access 会将其视为一对多关系的关系。我们还将强制参照完整性。让我们现在打开 Cascade Update ,然后单击 Create 按钮,如上面的屏幕截图所示。
The relationship you’re creating is the one that Access will consider as a one-to-many relationship. We will also enforce referential integrity. Let us now turn on Cascade Update and click on the Create button as in the above screenshot.
现在让我们按住 ProjectID ,然后直接从 tblAuthorJunction 将其拖放到 ProjectID 上。
Let us now hold the ProjectID, drag and drop it right on top of ProjectID from tblAuthorJunction.
我们将 Enforce Referential Integrity 和 Cascade Update Related Fields 。
We will Enforce Referential Integrity and Cascade Update Related Fields.
以下是多对多关系。
The following are the many-to-many relationships.
Ms Access - Wildcards
通配符是特殊字符,可以在文本值中代表未知字符,并且非常适合于查找具有相似但不相同数据的多个项目。通配符还可以帮助根据特定模式匹配获取数据库。
Wildcards are special characters that can stand in for unknown characters in a text value and are handy for locating multiple items with similar, but not identical data. Wildcards can also help with getting databased on a specified pattern match.
Access 支持两组通配符,因为它支持两种结构化查询语言标准。
Access supports two sets of wildcard characters because it supports two standards for Structured Query Language.
-
ANSI-89
-
ANSI-92
一般来说,在对 Access 数据库(例如 .mdb 和 .accdb 文件)运行 queries 和 find-and-replace 操作时,您使用 ANSI-89 通配符。
As a rule, you use the ANSI-89 wildcards when you run queries and find-and-replace operations against Access databases such as .mdb and .accdb files.
在对 Access 项目(连接到 Microsoft SQL Server 数据库的 Access 文件)运行查询时,您使用 ANSI-92 通配符。Access 项目使用 ANSI-92 标准,因为 SQL Server 使用该标准。
You use the ANSI-92 wildcards when you run queries against Access projects — Access files connected to Microsoft SQL Server databases. Access projects use the ANSI-92 standard because SQL Server uses that standard.
ANSI-89 Wildcard Characters
下表列出了 ANSI-89 支持的字符 -
The following table lists out characters supported by ANSI-89 −
Character |
Description |
Example |
* |
Matches any number of characters. You can use the asterisk (*) anywhere in a character string. |
wh* finds what, white, and why, but not awhile or watch. |
? |
Matches any single alphabetic character. |
B?ll finds ball, bell, and bill. |
[ ] |
Matches any single character within the brackets. |
B[ae]ll finds ball and bell, but not bill. |
! |
Matches any character not in the brackets. |
b[!ae]ll finds bill and bull, but not ball or bell. |
- |
Matches any one of a range of characters. You must specify the range in ascending order (A to Z, not Z to A). |
b[a-c]d finds bad, bbd, and bcd. |
# |
Matches any single numeric character. |
1#3S finds 103, 113, and 123. |
ANSI-92 wildcard characters
下表列出了 ANSI-92 支持的字符 -
The following table lists out characters supported by ANSI-92 −
Character |
Description |
Example |
% |
Matches any number of characters. It can be used as the first or last character in the character string. |
wh% finds what, white, and why, but not awhile or watch. |
_ |
Matches any single alphabetic character. |
B_ll finds ball, bell, and bill. |
[ ] |
Matches any single character within the brackets. |
B[ae]ll finds ball and bell, but not bill. |
^ |
Matches any character not in the brackets. |
b[^ae]ll finds bill and bull, but not ball or bell. |
- |
Matches any one of a range of characters. You must specify the range in ascending order (A to Z, not Z to A). |
b[a-c]d finds bad, bbd, and bcd. |
Example
现在,让我们通过打开查询设计来了解使用这些通配符的简单示例。
Let us now look at a simple example of using these wildcard characters by opening the query design.
添加高亮的表并关闭 Show Table 对话框。
Add the highlighted tables and close the Show Table dialog box.
在查询网格中添加想要作为查询结果查看的字段。
Add the fields in the query grid which you want to see as a query result.
运行查询。
Run your query.
让我们再次转到 query design 并添加项目名称提示符。
Let us again go to the query design and add prompt for project name.
现在运行查询,让我们假设你不知道确切的项目名称,但你知道项目名称包含单词“potion”。单击 Ok 。
Now run your query and let us assume that you don’t know the exact project name, but you know that the project name contains the words “potion”. Click Ok.
上述步骤不会生成任何结果。这是因为 Access 正在 project name field 中寻找完全匹配。它正在寻找名称中包含单词 potion 的项目。
The above step does not generate any result. That is because Access is looking for an exact match in the project name field. It is looking for that project which has the word potion in its name.
如果你想要让用户输入通配符来替换未知字符,那么你需要调整条件并包括运算符 like 。
If you want it so that the users can enter wildcards to replace unknown characters, then you need to adjust the criteria and include the operator like.
运行查询时,用户可以使用通配符替换任意数量的字符。
When you run the query, users can use wildcards to replace any number of characters.
让我们假设你知道单词 potion 出现在标题中的某处,但你无法确定具体位置。
Let us assume that you know that the word potion appears somewhere in the title but you are not exactly sure where.
你可以在 potion 前添加一个 * 来替换任意数量的字符,然后在后面再添加一个 。单击 *Ok 。
You can add an * to replace any number of characters before the word potion and then add another * after the. Click Ok.
用户了解他们可以输入哪些通配符总是有益的。但有些用户可能不知道他们可以和不可以输入哪些通配符。在这种情况下,你可以自己输入通配符。
It is always good for the users to know the wildcards they can enter. But there are users who may not know the wildcards they can and cannot enter. In that case, you can enter the wildcards yourself.
在这种情况下,我们可以在 Like 运算符和我们的参数提示符之间添加这些通配符,现在我们必须用一种非常明确的方式来编写它。在带引号的 like 词后,输入我们正在使用的通配符。在这种情况下,我们使用了“ ” to replace any number of characters. We will now add this to the parameter. To do so, we need ampersand *(&) 符号和一个空格。现在,我们将重复此操作,并添加另一个和号 (&),因为我们正在将该通配符连接到用户为该项目名称输入的任何条件,然后在引号中输入“*”。
In this situation, in-between the Like operator and our parameter prompt, we can add those wildcards and now there’s a very specific way we have to write this. After the word like in quotation marks, enter the wildcard that we are using. In this case, we have used “” to replace any number of characters. We will now add this to the parameter. To do so, we need ampersand *(&) symbol and a space. We will now repeat this step and add another ampersand (&) because we’re joining that wildcard to whatever criteria is entered in for that project name by the user and then “*” in quotes.
我们现在重新运行查询。在提示中输入单词 potion ,不使用通配符。
Let us now run our query again. Enter the word potion in the prompt without any wildcards.
该查询现在将在此处输入内容的任一侧跟踪这些通配符。我们需要直接输入药剂并按 Enter。
The query will now track those wildcards on either side of whatever is entered here. We need to simply type potion and press enter.
我们现在得到了我们想要的结果,无论用户输入什么,结果都将相同。
We now get the results we were looking for and the results will be the same regardless of what the users enter.
让我们假设我们要查找标题中包含单词“the”的每个项目。然后,您只需要输入该单词和 Enter 或单击 Ok 。
Let us say we want to find every project with the word “the” in the title. Then, you need to just type the word and Enter or click Ok.
使用此查询,可以更容易地搜索名称中包含单词“the”的项目。第 2 级结果可能还包含单词“the”是其中一部分的单词“mother”的项目名称。
With this query, it becomes easier to search for projects with the word “the” in their names. The 2nd level of results may also include the project names with the word “mother” where “the” is part of the word.
Ms Access - Calculated Expression
在 MS Access 中,表达式类似于 Excel 中的公式。它包含许多元素,可以单独或组合使用以产生结果。表达式可以包括运算符、常量、函数和标识符。
In MS Access, an expression is like a formula in Excel. This consists of a number of elements that can be used alone or in a combination to produce a result. Expressions can include operators’, constants, functions and identifiers.
-
Expressions can be used to perform a variety of tasks from retrieving the value of a control or supplying criteria to a query to creating calculated controls and fields or defining a group level for report.
-
In this chapter, we will be focusing on creating a very specific kind of expression called a calculated expression. We will create several calculated fields that will calculate and display data that is not stored anywhere within the database itself but calculated from separate fields that are stored.
我们现在有一个包含更多要计算数据的新 Access 数据库。
We now have a new Access database that contains more data to calculate.
Example
让我们尝试一个示例来理解这一点。我们将从各种表—— tblCustomers, tblOrders, tblOrdersDetails 和 tblBooks 中检索一些信息并查看各种域。
Let us try an example to understand this. We will be retrieving some information from a variety of tables — tblCustomers, tblOrders, tblOrdersDetails and tblBooks and reviewing a variety of fields.
我们现在运行查询,您将看到以下结果。
Let us now run the query and you will see the following results.
我们想要一个非常简单的子计计算,该计算将采用订购的书籍数量并乘以该书籍的零售价。最好在每个表达式或任何表达式开头为要调用的字段指定一个名称。我们称之为 subtotal 。
We want a very simple calculation for a subtotal which will take the quantity of the books ordered and multiply that by the retail price of the book. It is a good idea to begin every expression or any expression with a name to call that field. Let us call this one subtotal.
完整表达式 (Subtotal: [tblOrdersDetails]![QTY] [tblBooks]![RetailPrice])* 包括表名,首先是 tblOrdersDetails 放在方括号内,因为那里是我们数量域所在的地方。现在一个感叹号告诉 Access 在该表中查找 QTY 域,并将其乘以 tblBooks 中的零售价域。
The full expression (Subtotal: [tblOrdersDetails]![QTY][tblBooks]![RetailPrice])* includes the table name, first tblOrdersDetails inside square brackets because that is where our quantity field lives. Now an exclamation is telling Access to look inside that table for a field QTY and multiply that by the retail price field in tblBooks.
让我们尝试运行您的查询,您将在末尾看到子计字段。
Let us try to run your query and you will see at end the subtotal field.
假设我们要计算我们的销售税。在查询网格中,我们会以与之前完全相同的方式操作,这次我们将把该域简单地称为 Tax,然后将 Subtotal 乘以百分之九的税率。
Let us say we want to calculate our sales tax. In the query grid we will do in the exact same way we did before and this time we will call the field simply Tax and Let us multiply Subtotal by nine percent tax rate.
让我们再次运行您的查询,您将在末尾看到新的 Tax field 。
Let us run your query again and you will see the new Tax field in the end.
Ms Access - Indexing
索引是一种数据结构,一种特殊的数据结构,旨在提高数据检索速度。如果您经常按特定字段搜索表或按该字段对记录进行排序,则可以通过为该字段创建索引来加快这些操作。Microsoft Access 在表中使用索引的方式与您在书中使用索引查找数据的方式相同。
An index is a data structure, a special data structure designed to improve the speed of data retrieval. If you often search a table or sort its records by a particular field, you can speed up these operations by creating an index for the field. Microsoft Access uses indexes in a table as you use an index in a book to find data.
-
In some instances, such as for a primary key, Access automatically creates an index for you.
-
At other times, you might want to create an index yourself.
-
An index stores the location of records based on the field or fields that you choose to index.
-
After Access obtains the location from the index, it can then retrieve the data by moving directly to the correct location.
-
In this way, using an index can be considerably faster than scanning through all of the records to find the data.
-
Indexes can speed up searches in queries, but they can also slow down performance when adding or updating records.
现在让我们来了解 Microsoft Access 默认创建的索引以及我们如何自己创建索引,并学习删除任何不必要的索引。在 Access 数据库中打开我们创建的 tblEmployees 表。
Let us now look into what indexes Microsoft Access creates by default and how to create them ourselves and learn how to delete any unnecessary indexes. Open the tblEmployees table in Access database which we have created.
-
We haven’t really played with indexes in this database but that doesn’t mean we don’t have any.
-
In fact, any field that is set as a primary key in Access is automatically indexed.
-
Access creates additional secondary indexes depending on the names of your fields.
现在,让我们转到“文件”菜单,然后选择“选项”。
Let us now go to the File menu and select Options.
您将看到“Access 选项”窗口。
You will see the Access Options window.
转到 Object Designers ,您将看到一个标题为“导入/创建自动编制索引”的部分,并在文本框中看到 ID;key;code;num。默认情况下,Access 会自动为以这些名称开头或结尾的字段添加二级索引,这适用于您导入的字段以及您手动创建的字段。
Go to the Object Designers and you will see a section labeled AutoIndex on Import/Create and in the textbox you will see ID;key;code;num. By default, access automatically adds a secondary index to fields that start or end with these names and that goes for fields you have imported as well as ones you have manually created.
如果要使任何字段建立索引,可以转到 Field 选项卡。
If you want to make any field indexed you can go to the Field tab.
选择您希望建立索引的任何字段,然后选中“字段验证”部分中的“已建立索引”复选框。您还有替代选项可以创建或删除索引。您可以返回 Design View 。
Select any field that you want indexed and check the Indexed checkbox in Field Validation section. You also have alternate options for creating or removing an index. You can go back to the Design View.
您可以通过选择任何字段来调整索引。您还可以在下面的字段属性区域中查看它们的建立索引方式。具有 No 下拉菜单项旁边选定的任何字段表示该指定字段没有索引。您可以通过单击下拉菜单并选择其他两个选项( Yes (Duplicate OK) 和 Yes (No duplicates) )来更改该选项。
You can adjust indexes by selecting any field. You can also see how they are indexed in the field properties area below. Any field that has No selected next to indexed, means there is no index for that given field. You can change that by clicking on the drop-down menu and choosing the other two options — Yes (Duplicate OK) and Yes (No duplicates).
最后一个选项 Yes (No Duplicates) 表示 Access 将自动禁止该字段中的重复值。现在让我们为我们的姓氏字段创建索引。
The last option Yes (No Duplicates) means that Access will automatically prohibit duplicate values in that field. Let us now create an index for our last name field.
让我们选择 LastName 建立索引并说 Yes (Duplicates OK) 。当我们保存时,Access 将创建该索引。您可以查看和调整表索引的另一个区域是 Show/Hide 组中的“表设计”区域。
Let us select LastName to index and say Yes (Duplicates OK). As we save, Access will create that index. Another area where you can view and adjust your indexes for a table is the tables design area in the Show/Hide group.
如果您单击此“索引”按钮,则会弹出一个特殊视图,显示针对该表创建的所有索引。
If you click on this Indexes button that will bring up a special view displaying all the indexes created for this table.
我们现在有两个 tblEmployees 索引,一个基于主键字段自动创建,另一个是我们刚为 LastName 字段创建的。这些是在 Microsoft Access 表中处理索引的不同方式。
We now have two indexes for tblEmployees —one that was created automatically based on the primary key field and one that we just created for the LastName field. These are the different ways to deal with indexes in Microsoft Access tables.
Ms Access - Grouping Data
在本章中,我们将介绍如何在 Access 中按一组记录进行计算。我们已经创建了一个按行或按记录进行计算的字段来创建行总计或小计字段,但是如果我们希望按一组记录而不是按各个记录进行计算该怎么办?我们可以通过创建称为 Aggregate Query 的内容来执行此操作。
In this chapter, we will be covering how to calculate on a group of records in Access. We have created a field that calculates row by row or record by record to create a line total or subtotal field, but what if we wanted to calculate down by a group of records rather than by individual ones. We can do this by creating what’s known as an Aggregate Query.
Aggregate Query
聚合查询也称为总计或汇总查询是一种求和、求总量或分组查询。它可以是总金额或总量,也可以是一组记录或记录子集。聚合查询可以执行多种操作。这是一个简单的表格,列出了根据一组记录求总和的一些方法。
An aggregate query also known as a totals or summary query is a sum, mass or group particulars. It can be a total or gross amount or a group or subset of records. Aggregate queries can perform a number of operations. Here’s a simple table listing some of the ways to total on a group of records.
S.No |
Aggregate Functions & Description |
1. |
Sum Adds the field values |
2. |
Avg Average of the field values |
3. |
Min Lowest (minimum) field value |
4. |
Max Highest (maximum) field value |
5. |
Count Count of the values (records) |
6. |
StDev Standard deviation of the field values including date/time fields |
7. |
Var Variance of the field values including date/time |
让我们打开数据库,转到查询设计并包含以下表格 -
Let us open your database and go to the Query Design and include the following tables −
关闭此对话框,以下两个字段将显示在查询网格中,如下面的屏幕截图所示。
Close this dialog box and the following two fields will be displayed as shown in the query grid in the following screenshot.
这是一个简单的查询,我们只显示了两个字段—— book title 和 quantity ,当我们运行它时,我们将在我们的数据库中看到每一个单个订单。
This is a simple query and we are displaying only two fields — book title and quantity and when we run it we’re seeing every single individual order in our database.
现在让我们运行这个查询,您将看到以下结果。
Let us now run this query and you will see the following results.
这就是书名重复的原因。此处为每本图书分别下了不同的订单,并且它们订购的数量不同。让我们假设我们只想查看只列出一次的书名的摘要。然后列在其旁边的数量之和。
This is why book titles are repeating. Separate orders have been placed for each of these books here and they were ordered in different quantities. Let us assume we want to see a summary of only that book title that has been listed only once. And then the sum of the quantity listed beside it.
现在让我们转到设计视图,在设计选项卡中,您会看到一个西格玛符号。这是您的总计按钮。
Let us now go to the Design View and in the Design tab, you will see a Sigma symbol. This is your totals button.
单击西格玛符号,它将在表格行中的字段下方打开另一行,您可以在此指定如何对该查询进行分组。
Click the sigma symbol which will open another row underneath your field in the table row and from here, you can specify how you are grouping this query.
现在我们按书名进行分组,并对数量字段进行求和。
We will now group by book title and also sum our quantity field.
如果我们单击 group by area 并进一步单击下拉菜单,所有的选项都将被列出来。在本例中,我们将选择选项 Sum ,然后运行您的查询。
If we click on group by area and further click on the drop-down menu, all the options will be listed down. In this case, we will choose the option Sum and then run your query.
现在您可以看到每本书,以及所有单笔订单,这些订单显示在书名旁边。
You can now see each individual book and also all the individual orders displayed beside the book name.
Concatenation in Access
我们学习了规范化的过程,将信息存储在不同的字段中。有时您希望一起查看或查看数据,例如将名字和姓氏字段合并为单个字段。您可以通过创建一个连接一个或多个字符串的计算字段来一起显示这些信息。您还可以添加您想要的其他字符,如逗号或句点。
We learned the process of normalization, storing information in separate fields. Sometimes you want to see or view data together like combining the first name and last name fields as a single field. You can display that information together by creating a calculated field that concatenates one or more strings. You can also add other characters like a comma or period that you may want.
-
To concatenate in Access, there are two different operators you can use the ampersand (&) and the plus (+) sign.
-
The ampersand will combine two strings into one whereas the plus sign will combine two strings and propagate NULL values, for example, if one value is NULL the entire expression evaluates to null.
Example
让我们举一个简单的例子来理解使用查询设计创建新查询的过程。这将是一个非常简单的查询,它将从客户表中提取信息。
Let us take a simple example to understand the process of creating a new query using query design. This is going to be a very simple query that is pulling information from our customers’ table.
现在,让我们添加 tblCustomers 关闭表的表格对话框。我们尝试将一些简单的命名字段与一些连接进行连接。
Let us now add tblCustomers table close that show table dialog box. We will try some concatenation with some simple named fields.
现在,让我们添加名和姓字段并运行查询。
Let us now add the first name and last name fields and run the query.
如你所见,名和姓被分成了两个不同的字段。
As you can see, the first name and the last name are separated into two different fields.
我们需要一起显示这些信息。
We need to display this information together.
回到 Design View ,并在第三个字段创建一个名为 full name 的新字段。现在,添加全名并输入将这两个字段连接在一起的表达式。
Go back to the Design View and in the third field create a new field called full name. Now, add the full name and type the expression that concatenates those two fields together.
让我们运行查询,然后你将看到一个新的计算字段。
Let us run the query and you will see a new calculated field.
它可以连接来自这两个字段的信息,但它并非完全按照我们希望的方式显示。它将所有文本运行在一起,因为它正在按照我们要求的方式执行。现在,我们将不得不要求 Access 在这两个字段的信息之间添加空格。
It can concatenate the information from these two fields together, but it doesn’t exactly appear the way we want it to. It runs all of that text together because it’s performing the way we have asked it to. We will now have to ask Access to add space in between the information from these two fields.
现在让我们回到设计视图并向该表达式添加另一个部分。
Let us now go back to the Design View and add another section to this expression.
在引号内添加一个空格和另一个和号。这使得 Access 取自名字段的信息;用空格将它添加,然后在最后添加姓字段的信息。现在,再次运行查询,然后你将看到以下结果。
Add a Space inside quotes and another ampersand. This makes Access take the information from that first name field; add it with a space and then add the information from the last name field at the very end. Now, run the query again and you will see the following results.
Ms Access - Summarizing Data
如果你只想要一个特定的数字,汇总查询非常棒,但是,如果你想要汇总信息,比如电子表格式汇总,你可能想要考虑尝试交叉表查询。当您想要重组汇总数据,以便更轻松地阅读和理解时,请考虑使用 Crosstab Query 。
Aggregate queries are great if you are looking just for one specific number, but if you want summarizing information like a spreadsheet-like summary you might want to consider trying a crosstab query. When you want to restructure summary data to make it easier to read and understand, consider using a Crosstab Query.
-
A crosstab query is a type of Select Query. When you run a crosstab query, the results get displayed in a datasheet. This datasheet has a different structure from the other types of datasheets.
-
The structure of a crosstab query can make it easier to read than a simple select query that displays the same data, as shown in the following screenshot.
迄今为止,创建 Access 交叉查询的最简单方法是直接使用 Access 自带的向导来创建交叉查询。现在我们转到“创建”选项卡,进入“查询”组,然后单击“查询向导”。
By far, the simplest way to create a crosstab query in Access is to simply use the wizard that comes with Access to create your crosstab query. Let us now go to the Create tab, in the queries groups and click on query wizard.
在上面的对话框中,可以看到各种特殊查询。你可以创建一个简单的查询向导,就像我们迄今为止一直从设计视图执行的操作一样。第二个查询向导是我们想要的——以紧凑型电子表格格式显示数据的交叉查询。现在,选择交叉查询,然后单击“确定”。
In the above dialog box, you can see all kinds of special queries. You can create a simple query wizard like how we have been doing so far from the Design View. The second one is the one we want — the crosstab query that displays data in a compact spreadsheet like format. Now, select the crosstab query and click Ok.
向导中的第一个屏幕会提示你的交叉查询结果中包含哪些字段的表或查询。单击“ Queries ”单选按钮,然后选择“ qryOrdersInformation ”——这是我们之前创建的包含小计、销售税等的表格。单击“ Next ”。
The first screen in the Wizard is going to prompt what table or query contains the fields that you want for your Crosstab Query results. Click on the Queries radio button and select qryOrdersInformation — this is the one that we created earlier which contain the subtotal, sales tax etc. Click Next.
现在我们将查看该查询中可用的字段。系统会提示你输入要作为行标题的字段值。假设我们希望按我们所有不同的书名生成一个简单列表。现在,选择书名字段,然后将其发送到所选字段区域,并单击“ Next ”。
We will now look into the available fields from that query. It will prompt you to enter the field values that you want as row headings. Let us say we want a simple list of all of our different book titles. Now select the book title fields and send that over to your selected field area and click Next.
在上面的对话框中,问题是你希望将什么用作列标题,这实际上取决于你的评估内容。假设你要查看我们的“ sales by date ”,选择“ TransactionDate ”,然后单击“ Next ”。
In the above dialog box, the question is what you want to use as column headings and that really depends on what you want to evaluate. Let us assume that you want to view our sales by date, choose TransactionDate and click Next.
由于我们选择了日期时间列,因此上面对话框中的问题是一个专门提出的问题,基于我们之前选择字段。它询问我们是否希望按特定的间隔对我们的日期时间列信息进行分组。选择年份选项,然后单击“ Next ”。
The question in the above dialog box is a specialized one based on the field we previously selected since we chose a date time column. It is asking if we want to group our date time column information by a specific interval. Select the year option and click Next.
上面对话框屏幕中的下一个问题询问你希望为行交集中的每一列计算什么数字。在这种情况下,我们可以按所售书籍的数量进行计算,选择“ quantity (QTY) ”,从函数中求和,然后单击“ Next ”。
The next question in the above dialog screen asks what number do you want calculated for each column in the row intersection. In this case, we can go by quantity of the books sold by selecting quantity (QTY) and Sum from the functions and click Next.
向导的最后一个屏幕将询问“ what do you want to name your query ”,然后单击“ Finish ”以“ View that query ”。
The very last screen of your wizard is going to ask what do you want to name your query and further, click Finish to View that query.
我们现在获得按书分类的信息以及总销售额等信息,例如每一销售额何时发生。
We now have book by book information and also information on the total sales like when each one of those sales occurred.
Ms Access - Joins
数据库是一个数据表集合,这些数据表允许相互进行逻辑关系。你可以使用关系通过表中公共的字段来连接表。一个表可以是任意数量的关系的一部分,但每个关系始终恰好有两个表。在查询中,关系由 Join 表示。
A database is a collection of tables of data that allow logical relationships to each other. You use relationships to connect tables by fields that they have in common. A table can be part of any number of relationships, but each relationship always has exactly two tables. In a query, a relationship is represented by a Join.
What is Join
联接指定如何在一个数据库中的两个或多个表中组合记录。从概念上来说,联接与表关系非常相似。实际上,联接对于查询就像关系对于表一样。
A join specifies how to combine records from two or more tables in a database. Conceptually, a join is very similar to a table relationship. In fact, joins are to queries what relationships are to tables.
以下是本章将讨论的两种基本类型的联接 −
The following are the two basic types of joins that we will be discussing in this chapter −
-
The inner join
-
The outer join
两者都可以轻松地从 queries design view 中创建。
Both of which can easily be created from a queries design view.
Inner Join
现在让我们了解 Inner Join −
Let us now understand Inner Join −
-
The most common type of join is an inner join which is also the default join type in Microsoft Access.
-
Inner Join will display only the rows or records where the joined fields from both tables are equal.
-
This join type looks at those common fields and the data contained within. It only displays the matches.
Outer Join
现在让我们了解 Outer Join 是什么 −
Let us now understand what an Outer Join is −
-
An outer join displays all rows from one table and only those rows or records from the other table where the joined fields are equal.
-
In other words, an outer join shows all rows from one table and only the corresponding rows from the other table.
还有其他联接类型 −
There are other Join types too −
Left Outer Join & Right Outer Join
让我们理解 Left Outer Join 和 Right Outer Join -
Let us now understand Left Outer Join and Right Outer Join −
-
You can choose the table that will display all rows.
-
You can create a Left Outer Join which will include all the rows from the first table.
-
You can create a Right Outer Join that will include all the rows from the second table.
让我们转到 Create 选项卡,从 Design View 创建一个查询。选择 tblProjects 和 tblTasks ,关闭“显示表格”对话框,如图所示。
Let us now go to the Create tab and create a query from the Design View. Select tblProjects and tblTasks and close the Show Table dialog box as in the following screenshot.
添加 tblProjects 中的 ProjectName 字段,以及 tblTasks 表格中的 TaskTitle, StartDate 和 DueDate 。
Add the ProjectName field from tblProjects, and TaskTitle, StartDate and DueDate from tblTasks table.
让我们运行这个查询。
Let us now run the query.
我们只显示了几个项目的记录。一些项目有很多与该项目相关联的任务,这些信息通过 ProjectID 字段关联。
We are only displaying records from a few projects. A couple of these projects have a lot of tasks associated with that project and this information is related through ProjectID field.
-
When we create this query in Microsoft Access, Access is taking this relationship from the relationships we have created.
-
By default, it is creating what’s known as an Inner Join between these two fields, between these two tables, and that is how it is relating this information together from these two tables.
-
It is showing us only the matches, so when we run this query, there are a lot of other projects listed in tblProjects that do not appear as part of our records set in this query, and that is because of how these two tables are joined together, through that Inner Join, which is again, that default Join for any query.
但是,如果你想改变关系,假设你想创建外连接,或者换句话说,显示 tblProjects 的所有项目,该表格中的所有记录以及 tblTasks 中的所有任务——打开连接属性;只需双击关系行即可。
However, if you want to alter the relationship, Let us say you want to create an outer join, or in other words, show all of the projects from tblProjects, every single record that is in that table, along with all of the tasks from tblTasks — Open join properties; we can do this simply by double-clicking on the relationship line.
Access将在连接属性对话框中显示左表格名称和右表格名称。
Access will display the left table name and the right table name in Join Properties dialog.
-
The left column name and the right column name and the first radio button is to only include rows where the join fields from both tables are equal and that is the inner join and that is what is selected by default when creating relationships, when creating a join in the query, but, you can change it.
-
We have two other options as well; we can include all records from tblProjects, and only those records from tblTasks where the joined fields are equal and this one is Left Outer Join.
-
We have a third option, include all records from tblTasks and only those records from tblProjects where the joined fields are equal and this one is Right Outer Join.
这些是你能从“设计视图”轻松创建的不同类型的连接。我们选择第二个选项,即左外连接,然后单击 Ok 。
Those are the different types of joins you can create easily from the Design View. Let us select the second option, which is Left Outer Join, and click Ok.
让我们看看其他步骤 -
Let us now look into the other steps −
当你查看关系行时,你会看到一个指向 tblTasks 的 ProjectID 的小箭头。当你运行此查询时,你将看到以下结果。
When you look at the relationship line, you will see a little arrow pointing towards ProjectID in tblTasks. When you run this query, you will see the following results.
如您所见,它向我们展示了每个项目的名称,无论它是否具有关联的任务。您还将看到许多空字段。所有这些都将为空,因为在这些字段来自的 tblTasks 中没有相关信息。现在,让我们再次转到 Design View ,然后双击关系线。
As you can see that it is showing us every single project name, whether or not it has a related task. You will also see a bunch of null fields. All of this will be blank because there is no related information in tblTasks, where these fields come from. Let us now go to the Design View again and double-click on the relationship line.
在“联接属性”对话框中,选择第三个选项,即右外联接,然后单击 Ok 。
In the Join Properties dialog box, select the third option which is for the Right Outer Join and click Ok.
现在,看看我们的关系线。您将看到一个箭头现在指向 tblProjects 中的 ProjectID 。当您运行此查询时,您将看到以下结果。
Now look at our relationship line. You will see that a little arrow is now pointing towards ProjectID in tblProjects. When you run this query you will see the following results.
Self-Joins
自联接是另一种类型的联接。自联接关联同一表内匹配的字段。例如,查看具有主管字段的员工表,该字段引用存储在同一表内另一个字段(员工 ID)中的同类数字。
Self-join is another type of Join. A Self-join relates matching fields from within the same table. For example, look at the employee’s table with a supervisor field, which references the same kind of number stored in another field within the same table — the employee ID.
如果我们想了解 Kaitlin Rasmussen 的主管是谁,我们将不得不获取存储在该主管字段中的号码并在完全相同的表内该员工 ID 字段中查找它,才能知道 Charity Hendricks 是主管。
If we wanted to know who Kaitlin Rasmussen’s supervisor is, we will have to take the number stored in that supervisor field and look it up within the exact same table in that employee ID field in order to know that Charity Hendricks is the supervisor.
此表不是关系数据库的理想结构,因为它未经过规范化。
This table is not the ideal structure for a relational database because it’s not normalized.
如果我们有一种情况,即我们想创建仅列出员工姓名及其主管姓名的查询,则除非我们创建自联接,否则我们无法轻松查询它。
If we have a situation where we want to create a query that just lists the employee names alongside the names of their supervisors, there is no easy way we can query that unless we create a Self-join.
要查看自联接,请使用以下字段创建一张表并输入一些数据。
To see a Self-join, create a table with the following fields and enter some data.
在这里,我们再次想创建一个包含员工名字和主管姓名的列表。让我们从 query design view 创建查询。
Here we want to create a list again with the first name of the employee and then the name of the supervisor. Let us create a query from the query design view.
现在,添加 tblEmployees 表。
Now, add tblEmployees tables.
关闭此对话框。
Close this dialog box.
现在,为我们的员工添加名字和姓氏。
Now, add the first name and last name for our employees.
我们现在需要一种方法在此表及其自身之间创建关系。为此,我们需要打开 the show table dialog box 并再次添加 tblEmployees。
We now need a way to create a relationship between this table and itself. To do that, we need to open the show table dialog box and add tblEmployees one more time.
我们在本查询视图中创建了相同表的另一份副本。现在,我们需要创建自联接。为此,单击 tblEmployees 表中的主管,按住鼠标按钮,将其直接拖到复制表 tblEmployees_1 中的 EmployeeID 上方。然后,从该复制表中添加名字和姓氏。
We have created another copy of the same table in this query view. Now, we need to create Self-join. To do that, click on Supervisor in tblEmployees table and hold the mouse button and drop it right on top of the EmployeeID in that copied table — tblEmployees_1. Then, add the first name and last name from that copied table.
现在让我们运行您的查询,您将看到以下结果。
Let us now run your query and you will see the following results.
它显示员工的名字和他们主管的名字。这是如何在 Microsoft Access 中创建自联接的方法。
It displays the names of the employees along side the names of their supervisors. And, this is how you create a Self-join in Microsoft Access.
Ms Access - Duplicates Query Wizard
在本教程中,我们创建了各种选择查询,主要来自设计视图。但是在 Access 中,我们可以使用一些特殊查询向导来创建一些特定的选择查询。第一个是查找重复查询向导。查找重复查询向导将创建一个查询,该查询可在单个表或查询中找到具有重复字段值的记录。
In this tutorial, we have created a variety of select queries, mainly from the Design View. But in Access, we can make use of some special query wizards to create a couple of specific select queries. The first one is Find Duplicates Query wizard. The Find Duplicates Query Wizard will create a query that locates records with duplicate field values in a single table or query.
正如我们所讨论的,关系数据库旨在避免存储重复的信息。但尽管有这样的设计,用户有时会无意输入重复的信息。
As we have discussed that relational databases are designed to avoid storing duplicate information. But despite that design, sometimes users accidentally enter duplicate information.
Example
在客户表中,你可以让同一客户无意被添加两次。在这种情况下,客户将具有相同的地址,但客户 ID 不同,这会给报告造成问题。在此情况下,你可以使用 duplicates query wizard to quickly locate possible duplicate entries 。
In a customer’s table, you can have the same customer accidentally added twice. In such cases, the customer will have the same address, but different customer IDs, which can create problems with reporting. In this situation, you can make use of the duplicates query wizard to quickly locate possible duplicate entries.
现在让我们打开包含 tblAuthers 表的 Access 数据库并在 Create 选项卡中,在查询组中,选择 query wizard 。
Let us now open our Access database which contains tblAuthers table and go the Create tab and, in the queries group, select query wizard.
如果你看到以下屏幕截图,则有四种不同的向导可供选择。选择 find duplicates query wizard 并单击 Ok 。
If you see the following screenshot, you have four different wizards to choose from. Select the find duplicates query wizard and click Ok.
查找重复查询向导的第一个屏幕会询问要搜索可能的重复项的哪个表或哪个查询。假设我们想要检查作者表以确保同一作者未无意输入两次。因此,选择 tblAuthors 并单击 Next 。
The very first screen of the find duplicates query wizard will ask what table or what query you want to search for possible duplicates. Let us say we want to check our author’s table to make sure that the same author hasn’t accidentally been entered twice. So, select tblAuthors and click Next.
-
The second screen in the wizard will ask what fields might contain duplicate information.
-
Typically, you will not be using your primary key field, because, again, when you designate a field in Access as a primary key, Access will not allow duplicates to be entered.
-
We will look at any other field or a combination of fields.
我们可以按姓或名和姓搜索,也可以按他们的街道地址搜索,也可以更具体地按他们的电话号码或生日搜索。
We can search by the last name or the first name and the last name, or you can search by their street address, or to be more specific with their telephone number or birthday.
我们现在按名、姓和生日搜索,然后点击 Next 。
Let us now search by the first name, the last name, and birthday and click Next.
该向导的以下屏幕将要求输入我们希望在查询中显示的字段。为此,点击双箭头,所有字段都将移至其他查询字段区域,并将会添加到我们的查询结果中。
The following screen in this wizard will ask for the fields we want to be displayed in our query. For this, hit the double arrow, all of the fields will move over to the additional query fields area, and will be added to our query results.
我们现在点击 Next 。它将带我们进入该查询向导的最后一个屏幕。
Let us now click Next. It will take us to the last screen in this query wizard.
在以下屏幕中,输入你希望如何命名查询。
In the following screen, enter how do you want to name your query.
默认情况下,它将为其命名 find duplicates 对 whatever the name of the object that you’re querying 。在这种情况下, tblAuthors ,但你也可以为其提供任意其他名称,然后点击 finish 。
By default, it’s going to name it find duplicates for plus whatever the name of the object that you’re querying. In this case, tblAuthors, but you can give it any other name too and click finish.
此处,Access 找到了一个可能的重复项,作者 Jose Caline,他有相同的生日、地址、电话号码但有不同的 AuthorID。
Here, Access has found a possible duplicate, and that’s going to be author Jose Caline which has same birthday, same address, same telephone number but different AuthorIDs.
这一定是无意间重复输入了两次。我们现在已经将所有字段添加到查询中,我们能直接删除记录。我们还必须确保在其他表中没有与此相关的记录。
This one has definitely been entered twice by accident. We have now added all of the fields to our query, we could just go and delete the record. We also have to make sure that we don’t have any related records in another table.
选择任何记录,然后在以下对话框中选择“删除”。
Select any record and choose Delete as in the following dialog box.
Access 会提示你“您即将删除一条记录。”如果你要继续,请点击“是”。
Access gives you a prompt, “You are about to delete one record.” Click Yes if you want to continue.
如果你想查看该向导创建该查询的方式,请进入设计视图,查看该向导向该查询添加了哪些内容。
If you’d like to see how that wizard has created this query, go into the Design View and see what all has been added to this query.
正如你在以上屏幕截图中所看到的,我们的字段及一些特定的条件在名字段的下方。
As you can see in the above screenshot, we have our fields and some specific criteria underneath the first name field.
这是该向导搜索重复信息的方式。这是迄今为止查找重复项最简单的方法。
This is how this wizard is looking for that duplicate information. It is by far the easiest method to find duplicates.
Ms Access - Unmatched Query Wizard
在 Access 中,还有另一个非常有用的向导,即 Find Unmatched Query Wizard 。查找不匹配查询向导创建一个查询,该查询查找表中没有另一个表中相关记录的记录或行。
In Access, there is another very useful wizard and that is Find Unmatched Query Wizard. The Find Unmatched Query Wizard creates a query that finds records or rows in one table that have no related records in another table.
正如我们先前讨论的那样,数据如何在查询中连接在一起,以及大多数查询如何查找两个或更多表之间的匹配项。
As we have already discussed how data joins together in queries, and how most queries are looking for the matches between two or more tables.
-
This is the default join in Access, for example, if we design a query with two tables, tblCustomers and tblOrders, and join those two tables by the CustomerIDs, this query will return only the results that match. In other words, the customers who have placed orders.
-
There are times when we don’t want to see the matches, for instance, we may not want to see any customer in our database — the customers who have not placed orders as yet.
-
This is exactly what the find unmatched query does.
此类查询还有许多其他可能的用途。
There are many other possible uses for this kind of query as well.
在我们的数据库中,我们可以使用它来查看哪些作者尚未编写项目,或者您可以使用它来查看哪些员工尚未选择任何健康福利。现在让我们打开包含 Customers 和 Orders 表的数据库;转到“创建”选项卡并单击查询向导按钮。
In our database, we can use it to see which authors have not yet written a project or you could use it to see which employees have not yet elected any health benefits. Let us now open your database which contains Customers and Orders table; go to the Create tab and click on the query wizard button.
选择 Find Unmatched Query Wizard 并单击 Ok 。
Select the Find Unmatched Query Wizard and click Ok.
在此场景中,我们将找出尚未下订单的客户。在第一个屏幕中,它询问包含查询结果记录的表或查询。
In this scenario, we will look out for those customers who have not placed an order. In the first screen it’s asking which table or query contains the records you want in the query results.
我们现在想要 tblCustomers 中的客户列表。选择该选项并单击 Next 。
We now want a list of customers from tblCustomers. Select that option and click Next.
在以下屏幕中,您需要指定 which table or query contains the related records 。换句话说,您使用哪个表与第一个表进行比较。为此,我们需要找到尚未下订单的那些项目。我们需要选择包含所有订单信息的表 tblOrders 。现在,单击 Next 。
In the following screen, you need to specify which table or query contains the related records. In other words, what table are you using to compare with the first one. For this, we need to find the ones that have not placed orders. We need to select the table that contains information on all orders — tblOrders. Now, click Next.
在以下屏幕中,您需要指定两张表中包含哪些信息。
In the following screen, you need to specify which piece of information is in both tables.
-
This will typically be some kind of primary key, foreign key, field, or relationship.
-
If you have an existing relationship in your database, Access will select and match those fields for you.
-
But, if you have other fields that you can join together, contain similar information, you can choose that here as well.
此处,我们在“tblCustomers”字段和“tblOrders”字段中默认选择了 CustID。现在,单击 Next 。
Here, we have CustID selected by default in both Fields in ‘tblCustomers’ and Fields in ‘tblOrders’. Now, click Next.
在以下屏幕中,您可以选择想要在查询结果中显示的字段。
In the following screen, you can pick and choose the fields you want to see displayed in the query results.
现在让我们选择所有可用字段并单击双箭头。这将把所有 available fields 移动到 selected fields 区域。现在,单击 Next 。
Let us now select all the available fields and click on the double-headed arrow. This moves all the available fields over to the selected fields area. Now, click Next.
最后一个屏幕将允许您为查询选择一个名称并单击 Finish 。
The last screen will allow you to choose a name for your query and click Finish.
这里我们列出了尚未向我们下订单的某个客户。
Here we have one customer listed as that customer who has not placed an order with us yet.
您还可以了解如何创建该查询。为此,您需要返回到 Design View 。
You can also see how that query was created. For this, you need to go back to the Design View.
此向导在 tblCustomer 和 tblOrders 之间创建了 Outer Join ,并且 Is Null 条件已添加到 tblORders 中的 CustID。这样做是为了排除某些记录。在本例中,这些记录是已下达订单或在 tblOrders 中有相关信息的客户。
This wizard has created an Outer Join between tblCustomer and tblOrders and the Is Null criteria is added to the CustID from tblORders. This is to exclude certain records. In this case, it is the customers who have placed orders, or who have related information in tblOrders.
Ms Access - Create Form
Access 中的表单就像商店中的展示柜,可以更容易地查看或获取你想要的产品。由于表单是你可以或其他用户可用于添加、编辑或显示存储在你 Access 桌面数据库中的数据的对象,因此你的表单设计是重要因素。你可以使用 Microsoft Access 中的表单进行许多方面的设计。你可以创建两种基本类型的表单 -
Forms in Access are like display cases in stores that make it easier to view or get the items that you want. Since forms are objects through which you or other users can add, edit, or display the data stored in your Access desktop database, the design of your form is an important aspect. There’s a lot you can do design-wise with forms in Microsoft Access. You can create two basic types of forms −
-
Bound forms
-
Unbound forms
Bound Forms
我们现在了解一下什么是 Bound Forms -
Let us now understand what Bound Forms are −
-
Bound forms are connected to some underlying data source such as a table, query, or SQL statement.
-
Bound forms are what people typically think of when they think of the purpose of a form.
-
Forms are to be filled out or used to enter or edit data in a database.
-
Examples of bound forms will typically be what users use to enter, view or edit data in a database.
Unbound Forms
让我们查看未绑定表单 -
Let us look into Unbound Forms −
-
These forms are not connected to an underlying record or data source.
-
Unbound forms could be dialog boxes, switch boards, or navigation forms.
-
In other words, unbound forms are typically used to navigate or interact with the database at large, as opposed to the data itself.
Types of Bound Forms
在 Access 中,您可以创建多种类型的绑定表单。我们来了解一下类型 −
There are many types of bound forms you can create in Access. Let us understand the types −
Single Item Form
这是最流行的一种,其中会显示记录——一次显示一条记录。
This is the most popular one and this is where the records are displayed — one record at a time.
Creating Forms
有几种方法可以在 Access 中创建表单。为此,打开您的数据库并转到 Create tab 。在右上角的“表单”组中,您会看到“表单向导”按钮。
There are a few methods you can use to create forms in Access. For this, open your Database and go to the Create tab. In the Forms group, in the upper right-hand corner you will see the Form Wizard button.
单击该按钮以启动表单向导。
Click on that button to launch the Form Wizard.
在此向导中的第一个屏幕上,您可以选择想在表单中显示的字段,并且可以选择从多个表或查询中的字段。
On this first screen in the wizard, you can select fields that you want to display on your form, and you can choose from fields from more than one table or a query.
我们假设我们只需要一个简单的表单,用于输入我们的员工信息。
Let us assume we want to simply have a quick form that we are going to use for data entry for our employee information.
从 Tables/Queries 下拉列表中,选择 tblEmployees 表。单击双箭头一次移动所有字段。
From Tables/Queries drop-down list, select tblEmployees table. Click on the double arrow to move all the fields at once.
我们只使用此表,然后单击 Next 。
Let us just leave it with that one table, and click Next.
表单向导中的下一个屏幕将询问我们希望表单采用何种布局。我们有 columnar, tabular, datasheet and justified 布局。我们在此处选择“列式”布局,然后单击 Next 。
The following screen in the Form Wizard will ask for the layout that we would like for our form. We have columnar, tabular, datasheet and justified layouts. We will choose the columnar layout here and then click Next.
在以下屏幕中,我们需要为表单指定一个标题。我们将其命名为 frmEmployees 。
In the following screen, we need to give a title for our form. Let us call it frmEmployees.
为表单指定标题后,您可以打开表单以查看表单的外观,或可以开始向表中输入信息。或者,您可以选择修改表单设计的选项。我们选择第一个选项 open the form to view or enter information ,然后单击 Finish 。
Once you have given your form a title, you can open the form to see what that form looks like, or you can begin entering information into your table. Or you can choose the option to modify the form’s design. Let us choose the first option to open the form to view or enter information and click Finish.
现在,请看以下屏幕截图。这就是您表单的外观。这是一个单项表单,这意味着一次显示一条记录,在下方您可以看到导航按钮,这告诉我们这是正在显示 9 条记录中的第 1 条。如果您单击该按钮,它将移动到下一条记录。
Now, take a look at the following screenshot. This is what your form looks like. This is a single item form, meaning one record is displayed at a time and further down you can see the navigation buttons, which is telling us that this is displaying the record 1 of 9. If you click on that button then, it will move to the next record.
如果您想跳转到该表单或该表中的最后一条记录,则可以使用位于该右箭头旁边的按钮,一个箭头后边带有一条线,那是最后一条记录按钮。如果您想添加新的员工信息,则转到这些记录的末尾,然后在 9 条记录后,您会看到一个空白表单,您可以在其中开始输入新员工的信息。
If you want to jump to the very last record in that form or that table, you can use the button right beside that right arrow, the arrow with a line after it, that’s the last record button. If you want to add new employee information, go to the end of this records and then after 9 records you will see a blank form where you can begin entering out the new employee’s information.
这是一个使用表单向导创建表单的示例。现在,让我们关闭此表单并转到“创建”选项卡。现在,我们将使用向导创建稍复杂的表单。单击“表单向导”,这次,我们将从几个不同的表中选择字段。
This is one example of how you can create a form using the Form Wizard. Let us now close this form and go to the Create tab. Now we will create a slightly more complicated form using Wizard. Click the Form Wizard and this time, we will choose fields from a couple of different tables.
在此表单向导中,让我们选择 tblProjects 用于 Tables/Queries ,并选择一些可用的字段,例如 ProjectID、ProjectName、ProjectStart 和 ProjectEnd。这些字段现在将移动到所选字段。
In this Form Wizard, let us choose tblProjects for Tables/Queries, and select a few Available Fields such as ProjectID, ProjectName, ProjectStart, and ProjectEnd. These fields will now move to Selected Fields.
现在,选择 tblTasks 以使用表/查询,并发送 TaskID、ProjectID、TaskTitle、StartDate、DueDate 和 PercentComplete。单击 Next 。
Now select tblTasks for Tables/Queries and send over the TaskID, ProjectID, TaskTitle, StartDate, DueDate and PercentComplete. Click Next.
在这里,我们希望从几个不同的对象中检索数据。我们还可以选择如何安排表单的选项。如果我们想要创建一个扁平的表单,则可以选择按 tblTasks 进行排列,这将创建那个单一的表单,所有字段都以扁平视图进行布局,如上所示。
Here, we want to retrieve data from a couple of different objects. We can also choose from options on how we want to arrange our form. If we want to create a flat form, we can choose to arrange by tblTasks, which will create that single form, with all the fields laid out in flat view as shown above.
但是,如果我们想要基于一对多的关系创建一个分层表单,则可以选择按tblProjects排列数据。
However, if we want to create a hierarchical form based on that one-to-many relationship, we can choose to arrange our data by tblProjects.
在上面的窗口中,我们有一个选项,可以为 tblTasks 包含 subform ,或者我们可以将其制作成一个链接的表单。此链接表单是 tblProjects 将具有一个按钮的位置,该按钮将启动过滤到我们在该基础项目表单中选择的项目的第二个表单。现在,让我们选择 Form with subform(s) ,然后单击 Next 。
In the above window, we have the option to include a subform for tblTasks, or we can make that a linked form. This linked form is where tblProjects will have a button that will launch that second form filtered to the project that we have selected in that underlying projects form. Let us now select the Form with subform(s), and then click Next.
在以下屏幕中,您可以为子表单选择一个布局。默认情况下选择数据表视图。数据表视图类似于表视图。现在,单击 Next 。
In the following screen, you can choose a layout for your subform. The Datasheet View gets selected by default. The Datasheet View is similar to Table View. Now, click Next.
在以下屏幕中,您需要为您的表单提供一个名称。输入您想要的名称并单击 Finish 。
In the following screen, you need to provide a name for your forms. Enter the name you want and click Finish.
Access 将为您提供表单的外观预览。在顶部,您可以在主表单上进行控制,它来自我们的 Projects 表。向下滚动,您将看到一个子表单。它就像一个表单中的表单。
Access will give you a preview of what your form looks like. On top, you have the controls on your main form, which is from our Projects table. As you go down, you will see a subform. It’s like a form within a form.
Multiple Item Form
您可能还希望创建特定类型的表单。为此,您可以单击 More Forms 下拉菜单。
You may also want to create a specific kind of form. For this, you can click on the More Forms drop-down menu.
从菜单中,您可以创建一个 Multiple Items 表单、一个 Datasheet 表单、一个 Split 表单,甚至一个 Modal Dialog 表单。这些通常是绑定表单;选择您想要绑定到该表单的对象。这不适用于模态对话框表单。
From the menu, you can create a Multiple Items form, a Datasheet form, a Split form, or even a Modal Dialog form. These are typically bound forms; select the object that you would like to be bound to that form. This does not apply to the Modal Dialog forms.
要创建这种类型的表单,您首先需要在导航窗格中选择对象。让我们在此处选择 tblEmployees 。
To create this type of form, you will need to select the object in navigation pane first. Let us select tblEmployees here.
通过单击 More Forms 和 Multiple Items 继续。
Proceed by clicking on More Forms and Multiple Items.
以上步骤将进一步创建一个多项表单,列出所有员工。
The above step will further create a Multiple Items form, listing out all the employees.
Split Form
这种类型的表单平均分为两半,垂直或水平。一半显示单个项目或记录,另一半显示来自基础数据源的多个记录的列表或数据表视图。
This type of form is divided in equal halves, either vertically or horizontally. One half displays a single item or record, and the other half displays a list or a datasheet view of multiple records from the underlying data source.
现在,让我们在导航窗格中选择 tblEmployees ,然后在 Create 标签中,从“更多表单”菜单中选择 Split Form 选项,您将看到其中一个表单纵向划分的表单。
Let us now select tblEmployees in the navigation pane and then on Create tab. Select Split Form option from More Forms menu and you will see the following form in which the form is divided vertically.
Ms Access - Modify Form
我们已经学习了几种创建简单数据输入表单的方法。虽然表单简化了数据输入进程;但这些可能无法满足您想要的其他目的。它们可能不会像您预期的那样引人注目或用户友好。
We have learnt several ways to create simple data entry forms. Although the forms ease the process of data entry; these may not serve other purposes that you would want. They may not be catchy or user-friendly for what you have intended.
-
Upon creating a form, the first thing you will probably want to do is resize or move the controls around.
-
You might also need to add a control or remove a control.
现在,我们将讨论如何轻松地修改表单。
We will now discuss how to modify your form in an easy way.
Controls
它仅仅描述了表单或报表中用于显示数据、执行操作或作装饰的物品的通用术语,比如线条。换句话说,控件就是任何放置在表单或报表上的内容。
It is merely a generic term used to describe any object on a form or report that displays data, performs actions or items used for decorations such as a line. In other words, a control is just about anything that is placed on a form or report.
以下可以看作是控件:
The following can be considered as controls −
-
Objects such as labels.
-
Unbound or bound text boxes that you can use to add or edit or even calculate an expression.
-
Command buttons that perform actions like Save, Open an e-mail or Print and these buttons are also known as controls.
-
It is just a generic term for any object on a form or report.
我们现在将了解有关使表单美观并了解如何编辑和修改表单的不同方面。让我们现在打开从 tnlEmployees 列出所有员工的多项表单。
We will now look at the different aspects of making a form presentable and understand how to edit and modify a form. Let us now open our Multiple Items form which lists out all employees from tnlEmployees.
正如你所看到的员工列表,但并不像非常用户友好的列表。我们的控件超出了大小。它们太大,间距分布开,没有提供非常有用的列表视图。
As you can see a list of employees, but doesn’t really look like a very user friendly list. Our controls are oversized. They are too big, spaced apart and do not provide a very useful list view.
如需编辑此表单上控件的外观,你可以使用两个表单视图。在主页选项卡中,单击 View 下拉菜单。
To edit the appearance of your controls on this form, you have two form views that you can use. In the Home tab, click the View drop-down.
你有布局视图或设计视图,并且这两个视图都用于编辑你的表单。默认情况下打开表单视图;这是你用来与基础数据源交互或编辑的视图。
You have the Layout View or the Design View, and both of these views are used to edit your form. The Form View opens up by default; this is the view you will use to interact with or edit the underlying data source.
若要编辑表单本身的外观,我们需要首先转到布局视图。
To edit the appearance of the form itself, we need to go to the Layout View first.
当你切换到布局视图时,你会看到一系列上下文选项卡出现。
When you switch to the Layout View, you will see a series of contextual tabs appear.
在 Access 的顶部,你会看到一个标记为 Form Layout Tools 的区域,其中有三个选项卡——设计选项卡、排列选项卡和格式选项卡,并且每个选项卡都有不同的选项,用于设置表单外观的格式。
At the top of Access, you will see an area marked Form Layout Tools with three tabs — Design tab, an Arrange tab, and a Format tab and each of these tabs have different options for formatting the look or appearance of the form.
当你使用鼠标并单击任意一个控件时,你会注意到 Access 会高亮显示表单的指定区域,并且该区域内的所有控件都被浅橙色阴影覆盖,而你选择的实际控件的阴影将比前一个更深或在单击处周围有一个更深的橙色边框。
When you take your mouse and click on any one of these controls, you will notice that Access will highlight a given area of that form and all controls within that area are shaded a light orange whereas the actual control that you select will be shaded darker than the previous one or have a darker orange border around where you click.
在这里,你可以通过单击并拖动鼠标来按你的需要调整控件的大小,以调整该控件的高度、宽度或两者。
Here you can resize your controls as you want by clicking and dragging your mouse to resize the height or width or both of that one control.
在这个特定表单上,当你调整任何一个控件的大小时,还会随之更改表单上其他控件的大小,这是因为这些控件是如何分组的。让我们现在使用鼠标的单击并拖动功能,按你的需要调整所有字段。
On this particular form, when you resize any single control, you also change the size of the rest of your controls on your form, this is because of how these controls are grouped. Let us now adjust all the fields the way you want by using the click and drag function of the mouse.
这是从此布局视图编辑表单中控件的高度和宽度的一种快捷方法。
That is one quick way of editing the height and width of controls in your form from this Layout View.
Themes
在 Access 中,可以通过使用内置主题、颜色和字体样式、自定义填充颜色和为交替行着色来设置表单的格式。让我们打开 frmEmployees 。
In Access, there are some basic ways to format your forms by using built-in themes, colors, and font styles, customizing fill colors and shading alternate rows. Let us now open frmEmployees.
Access 创建的表单是朴素简洁的。它们在顶部有一个蓝色栏,背景是白色的。
The forms that Access creates are plain and simple. They have a blue bar on the top and a white background.
如果你想了解如何美化这些表单,你可以在设计视图或布局视图中探索设计选项卡中主题区域中的一些选项。
If you want to see how else you can stylize these forms, you can go to the Design View or Layout View and explore some of the options you have on the Design tab in the Themes area.
如果你点击“主题”下拉画廊,将有许多预设好的主题可供你尝试。将鼠标悬停在任意主题上,你会看到随之改变的预览,如颜色、字体大小和实际使用的字体。要应用特定的样式,只需点击鼠标即可看到它的外观。
If you click on the Themes' drop-down gallery, you have many pre-created themes to try out from. Hovering your mouse over any one of them will give you a preview of changing things like colors and font sizes and the actual font used. To apply a particular style, simply click your mouse on it and you can see what that looks like.
如果你喜欢某个主题但想要更改颜色,你可以通过返回“设计”选项卡上的“主题”组并选择你喜欢的颜色来调整颜色。还可以创建自定义颜色以匹配你的公司颜色。
If you like the theme but you want to change the colors, you can adjust the colors by going back to the Themes group on the Design tab and choosing the color you like. You can also create custom colors to match your company’s colors.
同样,你还可以从一系列字体样式中进行选择。你可以从 Office 套件中预先创建的众多样式中选择一个,也可以自定义那些字体,选择特定的标题字体、正文字体,甚至创建自定义名称并保存该字体组。
Similarly, you also have a series of font styles to choose from. You can choose one from the many that come prebuilt with the Office Suite or you can customize those fonts, choosing a specific heading font, a body font and even creating a custom name for that font group and saving.
让我们返回到 frmEmployees 。在此表单中,你将看到每隔一行都会用浅灰色阴影填充。
Let us go back to frmEmployees. In this form, you will see that every alternate row is shaded light gray.
此格式选项称为 Alternate Row Color ,如果你想要在多表单中调整该选项,请转到 Design View 。
The formatting option is referred to as Alternate Row Color and if you want to adjust that in a multiple form, go to the Design View.
选择该详细信息部分,然后转到“格式”选项卡,在“背景”组中,你应该会看到 Alternate Row Color 的选项。你可以更改交替行的颜色。要查看这有什么效果,只需转到“表单视图”或“布局视图”。
Select that detail section and then go to the Format tab and in background group you should see an option for Alternate Row Color. You can change the colors for alternate rows. To see what that looks like, simply go to the Form View or the Layout View.
如果你完全不想要阴影,可以选择 No Color 作为你的 Alternate Row Color ,它类似于早期版本 Access 中的传统外观。
If you don’t want any shading at all, you can choose No Color as your Alternate Row Color and that is more the traditional look from earlier versions of Access.
Ms Access - Navigation Form
Access 包含一个导航控件,可以轻松地在数据库中的各种表单和报表之间切换。导航表单只是一种包含导航控件的表单。导航表单是任何桌面数据库的极佳补充。
Access includes a Navigation Control that makes it easy to switch between various forms and reports in your database. A navigation form is simply a form that contains a Navigation Control. Navigation forms are a great addition to any desktop database.
-
Microsoft Access offers several features for controlling how users navigate the database.
-
We have already used navigation pane to navigate through all of the Access objects we create, such as forms, queries, tables, etc.
-
If you want to create your own navigational structure and make it easier for the users to find the specific objects that they really need, you can build navigation form, which is a form that uses a navigation control so users can use or view forms and reports right from within that main navigation form.
Example
现在我们来看一个简单的示例,其中我们将创建一个导航表单。为此,请转到“创建”选项卡;在“表单”组中,你将看到此导航下拉菜单。
Let us now take a simple example in which we will create the navigation form. For this, go to the Create tab; in the Forms group, you will see this navigation drop-down menu.
在此菜单中,你将看到用于排列你想要嵌入到此导航表单中表单和报表的不同布局。
In this menu, you will see different layouts for how to arrange your forms and reports that you would like to embed on this navigation form.
-
There is one with horizontal tabs, one with vertical tabs — where all the tabs are aligned to the left, a vertical tabs layout where all the tabs are aligned to the right.
-
There is a horizontal tabs layout that has two levels to it, so if you have a lot of objects that you want to display across the top, you can make use of this.
-
You can have one where you have both horizontal tabs and vertical tabs, either aligned to the left or to the right.
在以下示例中,我们将使用水平选项卡和垂直选项卡。要创建该布局或该导航表单,只需单击它,Access 就会创建一个不限表单,其中包含一个导航控件。
In the following example, we will be using Horizontal Tabs and Vertical Tabs. To create that layout or that navigation form, simply click on it, and Access will create an unbound form, with a navigation control on it.
向此导航表单添加对象的最简单方法是通过你的布局视图,只需将对象拖放至你希望它们显示的位置即可。
To add objects to this navigation form, the easiest way to do is through your layout view, by simply dragging and dropping objects to where you want them to appear.
现在让我们从导航窗格中拖动 frmProjects 表单并将其放到顶部的 [添加新项] 上。
Let us now drag frmProjects form from the navigation pane and drop it on the [Add New] on the top.
类似地,从导航窗格拖动 frmAuthers 表单,并将其放置在“新建”按钮的左侧。
Similarly, drag frmAuthers form from the navigation pane and drop it to the left of the Add New Button.
让我们现在添加其他与项目相关的表单,如 frmSubTasks、frmCurrentProjects 等。
Let us now add other forms related to Projects such as *frmSubTasks, frmCurrentProjects *etc.
现在让我们在顶部添加其他的标签。我们先添加 frmEmployees 表单。
Let us now add additional tabs across the top. We will first add the frmEmployees form.
现在,您将看到您的其他项目按钮已经从左侧消失了,这是因为它们附加到了项目标签。您在左侧看到的所有按钮都链接到您在顶部所选择的任何内容。现在,在选择“员工”标签后,让我们将与员工相关的信息拖动到左侧。
Now, you will see that your other project buttons have disappeared from the left and that is because they are attached to Project tab. All the buttons you view on the left are linked to whatever you have selected up the top. Now with the Employee tab selected, let us drag employees-related information to the left.
现在一个标签上是我们项目信息,另一个标签上是员工信息。类似地,您可以根据要求添加更多的标签。正如您所看到的那样,这些标签的名称不恰当,所以让我们开始重命名其中一些标签,以便使其更加用户友好。最简单的方法是双击左侧的任意标签或任意按钮,然后按如以下屏幕截图中所示重命名。
Now we have project information on one tab, employee information on the other. Similarly, you can add more tabs as per your requirements. As you can see that the name of the tabs is not appropriate, so let us start renaming some of these tabs to make them more user-friendly. The easiest way is to double-click on any tab or any button on the left and rename it as shown in the following screenshot.
Ms Access - Combo BOx
在任意表单中输入数据时,从列表中选择一个值可能会比记住要输入的值更快、更方便。选项列表还可以帮助确保输入字段中的值是合适的。列表控件可以连接到现有数据,或者可以在创建控件时显示您输入的固定值。在本章中,我们将介绍如何在 Access 中创建组合框。
When you enter data in any form, it can be quicker and easier to select a value from a list than to remember a value to type. A list of choices also helps ensure that the value entered in a field is appropriate. A list control can connect to existing data, or it can display fixed values that you enter when you create the control. In this chapter, we will cover how to create a combo box in Access.
Combo Box
组合框是一个对象或控件,其中包含一个用户可以选择值的下拉列表。
A combo box is an object or control which contains a drop-down list of values that the user can select from.
-
The combo box control provides a more compact way to present a list of choices.
-
The list is hidden until you click the drop-down arrow.
-
A combo box also gives you the ability to enter a value that is not in the list.
-
In this way, the combo box control combines the features of a text box and a list box.
Example
现在让我们举一个创建组合框的简单示例。我们已经创建了一个员工表单,如下面的屏幕截图所示。
Let us now take a simple example of creating a combo box. We have created a form for an employee as shown in the following screenshot.
我们现在想要为电话类型创建组合框,因为我们知道电话类型应该是 Home, Cell 或 Work 。此信息应该在下拉列表中可用,并且用户无需键入此信息。
We now want to create a combo box for Phone type because we know that phone type should be either Home, Cell or Work. This information should be available in the dropdown list and the user need not type this information.
现在让我们转到此表单的 Design View 。选择 Phone Type 字段并按 delete 键。
Let us now go to the Design View for this form. Select the Phone Type field and press delete.
删除电话类型字段后,转到“设计”标签。
Once the Phone Type field is deleted, go to the Design tab.
现在,让我们从控件菜单中选择 Use Control Wizards 选项,然后从菜单中选择组合框控件,如下面的屏幕截图所示。
Let us now select Use Control Wizards option from the Controls menu and then Select the Combo Box Control from the menu as shown in the following screenshot.
现在,在您想要的位置绘制组合框,当您松开鼠标时,您将看到组合框向导对话框。
Now, draw the combo box where you want and when you release your mouse then you will see the Combo Box Wizard dialog box.
在这里,您有不同的数据选项;让我们选择第 2 个选项,在此选项中,我们将添加值并单击下一步。
Here you have different option for data; let us select the 2nd option wherein, we will add the values and click Next.
输入您想要在下拉列表中显示的值,然后单击 Next 。
Enter the values you want to be displayed in the drop-down list and click Next.
再次单击 Next 。
Click Next again.
在组合框中输入 label ,然后单击 Finish 。
Enter the label for your combo box and click Finish.
可以看出,已创建组合框,但其未对齐至其他字段。我们先全选所有字段,然后转至“排列”选项卡。
You can see that combo box is created but it is not aligned to other field. Let us do that first by selecting all fields and then go to the Arrange tab.
左侧将看到 Stacked 选项。单击该按钮。
To the left, you will see the Stacked option. Click this button.
现在可以看到,这些字段已对齐。
You can now see the fields are aligned.
为了使每个字段的大小一致,我们可以使用不同的选项,让我们单击“大小/空格”按钮。
To make the size of each field same we have different options, let us click the Size/Space button.
在下拉列表中,单击 To Shortest 。
From the drop-down list, click To Shortest.
现在转至 Form 视图。
Now go to the Form view.
用户现在可以选择电话类型的任何选项。
A user can now easily select any option for the Phone type.
Ms Access - SQL View
在本教程中,我们将介绍 SQL 视图。无论何时在查询设计器中创建一个查询,Access 都会自动为你创建 SQL 查询。这实际上是从表中检索数据。如需了解在查询设计器中创建查询时你的查询如何以 sql 创建,让我们打开你的数据库。
In this chapter, we will be covering the SQL view. Whenever you create a query in query design, Access automatically creates the SQL query for you. This actually retrieves data from the tables. To see how your query is created in sql when you create it in query design, let us open your database.
从 Create 选项卡中选择 Query Design ,并添加 tblEmployees 表。
Select the Query Design from the Create tab and add the tblEmployees table.
选择要作为查询结果的字段,然后运行查询。
Select the field you want to see as query result and then run your query.
现在可以将所有员工信息作为查询结果查看。已在查询网格中选择特定字段;与此同时,MS Access 还使用从查询网格中获得的结果创建了 SQL 查询。
You can now see all the employee information as query result. You have selected certain fields in the Query Grid; at the same time, MS Access has also created an SQL Query with the results obtained from your Query Grid.
若要查看 SQL,请转至“开始”选项卡。从“视图”菜单中选择 SQL View ,你将看到该查询的 SQL。
To view the SQL, go to the Home tab. Select SQL View from the View menu and you will see the SQL of your query.
Example
以下是另一个示例,其中我们将看到正在进行的项目。
The following is another example wherein, we will see the projects in progress.
现在让我们运行查询。
Let us now run your query.
若要查看 SQL,请从“视图”菜单中选择“SQL 视图”。
To see the SQL, select the SQL View from the View menu.
可以看到 Access 自动生成的 SQL 查询。这有助于从两个表中检索数据。
You can see the SQL query which is generated by Access automatically. This helps retrieve data from two tables.
Ms Access - Formatting
Access 中的一个特别有用的格式化工具是能够应用 Conditional Formatting 以突出显示特定数据。让我们做一个条件格式的简单示例。
One especially useful formatting tool in Access is the ability to apply Conditional Formatting to highlight specific data. Let us take a simple example of conditional formatting.
Example
在这个示例中,我们将在数据库中使用表单 fSubCurrentProjects 。
In this example, we will be using a form fSubCurrentProjects in our database.
我们有这个数据库中所有项目的清单,我们还有一些新字段,如 On Time Status 和 Number of Late Tasks 。此表单是通过另一个查询创建的。
We have a list of all of the projects in this database and we have also got a couple of new fields like the On Time Status and the Number of Late Tasks. This form is created from another query.
在此查询中,我们有一个表与查询之间的联接,查询将显示到期日期计数或有多少项目有逾期任务。我们这里还有一个计算字段,它使用 IF 函数来确定到期日期的计数是否大于零。如果项目已逾期,它将显示单词 Late ;如果特定项目没有逾期任务,它将显示 On Time 。
In this query, we have a join between a table and a query that will display the count of due dates or how many projects have tasks that are overdue. We also have a calculated field here that uses the IF function to determine whether or not the count of the due date is greater than zero. It will then display the words Late if the project is late or On Time if that specific project does not have any overdue tasks.
Example 1
在此示例中,我们将使用上面的表单来了解如何使用条件格式突出显示特定信息。我们现在将突出显示当前逾期的每个项目。若要将条件格式应用于一个字段或多个字段,我们需要切换到“布局”视图。
In this example, we will be using the above form to understand how you can use Conditional Formatting to highlight specific pieces of information. We will now highlight every single project that is currently running late. To apply Conditional Formatting to one field or more than one field, we will need to switch over to the Layout view.
现在,选择 On Time Status 字段。
Now, select the On Time Status field.
现在,转到“格式”选项卡。
Now, go to the Format tab.
在此“格式”选项卡上,您应看到一个名为“控件格式”的组和一个用于“条件格式”的特殊按钮。现在,我们单击“条件格式”。
On that Format tab, you should see a group called Control Formatting and a special button for Conditional Formatting. Let us now click on Conditional Formatting.
现在,您将看到一个条件格式规则管理器,目前我们未对此控件应用任何规则。现在,通过单击“新建规则”按钮来创建新规则。
You will now see a Conditional Formatting Rules Manager and currently we have no rules applied to this control. Let us now create a new rule by clicking on the New Rule button.
现在,您将看到一个“新建格式规则”对话框。我们首先将指定将要创建的规则类型,这里有两个选项。第一个选项是 check the values in the current record or to use an expression ,第二个选项是 compare this record with the other records 。
You will now see a New Formatting Rule dialog box. We will first specify the type of rule we will be creating and here we have two options. The first option is to check the values in the current record or to use an expression, and the second option is to compare this record with the other records.
我们现在表单中只有两个值之一;“准时”或单词“逾期”,并且这些来自给定查询。现在,让我们从第一个组合框中选择“ Field Value Is ”,然后从第二个组合框中选择“ equal to ”。现在,在引号中输入单词“ Late ”。
We now have only one of two values in our form; either On Time or the word Late and that is from the given query. Let us now select the “Field Value Is” from the first combo box and then select “equal to” from the second combo box. Now, type the word “Late” in quotation marks.
我们现在可以设置我们的条件格式,如果单词 Late 出现在该字段中,我们希望此字段的外观如何。现在,让我们将字体颜色更改为红色,并使其 bold, italic 且 underline ,这就是我们的条件规则。现在,让我们单击 Ok ,再单击 Apply 及 Ok 。
We can now set our Conditional Formatting, how we want this field to look like if the word Late appears in that field. Let us now change the font color to red and make it bold, italic and underline, and that’s our conditional rule. Let us now click Ok and then click Apply, and Ok again.
您可以看到单词 Late 已格式化。这是一个创建非常基本的条件格式规则的示例。
You can see that the word Late is formatted now. This is one example of how to create a very basic conditional format rule.
Example 2
让我们以另一个示例说明。此处,我们将使项目的标题或名称 red 且 bold, italic 且 underline 。在您的表单上选择项目名称控件。
Let us take another example. Here, we will make the title or the name of the project red and bold, italic and underline. Select the project name control on your form.
我们现在将返回我们的“格式”选项卡并单击“条件格式”,并为特定控件创建新规则,如上方屏幕截图所示。
We will now go back to our Format tab and click on Conditional Formatting and create a new rule for that specific control as shown in the above screenshot.
此处,我们将不会检查已选择的当前字段的值,而是将其与此表单上的另一个字段进行比较。在第一个组合框中选择 Expression Is ,然后单击如上方屏幕截图中所示的末尾的 … 按钮。
Here, we will not be checking the value of the current field we have selected, but we will be checking it against another field on this form. Select Expression Is in the first combo box and then click on … button at the end as in the above screenshot.
在“表达式类别”中,您具有此表单中的每个对象。双击 CountofDueDate 。这会将对控件或字段的引用发送到我们的表达式生成器,条件为大于零。现在,单击“确定”。
In the Expression Categories, you have every single object that is on this form. Doubleclick on CountofDueDate. This will send the reference to that control or that field up to our expression builder and condition if it is greater than zero. Now, click Ok.
现在,让我们单击“确定”,再单击“应用”并再次单击“确定”。
Let us now click Ok and then, click Apply and Ok again.
这是基于另一个字段中的值的字段上条件格式的示例。
That was an example of Conditional Formatting on a field based on values in another field.
Example 3
我们现在来看看条件格式的另一个示例。假设我们希望查看哪些项目比其他逾期项目更加逾期或有更多逾期任务。选择“条件格式”选项。
Let us now look at another example of conditional formatting. Let us assume, we want to see which projects are more late or have more late tasks than other late projects. Select the Conditional Formatting option.
单击新建规则按钮以创建新规则,然后单击确定,如上图所示。
Click on the New Rule button to create a new rule and then click Ok as in the above screenshot.
在新建格式规则中,我们现在将选择规则类型“ Compare to other records ”。我们进一步更改 Bar color to red 。我们希望最短条形图表示最小值,最长条形图表示最大值。现在单击确定,然后再次单击应用和确定。
In the New Formatting Rule, we will now select a rule type “Compare to other records”. Let us further change the Bar color to red. We want our shortest bar to represent the lowest value and the longest bar to represent the highest value. Let us now click Ok and then, click Apply and Ok again.
你现在可以看到条件性阴影已应用,如上图所示。现在让我们转到表单视图。
You can now see Conditional Shading applied as in the above screenshot. Let us now go to the Form view.
Ms Access - Controls & Properties
在本章中,我们将介绍 Access 中可用的 Controls and Properties 。我们还将学习如何向表单添加控件。控件是表单或报告的组成部分,您使用这些控件可以 enter, edit, 或 display 数据。使用控件,您可以查看和处理数据库应用程序中的数据。
In this chapter, we will be covering Controls and Properties available in Access. We will also learn how to add controls to forms. Controls are the parts of a form or report that you use to enter, edit, or display data. Controls let you view and work with data in your database application.
-
The most frequently used control is the text box, but other controls include command buttons, labels, check boxes, and subform/subreport controls.
-
There are different kinds and types of controls you can create, but all of which will fall into one of the two categories — bound or unbound.
Bound Controls
我们现在来了解一下什么叫绑定控件。
Let us now understand what Bound Controls are −
-
Bound controls are ones that are tied to a specific data source within your database such as a field and a table or a query.
-
Values can be either text, dates, number, check boxes, pictures or even graphs.
-
You use bound controls to display values that come from fields in your database.
Unbound Controls
我们现在来了解一下什么叫未绑定控件。
Let us now understand what Unbound Controls are −
-
Unbound controls on the other hand are not tied to a data source, and they exist only in the form itself.
-
These can be text, pictures or shapes such as lines or rectangles.
Control Types
您可以在 Access 中创建不同类型的控件。这里,我们将讨论一些常见的控件,如文本框、标签、按钮标签控件等。
You can create different types of controls in Access. Here, we will discuss a few common ones such as Text box, Label, Button Tab Controls etc.
Text Box
-
Typically, anything that is in a text box will be bound, but not always.
-
You can use these controls to interact with the data stored in your database, but you can also have unbound text boxes.
-
Calculated controls will perform some kind of calculation based on an expression that you write and that data is not stored anywhere in your database.
-
It is calculated on the fly and live just on that one form.
Labels
-
Labels will always be text and unbound.
-
Normally, labels are not connected to any source in your database.
-
Labels are used to label other controls on your form such as text boxes.
Button
-
This is another type of popular control; these command buttons usually perform a macro or module.
-
Buttons are usually used to interact with the data or objects within your database.
Tab Controls
-
Tab controls give you a tabbed view of controls or other controls in your form.
-
Adding tabs to a form can make it more organized and easy to use, especially if the form contains many controls.
-
By placing related controls on separate pages of the tab control, you can reduce clutter and ease your work with data.
Hyperlink
-
Hyperlink creates a hyperlink on your form to something else. It can either be a web page or even another object or place within your database.
Some Other Controls
现在让我们来看看其他一些控件−
Let us now look into some other controls −
-
You can also create a web browser control and navigation control, groups, page breaks, combo boxes.
-
You can create charts, lines, toggle buttons, list boxes, rectangles, check boxes, unbound object frames, attachments, option buttons, subforms and subreports, bound object frames and even place images on your form.
Example
现在让我们通过创建一个新的空白表单,看看其中一些控件的简单示例。转至 Create 组中的 forms 选项卡,然后单击 Blank Form 。
Let us now look at a simple example of some of these controls by creating a new blank form. Go to the Create tab in the forms group and click on Blank Form.
上述步骤将打开一个未绑定的表单,该表单尚未附加到我们数据库中的任何项目。
The above step will open an unbound form, which is not attached yet to any item in our database.
默认情况下,它将以布局视图打开,如上图所示。
By default, it will open in layout view as shown in the above screenshot.
现在让我们转到“设计”视图以添加字段。
Let us now go to the Design View to add fields.
在“设计”选项卡上,单击“属性表”。
On the Design tab, click on the Property Sheet.
在“属性表”中,单击下拉箭头并确保选择了“表单”,然后转到“数据”选项卡。
On the Property Sheet, click on the drop-down arrow and make sure Form is selected, and then go to the Data tab.
在“数据”选项卡上,您将看到记录源仍然为空白。让我们假设我们想要创建一个将绑定到我们数据库中两个不同表的表单。现在单击…按钮。它会进一步打开其自己的查询生成器。
On the Data tab, you will see that the Record Source remains blank. Let us assume, we want to create a form that’s going to be tied to two different tables in our database. Now click on … button. It will further open its own query builder.
选择包含您要显示的数据的表;单击“添加”按钮,然后关闭此对话框。
Select the tables that contain the data you want to display; click on the Add button and then close this dialog box.
现在,让我们从 tblEmployees 中选择所有字段并将其拖动到查询网格中,并以类似的方式从 tblHRData 中添加所有字段。
Let us now select all the fields from tblEmployees and drag to query grid, and similarly add all the fields from tblHRData.
现在,单击“另存为”并为该查询命名。
Now, click Save As and give this query a name.
我们称之为 qryEmployeesData 并单击 Ok ,然后关闭查询生成器。
Let us call it qryEmployeesData and click Ok and then close the query builder.
您现在可以看到该查询作为其记录源。我们现在已将此表单绑定到我们的数据库中的一个对象,在本例中是 qryEmployeesData。我们现在可以开始向此表单添加一些控件,要添加任何一个控件,请转到“设计”选项卡并从“控件”组中查看您的选项。
You can now see the query as its Record Source. We have now bound this form to an object in our database, in this case qryEmployeesData. We can now start by adding some controls to this form and to add any one of the controls, go to the Design tab and view your options from the controls group.
在控件菜单中,你将看到“使用控件向导”已高亮,如上图所示。默认情况下,这个小按钮周围有高亮框。这意味着控件向导已打开。这就像一个切换开关。当你点击切换开关,向导就会关闭。再次点击它,将再次打开向导。
From the Controls menu, you will see that the Use Control Wizard is highlighted as in the above screenshot. This little button has that highlighted box around it by default. This means that the control wizards are turned on. This is like a toggle switch. When you click on the toggle switch the wizards turn off. Clicking it again will turn the wizards on.
现在让我们点击标签,拖动此标签,输入“员工信息”,然后转到“格式”选项卡,按以下屏幕截图进行格式化。
Let us now click on the Label and drag this label and enter Employee Information and then go to the Format tab to format it as in the following screenshot.
你可以选择应用粗体样式或更改该标签内文本的字体大小等。此控件显示在表单的详细信息部分。最好将此标签放在表单页眉部分中,该部分暂时不可见。
You can choose to apply a bold style or change the font size of text inside that label etc. This control appears inside the detail section of your form. It makes more sense to position this label inside the form header section, which is not visible yet.
右键单击表单背景的任意位置,并选择 Form Header ,如上图所示。
Right click anywhere in the background of that form and choose Form Header as in the above screenshot.
将此控件移动到表单页眉区域。现在让我们从“设计”选项卡创建更多控件。此时,假设我们要将所有字段分发到两个不同的选项卡中。
Move this control into the Form Header area. Let us now create some other controls from the Design tab. At this point, Let us say, we want to distribute all our fields into two different tabs.
在控件菜单中,你可以看到“选项卡控件”,将创建表单上的选项卡。单击选项卡控件,并在详细信息部分中将其绘制到表单上,如上图所示。
From the control menu, you can see the Tab Control which will create tabs on your form. Click on the tab control and draw it on your form in the detail section as in the above screenshot.
它将创建两个选项卡 - 页面 2 和页面 3,如上图所示。
It will create two tabs — page 2 and page 3 as in the above screenshot.
现在让我们开始向此选项卡控件添加一些控件。
Let us now start by adding some controls to this tab control.
如果我们想查看基本查询中存储的一些信息,请从“工具”组中的功能区单击 Add Existing Fields 选项,以查看基本查询中的所有可用字段。
If we want to view some information that’s stored in our underlying query, click on the Add Existing Fields option from the Ribbon in Tools group to view all the available fields from our underlying query.
若要将现有字段添加到页面中,请如以下屏幕截图所示,选择“字段列表”中可用的所有字段。
To bring the existing fields to the page, select all the fields available in the Field List as in the following screenshot.
现在,将字段拖动到选项卡控件的 Page2,如以下屏幕截图所示。
Now, drag the fields to Page2 of the tab control as in the following screenshot.
现在,转到“排列”选项卡并选择 Stacked 布局。
Now, go the Arrange tab and select the Stacked layout.
在第二页,现在让我们从此查询添加剩余字段。
On the second page, let us now add the remaining fields from this query.
你可以浏览并调整这些控件的大小和宽度,以使其按你希望的方式显示。
You can go through and make any adjustments to the size and width of these controls to get it looking the way you like.
完成格式化后,单击 Save As 图标,使用你希望的名称保存表单。
Once you are done with formatting click on the Save As icon to save the form with the name you want.
单击“确定”,然后转到“表单”视图,以查看该表单中的所有信息。
Click Ok and go to the Form view to view all the information in that form.
选项卡控件将我们的控件或字段分成两个屏幕,以便更轻松地查看和添加信息。
The tab control is breaking up our controls or our fields into two screens to make viewing and adding information easier.
Ms Access - Reports Basics
在本章中,我们将介绍报表的原理以及如何创建报表。报表提供一种查看、格式化和整理 Microsoft Access 数据库中的信息的方式。例如,你可以为所有联系人创建电话号码的简单报表。
In this chapter, we will be covering the basics of reports and how to create reports. Reports offer a way to view, format, and summarize the information in your Microsoft Access database. For example, you can create a simple report of phone numbers for all your contacts.
-
A report consists of information that is pulled from tables or queries, as well as information that is stored with the report design, such as labels, headings, and graphics.
-
The tables or queries that provide the underlying data are also known as the report’s record source.
-
If the fields that you want to include all exist in a single table, use that table as the record source.
-
If the fields are contained in more than one table, you need to use one or more queries as the record source.
Example
现在,我们使用一个简单的示例来了解创建非常简单的报表的流程。为此,我们需要转到“创建”选项卡。
We will now take a simple example to understand the process of creating a very simple report. For this, we need to go to the Create tab.
在单击“报表”按钮创建基本报表之前,请确保选择正确的查询。在这种情况下,你的导航窗格中已选择“ qryCurrentProjects ”。现在单击“报表”按钮,该按钮会根据该查询生成一份报表。
Before clicking on the Report button to create a basic report, make sure the proper query is selected. In this case, qryCurrentProjects is selected in your navigation pane. Now click on the Report button, which will generate a report based on that query.
你将看到报表在版式视图中打开。这提供了一种快速方式来调整你在报表中看到的所有字段的大小或宽度。现在我们调整列宽,以便以更好的方式使所有内容都适合放置。
You will see that the report is open in Layout view. This provides a quick way to adjust the size or width of any of your fields that you see on the report. Let us now adjust the column widths to make everything fit in a better way.
向下滚动并调整底部的页面控件。
Scroll down and adjust the page control at the bottom.
这是创建非常简单的报表的一种非常快的方法。你还可以从报表设计视图进行轻微的更改和调整。
This was a very quick way to create a very simple report. You could also make minor changes and adjustments from the report design view.
-
Just like forms, a report is made up of a variety of different sections.
-
You have the detail section, which is where all of your data lives for the most part.
-
You also will see a page header and a page footer section; these appear at the top and at the bottom of every single page in your report.
现在让我们更改报表标题并赋予它另一个名称。
Let us now change the Title of the report and give it another name.
单击保存图标保存报表。
Click on the save icon to save your report.
您将看到上面的对话框。
You will get the above dialog box.
为报表输入名称并单击“确定”。如果您想查看此报表实际的样子,在打印预览中,您可以返回到“视图”按钮并单击“打印预览”查看此报表在纸张或 PDF 上打印时的样子。
Enter a name for your report and click Ok. If you want to view what this report will actually look like, in Print Preview, you can go back to the View button and click on Print Preview to see what this report would look like when printed either on paper or as a PDF.
使用右下角的工具,您可以放大或缩小。当您切换到“打印预览”时,还可以在“打印预览”选项卡上看到一些按钮,这些按钮会自动显示。在缩放部分,您可以查看一页、两页;或者如果您有较长的报表,您可以一次查看四页、八页或十二页。您还可以调整一些简单内容,例如打印要使用的纸张大小、报表页边距、方向、列数、页面设置等。这就是使用“创建”选项卡上的“报表”按钮创建非常快速简单的报表的方式。
Using the tools on the lower right-hand corner, you can zoom in or zoom out. You also have some buttons on the Print Preview tab that appear automatically when you switch to Print Preview. In the zoom section, you’ve got a view for one page, two pages; or if you have a longer report, you can view four pages at once, eight pages or twelve pages. You can also adjust simple things such as the size of the paper that you are using to print, the margins for your report, the orientation, the number of columns, page set up, etc. And that is how you can create a very quick simple report using the Report button on the Create tab.
Create a Report Using Report Design
报表设计是使用 Access 创建快速报表的另一种方法。为此,我们需要使用报表设计视图按钮,它类似于窗体设计按钮。这将创建一个空白报表并直接将其打开到设计视图,这使您可以更改控件源并直接将字段添加到报表的设计视图。
Report Design is another method for creating a quick report in Access. For this, we need to use the Report Design View button, which is like the Form Design button. This will create a blank report and open it directly to the Design View, allowing you to change the control source and add fields directly to the Design View of the report.
现在,让我们转到“创建”选项卡并单击“报表设计”按钮。
Let us now go to the Create tab and click on the Report Design button.
它将打开一个空白报表或一个未绑定的报表,这意味着此报表未连接到我们的数据库中的任何其他对象。
It will open a blank report or an unbound report, meaning this report is connected to no other object in our database.
在“工具”组中的“设计”选项卡上,选择“属性表”。这将打开属性窗格。
On the Design tab in the Tools group, select the Property Sheet. This will open up the Property pane.
在“数据”选项卡上,为该报表分配一个记录源,以将其连接到数据库对象,如下面的屏幕截图所示。
On the Data tab, assign a record source to this report, to connect it to a database object as in the following screenshot.
从下拉菜单中选择 qryLateProjects ,现在,下一步是单击“设计”选项卡上的“添加现有字段列表”按钮来添加一些字段到此报表。
Select qryLateProjects from the drop-down and now, the next step is to go through and add some fields to this report by clicking on Add Existing Fields list button on the Design tab.
选择字段,如上面的屏幕截图所示。
Select the fields as in the above screenshot.
将字段拖动到报表中,如上面的屏幕截图所示。转至“排列”选项卡,然后在“表”组中,您可以从几个选项中进行选择。
Drag the fields to you report as in the above screenshot. Go the Arrange tab, and in the Table group, you have a couple of options to choose from.
有一个堆叠布局和一个表格布局,它们类似于电子表格。让我们选择表格布局。
There is a stacked layout and a tabular layout, which is a layout that is very similar to a spreadsheet. Let us select the tabular layout.
您会看到它会将所有标签移到页眉区域。这些标签只会显示在每一页的顶部一次,而数据查询会针对详细信息部分中的每条记录重复。现在,您可以进行一些调整,使您的 ProjectName 字段更宽。
You can see that it moves all of the labels up to the page header area. These labels will appear only once at the top of every page and the data query will repeat for every record in the Details section. Now, you can go through and make some adjustments to make your ProjectName field wider.
正如您在上面的屏幕截图中看到的那样,详细信息部分和页脚之间有很大的空间。
As you can see in the above screenshot, there is a lot of space between Detail section and Page Footer.
让我们将页面页脚向上拖动,以减少空间,如下面的屏幕截图所示。我们现在将转到“设计”选项卡,然后单击“视图”按钮,并选择“报表视图”。
Let us drag the Page Footer up to reduce the space as in the following screenshot. We will now go to the Design tab and click on the View button and choose Report View.
你现在可以看到,一些项目名称不完整;你可以使用设计视图或版式视图调整它。
You can now see that some project names are not complete; you can adjust this with either the design view, or you can use the layout view to do that.
这就是我们只使用“设计视图”就创建简单报表的步骤。
That is how we create a simple report just from the Design View.
Ms Access - Formatting Reports
在本章中,我们将学习如何格式化报表。你会发现格式化报表与格式化表单有很多相似之处,但有一些工具和技巧是专门针对报表的。现在让我们了解报告部分和分组的概念。
In this chapter, we will learn how to format reports. You will find that there are a lot of similarities between formatting reports and formatting forms but there are a few tools and tricks that are specific to reports. Let us now look into the concept of report sections and grouping.
为此,我们需要打开我们在上一章中创建的一个报告。在这里,我们将看到其中一些信息如何在报告中显示。
For this, we need to open a report that we created in the last chapter. Here, we will see how some of this information is displayed on the report.
在这里,我们将从报告部分和分组开始。
Here, we will start with the report section and grouping.
现在让我们转到此报告的设计视图。
Let us now go to the Design view of this report.
你可以看到这里没有很多内容,这里只有页眉、详细信息部分和页脚可见。你可以非常容易地添加其他几个部分。
You can see that there’s not a lot to look and here only page header, details section, and page footer are visible. You can add an additional couple of sections very easily.
在此报告上的任意位置单击鼠标右键,你将看到页眉/页脚和报告页眉/页脚。此报告没有显示该报告页眉/页脚。让我们选择该选项并返回报告视图。
Right click anywhere on that report and you will see Page Header/Footer and Report Header/Footer. This particular report does not have that Report Header/Footer visible. Let us select that option and go back to the Report View.
可以看到,它只在报告的顶部添加了一个小彩色区域。在设计视图中,将鼠标悬停在该页眉分隔符正上方、单击并向下拖动以展开该区域。这将为报告页眉添加更多区域。
You can see it just adds a little colored area at the very top of the report. In the Design View, expand that area by hovering the mouse right at the top of that page header divider, clicking and dragging down. This will add more area to the report header.
在报告视图中,现在你将在报告的顶部看到更多区域,如下面的屏幕截图所示。
In the Report View, you will now see more area at the very top of the report as in the following screenshot.
报告页眉和页脚控件显示在该报告第一页的顶部。报告页脚控件控制你在报告底部最后一页上看到的内容。
The Report Header and Footer controls appear at the top of the first page of that report. The report footer controls what you see at the very last page at the bottom of the report.
报告页眉和页脚与你的页眉和页脚不同。放在页眉中的任何内容都会显示在每一页的顶部。同样,放在页脚中的任何内容都会在每页底部重复。
The Report Header and Footer is different from your Page Header and Page Footer. Anything that is placed in the Page Header will appear at the top of every page. Likewise, anything that is placed in the Page Footer will repeat at the bottom of every page.
现在你可以添加其他分组级别,要做到这一点,你要确保打开了组排序和总计区域。
Now you can add additional grouping levels, and to do that, you want to make sure you have the group sort and total area turned on.
在设计选项卡的分组和总计部分中,单击组和排序按钮,这将在底部打开组、排序和总计区域,如下面的屏幕截图所示。
In the Grouping and Totals section of the Design tab, click on the Group and Sort button which will open Group, Sort, and Total area at the bottom as shown in the following screenshot.
你现在可以在报告中的任何控件上添加其他组或分组。现在让我们单击添加组。
You can now add additional groups or grouping on any control that you have in your report. Let us now click on Add a Group.
在上图中,你可以看到报告的基本控件来源,包括项目名称、任务标题、到期日期和完成百分比。现在假设我们想要按项目将所有逾期任务分组,因此从列表中选择项目名称。
In the above screenshot, you can see the underlying control source for report with the project name, task title, due date and percent complete. Let us now say we want to group all of our late tasks by project, so select Project Name from the list.
现在,你将在详细信息部分上方看到一个额外的分组级别 ProjectName Header 。我们现在可以将此控件上移到此项目名称标题,而不是让项目名称显示在每个逾期任务旁边。你可以选择它,然后按键盘上的 Ctrl + X 将其从详细信息部分中剪切下来,然后单击该项目名称标题背景中的任意位置,然后按 Ctrl + V 将该控件粘贴到该项目名称标题中。
Now you will see an additional grouping level ProjectName Header above your details section. Instead of the project name appearing alongside each individual task that is late, we can now move this control up to this project name header. You can select it, and then press Ctrl + X on your keyboard to cut that from that details section and click anywhere in the background of that project name header, and then press Ctrl + V to paste that control in that project name header.
现在,你可以转到报告视图,查看对报告中事务进行分组所做的调整,如下面的屏幕截图所示。
You can now go to the Report View and see the adjustments made to group the things in the report as in the following screenshot.
每个项目在自己的线上都有自己的小级别,然后在这个区域下方,您将看到所有该项目已逾期的任务。现在,稍做更改之后,您可以在布局视图中开始设置报告格式。
Every single project has its own little level on its own line, and then underneath that area you will see all of the tasks that are late for the above project. With that changed now, you can go through and start formatting your report in the Layout View.
如果您想要扩大项目名称,则拖动下面的行,然后在“格式”选项卡中将字体大小更改为 20。
If you want to make the project name bigger, then drag the line below and change the font size to 20 in the Format Tab.
您可以增加控件的宽度,使其覆盖整个页面宽度。
You can increase the width of the control to cover the entire width of the page.
若要移除控件周围的边框,请在“格式”选项卡中单击“形状轮廓”,如下面屏幕截图所示。
To remove the border around the control, click on the Shape Outline in the Format tab as in the following screenshot.
选择 Transparent 选项。
Select the Transparent option.
让我们现在转到设计视图,如下面屏幕截图所示。
Let us now go to the Design View as in the following screenshot.
将任务标题调整到页面的左侧,如下面屏幕截图所示。
Adjust the task title to the left of your page as in the following screenshot.
让我们现在使用 Ctrl+X 和 Ctrl+V 将数据从“页眉”部分带到 ProjectName 标题部分。
Let us now bring the data from the Page Header section to the ProjectName header section using Ctrl+X and Ctrl+V.
让我们现在返回到报告视图。您现在会看到这些标签直接显示在它们描述的控件上方。
Let us now go back to the Report View. You will now see that the labels appear directly above the controls that they describe.
如果您注意到了,您会发现任务和下一个项目名称之间没有空格。它们之间应该有额外的空格。为此,我们需要添加 ProjectName Footer 。
If you notice, you will see that there is no space between the tasks and the next project name. There should be additional space in between. For that, we need to add ProjectName Footer.
现在,转到设计视图。
Now, go to the Design View.
在“分组、排序和总计”区域中,单击项目名称旁边的“更多”按钮。
In the Group, Sort and Total area click on the More button next to the project name.
您现在将看到有关如何按项目名称分组和排序的所有选项,如下面屏幕截图所示。
You will now find all the options for how to group and sort by project name as in the following screenshot.
在此处,我们有一个名为 Without a Footer Section 的属性。让我们通过单击文字 Without a Footer Section 旁边的那个小箭头更改该选项。
Here, we have a property called Without a Footer Section. Let us change that option by clicking on that little arrow next to the words Without a Footer Section.
将其更改为 With a Footer Section 。
Change it to With a Footer Section.
正如您在上面的屏幕截图中看到的那样,在“详细信息”部分下方的“ProjectName Footer”部分已添加。这将作为该项目名称组的页脚。
As you can see in the above screenshot that the ProjectName Footer section beneath the Details section is added. This will act as the footer for that project name group.
让我们现在返回到报告视图。
Let us now go back to the Report View.
若要移除交替区域在“详细信息”部分和“项目名称”区域中作为背景的阴影或外观,请再次转到设计视图。
To remove shading or the appearance of background for alternating areas in both the Details section and in the Project Name area, go to the Design View again.
让我们先通过单击详细信息分隔符然后打开“属性”表来更改“详细信息”部分。
Let us change the Details section first by clicking on the detail divider and then open Properties sheet.
在“格式”选项卡上,更改 Alternate Back Color 为 No Color ,如下面的屏幕截图所示。
On the Format tab, change the Alternate Back Color to No Color as in the following screenshot.
类似地,将“备用背景颜色”更改为“无颜色”,以适用于“ProjectName Header”和“ProjectName Footer”,然后转到“报表视图”。
Similarly change the Alternate Back Color to No Color for both ProjectName Header and ProjectName Footer and go to Report View.
现在你可以看到报表的显示方式。要了解数据将逐页打印的方式,让我们转到“打印预览”。
You can now see how the report looks. To see how your data will print page by page, let us go to the Print Preview.
这就是打印时显示的样子。
This is how it will look like when you print it.
Ms Access - Built-In Functions
在本章中,我们将使用内置函数。在 Access 中,内置函数接近 100 个,不可能涵盖所有函数。在本章中,我们将涵盖基本结构、语法以及使用一些常见的函数,还有一些需要注意的事项,以便你能自己探索其他一些函数。
In this chapter, we will be working with Built-in Functions. In Access, there are close to a hundred built-in functions and it is almost impossible to cover every single one of them. In this chapter, we will cover the basic structure, syntax, and use some of the more popular functions, and also the pitfalls, so that you can go exploring some of the other functions on your own.
Functions
函数是执行任务或计算并返回结果的 VBA 过程。函数通常可以用于查询,但还有其他可以使用函数的地方。
A function is a VBA procedure that performs a task, or calculation, and returns a result. Functions can generally be used in queries, but there are other places that you can use functions.
-
You can use functions in table properties, for example, if you want to specify a default value for a date/time field, you can use the date or the Now function to call up the current date/time information from your system, and input that value automatically.
-
You can also use functions in an expression when you create a calculated field, or use the functions inside form or report controls. You can use functions even in macro arguments.
-
Functions can be quite simple, requiring no other information to be called, or, simply reference one field from a table or query.
-
On the other hand, they can also get quite complicated, with multiple arguments, field references, and even other functions nested inside another function.
现在让我们了解一些使用内置函数的示例。
Let us now look into some examples of using built-in functions.
Date & Time Functions
现在让我们了解日期和时间函数 −
Let us now understand the Date and Time functions −
-
The Date() function is designed to return the current system date. This function does not require any function arguments or additional information. All you have to do is write the name of the function and those open and close parentheses.
-
There are two very similar built-in functions Time() and Now().
-
The Time() Function returns the current system time only and the Now() Function returns both the current system date and time.
-
Depending on the data that you want to track, or store, or query, you have three built-in, easy-to-use functions to help with that task.
现在让我们打开数据库,并使用查询设计创建一个新查询,然后添加 tblProjects 和 tblTasks。
Let us now open your database and create a new query using query design and add tblProjects and tblTasks.
从 tblProjects 添加 ProjectName,从 tblTasks 添加 TaskTitle、StartDate 和 DueDate,然后运行查询。
Add ProjectName from tblProjects and TaskTitle, StartDate and DueDate from tblTasks and run your query.
现在你可以看到来自所有项目的不同任务。如果你想要查看截至今天日期的正在进行中的项目任务,那么我们必须使用 Date() 函数指定一个条件来查看在今天或之后开始的项目。
You can now see all the different tasks from all projects. If you want to view the project tasks that are in progress as on today’s date, then we have to specify a criterion using a Date() Function to look at projects that start on or after today’s date.
我们现在来具体说明 StartDate 下的条件。
Let us now specify the criteria underneath the StartDate.
条件以一个运算符 greater than symbol 开头,其后是一个 equal to symbol 再加上一个 Date Function 。
The criteria starts with an operator greater than symbol, followed by an equal to symbol and then Date Function.
当我们运行此查询时,所有任务要么出现在今天的日期,要么出现在未来日期,如以下屏幕截图所示。
When we run this query, all the tasks will occur either on today’s date or in the future as in the following screenshot.
这是一个关于如何将 Date() 函数用作查询条件的示例。
This was an example of how you can use the Date() function as query criteria.
-
Let us now say this query needs to be more flexible in terms of the dates it is pulling starting this week.
-
We do have a couple of different tasks that began this week, that are not showing up in this current list, because of our criteria. It’s looking at start dates that are equal to today or above.
如果我们要查看已开始但尚未完成或应在今天完成的本周的任务,让我们返回到设计视图。
If we want to view the tasks that started this week, that have not yet completed or should complete today, let us go back to the Design View.
在此,我们将会向这些条件添加一些附加信息。事实上,我们希望它大于或等于今天的日期减去七天。
Here, we will add some additional information to these criteria. In fact, we want it greater than or equal to today’s date minus seven days.
如果我们键入减去七然后运行查询,您还将看到本周开始的任务。
If we type minus seven and run the query, you can see the tasks that started this week as well.
DateDiff() Function
DateDiff() 函数是另一个非常流行的日期/时间函数。DateDiff 函数返回一个变体(长整型),指定两个特定日期之间的时间间隔数量。换句话说,它可以计算两个日期之间的差值,而且您可以选择函数用以计算该差值的间隔。
The DateDiff() Function is another very popular date/time function. The DateDiff Function returns a Variant (long), specifying the number of time intervals between two specified dates. In other words, it calculates the difference between two dates, and you get to pick the interval by which the function calculates that difference.
现在让我们假设我们要计算作者的年龄。为此,我们首先需要创建一个新查询并添加作者表,然后添加 FirstName、LastName 和 BirthDay 字段。
Let us now say we want to calculate our authors' age. For this, we first we need to create a new query and add our authors table and then add FirstName, LastName, and the BirthDay fields.
我们可以通过计算出生日期或生日与今天的日期之间的差值来计算人们的年龄。
We can calculate people’s age by calculating the difference between their date of birth, or birthday and whatever today’s date is.
让我们尝试在一个新字段中使用 DateDiff 函数。
Let us try using the DateDiff Function in a new field.
我们称之为年龄,后加一个冒号,然后编写 DateDiff 函数。
Let us call it Age followed by a colon, and then write DateDiff Function.
-
The first function argument for the DateDiff function is the interval, so type “yyyy”.
-
The next function argument is the first date that we want to calculate by, which, in this case, will be the Birthday field.
-
The third function argument is whatever today’s date is.
现在,运行您的查询,您将看到新字段,该字段显示每个作者的年龄。
Now, run your query and you will see the new field which shows the age of each author.
Format() Function
Format() 函数返回一个字符串,其中包含一个表达式,该表达式根据一个格式表达式中包含的说明进行了格式化。以下是 Format() 函数中可以使用的用户定义格式列表。ss
The Format() Function returns a string, containing an expression formatted according to instructions contained in a format expression. Here is the list of user-defined formats which can be used in Format() function.ss
Setting |
Description |
yyyy |
Year |
q |
Quarter |
m |
Month |
y |
Day of year |
d |
Day |
w |
Weekday |
ww |
Week |
h |
Hour |
n |
Minute |
s |
Second |
现在,我们回到你的查询,并使用 Format() 函数在其中添加更多字段。
Let us now go back to your query and add more fields in the same using the Format() function.
输入 Format 函数。第一个函数参数将是一个表达式,它几乎可以是任何东西。现在,我们让 birthday 字段是第一个,接下来要做的是编写我们的格式。在这种情况下,我们需要月份、月份、天、天。用引号写上“mmdd”,然后运行你的查询。
Type the Format Function. The first function argument will be an expression, which can be almost anything. Let us now have the birthday field as the first and the next thing is to write our format. In this case, we need month, month, day, day. Write “mmdd” in quotes and then, run your query.
它现在正在从 birthday 字段获取日期,4 是月份,17 是天。
It is now taking the date from the birthday field, 4 is the month and 17 is the day.
让我们在下一个字段中添加“mmm”和“mmmm”,而不是“mmdd”,如下面的屏幕截图所示。
Let us add “mmm” and “mmmm” instead of “mmdd” in the next fields as in the following screenshot.
运行你的查询,您将看到结果,如下面的屏幕截图所示。
Run your query and you will see the results as in the following screenshot.
在下一个字段中,它将返回该生日中月份的前 3 个字符,在最后一个字段中,您将获得完整的月份名称。
In the next field, it is returning the first 3 character from the name of the month for that birthday and in the last field you will get the full month name.
要查看生日之后的月份和年份,让我们添加“yyyy”,如下面的屏幕截图所示。
To see the month followed by year from birthday, let us add the “yyyy” as well as shown in the following screenshot.
让我们再次运行你的查询。
Let us run your query again.
您现在将看到月份后面跟着一个逗号,然后是年份。
You will now see the month followed by a comma and then the year.
IIf() Function
IIf() 函数是“Immediate If”的缩写,此函数将表达式评估为真或假,并为每个表达式返回一个值。它最多有三个函数参数,所有这些参数都是必需的。
The IIf() Function is an abbreviation for “Immediate If” and this function evaluates an expression as either true or false and returns a value for each. It has up to three function arguments, all of which are required.
-
The first argument is any expression that you want to evaluate.
-
The next argument stands for the true part, which can be a value or an expression returned if your first expression is true.
-
The last argument is what you want returned if your expression is false.
Example
我们举一个简单的例子。我们将使用查询设计创建一个新查询,添加 tblAuthors 表,然后添加以下字段。
Let us take a simple example. We will create a new query using query design and add tblAuthors table and then add the following fields.
您现在可以看到我们有三个字段——FirstName、MiddleInitial、LastName,然后是这个连接字段,它将所有三个字段都拉在一起。让我们运行你的查询来查看此查询的结果。
You can now see we have three fields — FirstName, MiddleInitial, LastName, and then this concatenated field, which is pulling all three fields together. Let us run your query to see the result of this query.
现在,您可以看到查询的结果,但您也会注意到有些记录没有中间名首字母。例如,Joyce Dyer 记录没有中间名首字母,但在 FullName 字段中,您将看到实际不需要的句点。因此,返回到设计视图。在这里,我们将使用 IIf 函数以不同的方式连接名称。
Now, you can see the result of the query, but you will also notice that some records do not have a middle initial. For example, the Joyce Dyer record does not have a middle initial, but in the FullName field you will see the period that really doesn’t need to be there. So, go back to the Design View. Here, we will concatenate the name in a different way using the IIf Function.
让我们在另一个字段中写出名称并将其称为 FullName1,然后键入 IIf 函数。
Let us write the name in another field and call it FullName1 and then type the IIf function.
-
The first function argument for the Immediate If function is going to be your expression. In the expression, we will see if the middle initial field is blank or is null.
-
The next argument is the true part. So, if the middle initial is null then we would want to display the FirstName and the LastName.
-
Now, for our false part — if the MiddleInitial is not null, then we would want to display the FirstName, MiddleInitial, and LastName.
现在让我们运行您的查询,您将看到结果,如下面的屏幕截图所示。
Let us now run your query and you will see the results as in the following screenshot.
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.
Ms Access - Data Import
在本章中,我们将讨论在 Access 中导入数据以及您可以使用 Access 导入哪些类型的数据。通常,数据存储在各种格式、文件和位置,这使得获取和使用数据变得很困难。如果您在电子表格、SharePoint 列表或其他格式中拥有数据,则只需几个步骤即可将其导入 Access 数据库,使其在 Access 中更易于获得。
In this chapter, we will be talking about importing data in Access and what kinds of data you can import using Access. Normally data is stored in various formats, files, and locations, which makes it hard to get and use it. If you have data in a spreadsheet, a SharePoint list, or some other format, you can import it into an Access database with just a few steps, making it much more, easily available in Access.
-
The Save As command is generally used to save a document in another format, so that you can open it in another program.
-
In Access you can’t use the Save As command in the same way, you can save Access objects as other Access objects, but you cannot save an Access database as a spreadsheet file.
-
To save Access as a spreadsheet file, you will need to use the import feature on the External Data tab.
Different Types of Data Access Can Import
要了解您可以在 Access 数据中导入哪种类型的数据,让我们打开您的数据库并转到“外部数据”选项卡。
To understand what kind of data you can import in the Access data, let us open your database and go to the External Data tab.
在“导入和链接”组中,您可以看到 Access 中可用于数据导入的不同类型选项。下面是使用最广泛的数据导入格式。
In the import & Link group, you can see the different kind of options available for data import in Access. Following are the most commonly used data import formats.
-
Microsoft Office Excel
-
Microsoft Office Access
-
ODBC Databases (For example, SQL Server)
-
Text files (delimited or fixed-width)
-
XML Files
Example
让我们看一个从 Excel 文件导入数据的简单示例。以下是 Access 文件中的数据。
Let us look at a simple example of data importing from an Excel file. Here is the data in Access file.
要在 Access 中导入数据,我们首先需要打开 Access 数据库,然后转到“外部数据”选项卡,如下面的屏幕截图所示。
To import the data in Access, we first need to open the Access database and then go to the External Data tab as in the following screenshot.
在“导入和链接”组中,您会看到一个选项 Excel。让我们单击该选项。
In Import & Link group, you will see an option Excel. Let us click on that option.
浏览您想要从中导入数据的 Excel 文件,然后我们有不同的选项来存储数据。让我们选择第一个选项并单击确定。
Browse the Excel file from which you want to import data and the then we have different options to store data. Let us select the first option and click Ok.
在这里您将看到您数据的预览。现在,单击下一步。
Here you will see the preview of your data. Now, click Next.
在预览中,你现可以看到第一行包含列标题。现在我们勾选该复选框,然后点击下一步。
In the Preview, you can now see that the first row contains the column headings. Let us now check the check box and click Next.
你现在将看到一个对话框,你可以在其中为每列/字段设置数据类型。如果你不想导入任何字段,只需勾选该复选框。完成“FirstName”字段之后,只需点击“MiddleInitial”字段。
You will now see a dialog box where you can set the data type for each column/field. If you don’t want to import any field, just check the check box which says do not import field. Once you are done with the FirstName field, just click on the MiddleInitial field.
现在让我们遍历所有字段,然后点击下一步。
Let us now go through all the fields and then, click Next.
以下是主键的不同选项。让我们选择第一个选项并点击下一步。
Here are the different options for primary key. Let us select the first option and click Next.
在最后一个对话框中,你可以输入你选择的表名称,然后点击完成。
In the last dialog box, you can enter the table name of your choice and click Finish.
如果你希望保存所有这些步骤,则勾选复选框,然后关闭该对话框。
If you want to save all these steps, then check the checkbox and close the dialog box.
现在让我们到导航窗格。你将看到此处添加了一个新表,当你打开新添加的表时,你会在 Access 中看到所有数据。
Let us now go to the Navigation pane. You will see a new table is added here and when you open the newly added table you will see all of your data in Access.
Example
现在让我们看看从 Access 数据库导入数据的另一个示例。让我们再次进入外部选项卡。
Let us now look at another example of importing data from the Access database. Let us go to the External Tab again.
在导入和链接组中,点击 Access 选项。
In Import & Link group, click on the Access option.
浏览你想从中导入数据的 Access 数据库,然后选择第一个选项,即导入表、查询、窗体等。现在,点击确定。
Browse the Access database from which you want to import the data and then select the first option which says Import tables, queries, form etc. Now, click Ok.
在上述对话框中,你可以看到表、查询、窗体等的不同选项卡,你可以从中选择希望导入哪种类型的数据。
In the above dialog box, you can see different tabs for Tables, Queries, Forms etc. from where you can select what kind of data you want to import.
让我们进入 Reports 标签,然后选择你想导入的任何报表;你还可以通过点击 Select All 按钮选择所有数据。让我们选择 Projects 并点击 Ok 。
Let us go to the Reports tab and select any report you want to import; you can also select all the data by clicking on the Select All button. Let us select Projects and click Ok.
现在,关闭对话框。在导航窗格中,你将看到添加了一个新报表。让我们打开此报表,你将看到该报表中的所有数据。
Now, close the dialog box. In the navigation pane, you will see that a new report is added. Let us open this report and you will see all the data in that report.
Ms Access - Data Export
在本章中,我们将了解如何从 Access 中导出数据。数据导出实际上与数据导入相反。在数据导入中,我们将数据从其他格式导入 Access,而在导出中,我们会以其他格式保存数据。
In this chapter, we will understand how to export data from Access. Data export is actually the opposite of importing data. In importing data, we bring data from other formats in Access, while in exporting we save the data in other formats.
要了解你可以从 Access 数据中导出哪种数据,让我们打开你的数据库,进入外部数据选项卡。
To understand what kind of data you can export from Access data, let us open your database and go to the External Data tab.
在导出组中,你可以看到为从 Access 中导出数据而提供的不同类型的选项。以下是使用最广泛的数据导出格式−
In the Export group, you can see the different kind of options available for data export from Access. Following are the most commonly used data export formats −
-
Microsoft Office Excel
-
Microsoft Office Access
-
ODBC Databases (For example, SQL Server)
-
Text files (delimited or fixed-width)
-
XML Files
Example
让我们来看一个从 Access 中导出数据的简单示例。打开你想从中导出数据的数据库。在导航窗格中,选择你想从中导出数据的对象。
Let us look at a simple example of data export from Access. Open your database where you want to export the data from. In the Navigation Pane, select the object that you want to export the data from.
你可以从表、查询、窗体和报表对象等导出数据。让我们选择 qryAllProjects,然后进入外部数据选项卡。
You can export the data from table, query, form, and report objects etc. Let us select the qryAllProjects and then, go to the External Data tab.
在外部数据选项卡上,点击你想导出的数据类型。例如,要以可通过 Microsoft Excel 打开的格式导出数据,请点击 Excel。
On the External Data tab, click on the type of data that you want to export to. For example, to export data in a format that can be opened by Microsoft Excel, click Excel.
访问启动导出向导。在向导中,您可以设置导出目标文件名和格式、是否包含格式设置和布局以及要导出的记录等信息。填写完所需的信息后,单击“确定”。
Access starts the Export wizard. In the wizard, you can set the information such as the destination file name and format, whether to include formatting and the layout, which records to export. Once you are done with the required information, click Ok.
在此向导屏幕上,Access 通常会询问您是否要保存导出操作的详细信息。如果您认为将来需要反复执行相同的操作,则选中“ Save export steps ”复选框并关闭对话框。
On this screen of the Wizard, Access usually asks you if you want to save the details of the export operation. If you think you will need to perform the same operation on a recurring basis, select the Save export steps check box and close the dialog box.
随即打开以下 Excel 文件。
The following Excel file opens up.
现在假设您想将数据导出到文本文件,请在“外部数据”选项卡中单击“文本文件”。
Let us now suppose you want to export data to a text file, on the External Data tab, click on the Text File.
指定导出选项并单击“确定”。您将看到编码对话框,其中我们要使用默认编码导出数据。
Specify the export options and click Ok. You will see the Encode dialog box, wherein we want to export the data in default encoding.
选择第一个选项并单击“确定”。
Select the first option and click Ok.
在此向导屏幕上,Access 通常会询问您是否要保存导出操作的详细信息。如果您认为将来需要反复执行相同的操作,则选中“ Save export steps ”复选框并关闭对话框。
On this screen of the wizard, Access usually asks you if you want to save the details of the export operation. If you think you will need to perform the same operation on a recurring basis, select the Save export steps check box and close the dialog box.
现在,您将看到打开文本文件。
You will now see that the Text file is open.
同样,您还可以探索其他选项。因此强烈建议也熟悉其他导入导出功能。
Similarly, you can explore other options as well. Thus, it is highly recommended to play with other import export features too.