在 Access 中创建和自定义 Web 应用

重要

Microsoft 不再建议在 SharePoint 中创建和使用 Access Web App。 作为备选方法,请考虑使用 Microsoft PowerApps,生成适用于 Web 和移动设备的无代码业务解决方案。

Access 2013 采用新的应用程序模型,使主题专家可以快速创建基于 Web 的应用程序。 Access 中包含一系列模板,您可用于开始创建应用程序。

使用 Access 2013 构建应用程序的先决条件

若要按照此示例中的步骤操作,需要:

  • Access
  • SharePoint 开发环境

有关设置 SharePoint 开发环境的详细信息,请参阅设置 SharePoint 的常规开发环境

有关获取 Access 和 SharePoint 的详细信息,请参阅下载

创建应用程序

假设你要创建一个 Access 应用,用于跟踪业务问题。 在开始从头开始创建表和视图之前,应搜索满足需求的架构模板。

创建问题跟踪应用程序

  1. 打开 Access 并选择 "自定义 Web 应用程序"

  2. Enter a name and the web location for your app. You can also choose a location from the Locations list and choose Create.

  3. 在“希望跟踪什么?”框中键入 问题,然后选择 Enter。 图 1 显示了一个可能对跟踪问题有用的模板列表。

    图 1. 与问题搜索相匹配的模板

    与问题搜索匹配的

  4. 选择“问题”。 Access 创建一系列表和视图。

浏览应用程序

若要了解架构和视图是否满足您的需求,您应该对其进行检查。

平铺窗格中显示了通过选择"问题"架构创建的表。 "问题"、"客户"和"员工"表是应用程序的重心所在。 "问题"表存储关于每个问题的信息。 每个问题由员工代表客户创建并分配给某个员工。 "相关问题"和"问题注释"表在应用程序中起支持作用。 "相关问题"表使您可以将一个问题链接到另一个。 "问题注释"表存储对一个问题的多个注释。

In an Access desktop (.accdb) database, the relationships between tables are managed in the Relationships window. Access 2013 apps manage relationships by using fields set to the Lookup data type. Let's examine the relationships for the Issues table by right-clicking the Issues tile and selecting Edit Table.

The Customer field is related to the Customers table. To examine the relationship, select the Customer field and then select Modify Lookups. The Lookup Wizard is displayed, as shown in Figure 2.

图 2. 显示与 Customers 表的关系的查找向导

显示关系的查找向导

“查找向导”对话框显示“客户”字段链接到“客户”表,并从“客户”表中返回“显示名字和姓氏”字段。

The Opened By, Assigned To, and Changed By fields are related to the Employees table. Several other fields are also set to the Lookup data type. In these cases, the Lookup data type is used to specify the specific values to allow for in the field.

Close the Issues table and examine the Tile Pane. The top three tiles, for the Issues, Customers, and Employees tables, are displayed differently than the bottom two tiles for the Related Issues and Issue Comments table, as shown in Figure 3.

图 3. 适用于“问题”架构的平铺窗格

“问题架构”

“相关问题”和“问题注释”表显示为灰色,因为它们将对 Web 浏览器中的用户隐藏。

Let's use the app to track some issues. To do this, click Launch App to open the app in your web browser.

The app opens the Issues List view of the Issues table. Before adding an issue, it would be a good idea to add some customers and employees. Click the Customers tile to start adding customers.

使用视图选择器从“客户”表的三个可用视图(即“列表”、“数据表”和“组”)中选择一个,如图 4 中所示。

图 4. 视图选择器

视图选择器

Choosing List activates the Customers List view, which is a List Details view. List Details is one of the views Access automatically generates when you create a table. The main feature that distinguishes a List Details view is the list pane that appears on the left side of the view. The list pane is used to filter and navigate the records contained in the view. In an Access desktop database, implementing a searchable list view would require writing custom code.

Choosing Datasheet opens the Customers Datasheet view. Datasheet is the other kind of view Access automatically generates when you create a table. Datasheet views are useful for those who find it easier to enter, sort, and filter data in a spreadsheet-like manner.

选择"组"将打开"摘要"视图。 "摘要"视图可用于根据字段对记录进行分组,并(可选)计算总和或平均值。

添加客户时,可使用操作栏添加、编辑、保存和删除记录或取消编辑。 操作栏是每个视图顶部的 可自定义 工具栏,如图 5 中所示。

图 5. 操作栏

操作栏

添加一些客户和员工后,打开"问题列表"视图并开始添加问题。 在"客户"框中键入客户的名称时,将出现一个或多个客户名称,如图 6 中所示。

图 6. AutoComplete 控件

自动完成控件

"客户"框是一个自动完成控件。 自动完成控件显示与您在框中所键入内容匹配的记录的列表。 这有助于确保数据输入的准确性。

自定义应用程序

现在您已浏览了整个应用程序,您注意到"问题列表"视图不包含客户的任何联系信息。 现在我们来对应用程序进行自定义,在创建问题时在问题表中添加客户的工作电话。

向问题表添加字段

  1. 在 Access 中打开应用程序。
  2. 依次选择“问题”窗格、“设置/操作”图标和“编辑表”
  3. 在“字段名称”列的第一个空白单元格中输入“联系人号码”
  4. 在“数据类型”列中选择“短文本”
  5. 选择“保存”
  6. 关闭问题表。

现在我们已有字段来存储电话号码,让我们创建一个数据宏来查找联系信息。

创建数据宏以查找联系信息

  1. 在“创建”组中,依次选择“高级”、“数据宏”

  2. Choose Create Parameter.

  3. In the Name box, enter CustID. In the Type dropdown, choose Number (Floating Decimal).

  4. From the Add New Action dropdown, choose LookupRecord.

  5. 在“在其中查找 A 记录”下拉列表中,选择“客户”

  6. 在“Where 条件”框中,输入 [Customers].[ID]=[CustID]

  7. 从“添加新操作”下拉列表中选择“SetReturnVar”

    注意

    You'll see two Add New Action dropdowns, one within the LookupRecord block, and another outside the LookupRecord block. You should choose the Add New Action dropdown within the LookupRecord block, as shown in Figure 7.

    图 7. 添加“新建操作”下拉菜单

    “添加新操作”下拉列表

  8. 在“名称”框中,输入 ContactPhone

  9. In the Expression box, enter [Customers].[Work Phone].

  10. Choose Save. Enter GetContactPhone in the Macro Name box and then choose OK.

    宏应类似于图 8 中所示的宏。

    图 8. GetContactPhone 数据宏

    GetContactPhone 数据宏

  11. 关闭宏设计视图。

现在我们已准备好将“联系人号码”字段添加到问题列表窗体中。

将“联系人号码”字段添加到问题列表窗体中

  1. 选择“问题”表。 This chooses the Issues list form.
  2. In the View selector, choose List, choose the Settings/Action icon, and then choose Edit.
  3. 将“联系人号码”字段从“字段列表”窗格拖动到你希望联系人号码显示的窗体上的位置。
  4. 选择“联系人号码”文本框,然后单击“数据”
  5. 在“控件名称”框中,输入 CustomerContact,然后关闭“数据”弹出框。
  6. Choose Save.

Now we should write a user interface (UI) macro that copies the Work Phone field from the Customers table into the Contact Phone field of the Issues table. “CustomerAutocomplete”控件的“更新后”事件是该宏的一个理想位置。

创建 AfterUpdate 宏

  1. 依次选择“CustomerAutocomplete”控件、“操作”按钮、“更新后”。 将在宏设计视图中打开一个空宏。

  2. 从“添加新操作”下拉列表中,选择“RunDataMacro”

  3. 在“宏名称”下拉列表中,选择“GetContactPhone”

  4. 在" CustID"框中,输入" [CustomerAutocomplete] "。

  5. 在“SetLocalVar”框中,输入电话。 当您选择之前创建的 GetContactPhone 数据宏时,Access 自动填充参数名称并返回宏的变量。 客户的电话号码存储在名为 Phone 的变量中。

  6. 从“添加新操作”下拉列表中,选择“SetProperty”

  7. 在“控件名称”框中,输入 CustomerContact

  8. 在“属性”下拉列表中,选择“值”

  9. 在“值”框中,输入 =[Phone]

  10. 选择“保存”。 宏应类似于图 9 中所示的宏。

    图 9. AfterUpdate 宏

    更新宏后

  11. 关闭宏设计视图。

  12. Close the Issues List view. Choose Yes when you are prompted to save your changes.

Now we're ready to text the customization. 单击“启动应用”在 Web 浏览器中打开应用,然后添加新问题。 输入客户姓名后, “联系人号码 ”框会自动更新,如图 10 所示。

图 10. 已更新电话号码的问题视图

使用电话号码更新的问题视图

总结

使用 中包含的架构模板之一是开始创建 Access Web 应用程序的好办法。 为您自动创建的视图包含高级功能,这需要在 Access 桌面数据库中实施的自定义代码。

另请参阅