资源描述
第五章 窗体设计 Creating Forms Hour 4. Forms Introduced,5.1 Understanding the functions and types of forms 窗体的作用及分类 5.2 Using the AutoForm feature and Form Wizard to create new forms, Understanding controls and properties 窗体的创建方法、控件及属性 5.3 Forms in Northwind 窗体实例分析:罗斯文商贸系统中的窗体对象,Forms allow you to display data in an aesthetically pleasing way. They also provide an excellent mechanism for data entry. In this hour youll learn the following: What types of forms are available How to open / close a form How to work with data from within a form How to find and replace table data in a form How to sort and filter form data How to view the design of a form How to use the AutoForm feature and Form Wizard to create new forms How to use conditional formatting to enhance forms,5.1 Understanding the functions and types of forms Uses for Forms Developers often think that forms exist solely for the purpose of data entry. To the contrary, forms serve many different purposes in Access 2003: Data entry They can be used for displaying and editing data. 显示和编辑数据 Application flow They can be used for navigating through an application. 应用程序流程控制 Custom dialog boxes They can be used to provide messages to users. 定制对话框(信息显示、数据输入) Printing information They can be used to provide hard copies of data-entry information. 数据打印,For examples Data entry 显示和编辑数据:“订单”窗体,For examples Application flow 应用程序流程控制:“主切换面板”,For examples Custom dialog boxes They can be used to provide messages to users。定制对话框(信息显示、数据输入):“各年销售额对话框”窗体,For examples “销售额报表对话框”窗体,For examples Printing information数据打印:“客户电话列表”窗体,For examples “客户电话列表”窗体的打印预览视图,5.1 Understanding the functions and types of forms What are the basic types of forms? There are six basic types of forms: Columnar (also known as full-screen) forms 纵栏式 Tabular forms 表格式 Datasheets 数据表 Main/subforms 主/子窗体 Pivot table forms 数据透视表 Graphs 图表,5.1 Understanding the functions and types of forms Columnar form:“雇员”窗体,Figure 5-1-1 “Employees” Form 雇员,5.1 Understanding the functions and types of forms tabular form:“产品-表格式”窗体,Datasheet:”订单子窗体”,Main/subform:“订单”窗体,内含“订单子窗体”,订单子窗体,Main/subform:“季度订单”窗体,内含“季度订单子窗体”,订单子窗体,Pivot table forms:“销售额分析”窗体,Graphs 图表:“各月销售额(图表式)”,Viewing the Design of a Form,Figure 5-1 “Customers” Form in Design View,Figure 5-2 “Customers” Form-Data Form View,5.2 Using the AutoForm feature and Form Wizard to create new forms, Understanding controls and properties 窗体的创建方法、控件及属性,Using the AutoForm Feature Creating a form by using the AutoForm feature is amazingly easy. Heres how it works: Select the table or query on which you want to base the new form. Select the Customers table for this example. Select AutoForm from the New Object drop-down list box (see Figure 5-3). Access creates a form based on the selected table or query (see Figure 5-4).,Figure 5-3 The New Object drop-down list box,Figure 5-4 The Result_ AutoForm,Using the Form Wizard to Build a Form Using the Form Wizard gives you more flexibility than using the AutoForm feature to create forms. It also requires more knowledge on your part. Heres how you use it: Click Forms in the list of objects in the Database window. Double-click the Create Form by Using Wizard option. Select the table or query on which you want to base the form (see Figure 5-5).,Figure 5-5 Selecting a table or query,and selecting fields,Using the Form Wizard to Build a Form Select the fields you want to include on the form. Click Next. Select a layout for the form. Click Next. Select a style for the form. Click Next. Provide a title for the form. Click Finish. (see Figure 5-6),Figure 5-6 “Customers-Wizard” Form,Hour 11. Creating Forms Creating a Form in Design View Working with the Form Design Window Selecting the Correct Control for the Job Form and Control Properties,Creating a Form in Design View,Figure 5-7 A New Form in Design View,工具箱,字段列表,属性对话框,设计区,Creating a Form in Design View 其中: 设计区:布局控件(controls) 字段列表:窗体的数据 工具箱:选取控件 属性对话框:窗体及控件的属性 与窗体有关的工具如下图所示:,工具箱,属性对话框,字段列表,Creating a Form in Design View 设计区的组成:每个窗体的设计区由主体节、窗体页眉/页脚节 以及页面页眉/页脚节组成。 主体节:显示窗体的数据 窗体页眉/页脚:每个窗体的概要信息 页面页眉/页脚:打印窗体数据时每一页要显示的信息 如下一页的“客户电话列表”窗体所示,Figure 5-8 “客户电话列表” Form in Design View,Working with the Form Design Window see Hour 11 Understanding and Working with the Form Design Tools Toggling the Tools to Get What You Want Adding Fields to a Form Selecting, Moving, Aligning, and Sizing Form Objects Modifying Object Tab Order,Selecting the Correct Control for the Job see Hour 11 Labels Text Boxes Combo Boxes List Boxes Check Boxes Option and Toggle Buttons Option Groups,Form and Control Properties see Hour 11 Format 格式:外观形式 Data 数据:绑定的数据 Event 事件:利用事件属性执行特定操作 Other 其它:名称、Tab键索引等,5.3 Forms in Northwind 窗体实例分析:罗斯文商贸系统中的窗体对象 启动:信息提示 主切换面板:系统功能选项 类别:主子窗体,子窗体“产品列表”,显示/编辑类别及产品 信息 产品列表:连续窗体,显示/编辑相关产品信息 产品:纵栏式单一窗体,显示/编辑产品信息 客户:纵栏式单一窗体,显示/编辑客户信息,订单:主子窗体,子窗体“订单子窗体”,显示/编辑订单及订 单明细信息 订单子窗体:数据表式,显示/编辑订单明细信息,用作订单 窗体的子窗体 客户标签对话框 各年销售额对话框 销售额报表对话框 客户电话列表:连续窗体,包含页面页眉/页脚 客户订单:多层主子窗体,显示客户订单信息。子窗体为客户 订单子窗体1和客户订单子窗体2 季度订单:主子窗体,显示客户1997年各季度订单金额。子窗 体为季度订单子窗体,其数据源来自交叉表查询 销售额分析:数据透视表。,信息提示、参数输入,5.3 Forms in Northwind 窗体实例分析:罗斯文商贸系统中的窗体对象 例一 “类别”窗体主子窗体,子窗体“产品列表”,Figure 5-9 “类别”窗体 连续窗体,例二 “产品”窗体纵栏式,其中,按钮控件“预览产品列表”使用了“超级链接子地址”属性,以打开相应报表。,Figure 5-10 “产品”窗体,例二 “产品”窗体中按钮控件“预览产品列表”的格式属性:,例三,“订单”窗体主子窗体。,“订单子窗体”窗体,记录源:订单查询,其中,主窗体记录源为“订单查询”查询:订单 + 客户 (信息) 子窗体记录源是“扩展订单明细”查询:订单明细 + 产品 重点关注: 1、“收单者”:设计窗口如下,其中控件来源“客户ID”(订单),该控件的数据被更新前和更新后分别执行相关代码。,Private Sub 客户ID_BeforeUpdate(Cancel As Integer) 如果“客户ID”组合框为空显示消息。 Dim strMsg As String, strTitle As String Dim intStyle As Integer If IsNull(Me!客户ID) Or Me!客户ID = “ Then strMsg = “您必须从“收单者“列表框选择一项。“ strTitle = “要求收单客户“ intStyle = vbOKOnly MsgBox strMsg, intStyle, strTitle Cancel = True End If End Sub,Private Sub 客户ID_AfterUpdate() 基于“客户ID”组合框选定的值更新“货主”控件。 Me!货主名称 = Me!客户ID.Column(1) Me!货主地址 = Me!地址 Me!货主城市 = Me!城市 Me!货主地区 = Me!地区 Me!货主邮政编码 = Me!邮政编码 Me!货主国家 = Me!国家 End Sub,重点关注: 2、计算控件“订单小计”:单独运行“订单子窗体”时,该控件值为所有订单明细中的总价之和,当关联到“订单”窗体中时,该控件值仅为一张订单的总价之和了。 “订单”窗体中的“小计”控件将引用该值。,Figure 5-11 “订单子窗体”窗体,计算控件“订单小计”,重点关注: 3、计算控件“小计”:控件来源为 “订单子窗体”窗体中的“订单小计”,既: =订单子窗体.Form!订单小计,4、计算控件“合计”:控件来源为 =小计+运货费,重点关注: 5、控件“运货商”:使用了“选项组”,三种可能的值1,2,3,分别对应三个运货商的ID 。,例四,“季度订单”窗体主子窗体。(see Figure 5-12 ),Figure 5-12 “季度订单”窗体,思考:分析主窗体中的“总计”控件,其中,主窗体记录源为“季度订单”查询:客户(1995年有订单的) 该查询设计画面如下图所示。,Figure 5-13 数据来源:“季度订单”查询,其中,主窗体记录源为“季度订单”查询:客户(1995年有订单的) “季度订单子窗体”控件的记录源是“各种产品的季度订单”查询,如下图所示(交叉表查询,列标题固定)。,Figure 5-14 数据来源:“各种产品的季度订单”查询,交叉表查询,列标题固定,“季度订单子窗体”的设计画面如下图所示,其中,窗体的默认视图为“连续视图”,这样作为子窗体使窗体页眉和页脚可以显示出来; “合计”控件的可见性属性为“否”,而主窗体的“总计”控件引用该控件的值。,“合计”控件,例五,“客户标签对话框”窗体参数窗体。 (see Figure 5-15) 目的:为预览或打印“客户标签”报表提供参数。,Figure 5-15 “客户标签对话框”窗体,其中,“预览”按钮的“单击”事件将运行(击活)宏:客户标签对话框.预览(see Figure 5-16)。 目的:根据用户在“打印标签”控件上所做的选择以预览所有客户标签或预览指定国家的客户标签。,Figure 5-16 “客户标签对话框”宏对象,例六,自定义的“参数查询-指定国家的客户”窗体参数窗体。 (see Figure 5-17) 目的:为运行“参数查询-指定国家的客户”查询提供参数。,Figure 5-17 自定义的“参数查询-指定国家的客户”窗体,例七,改进自定义的“参数查询-指定国家的客户”窗体。 (see Figure 5-18) 目的:使参数和结果在同一个窗体中。,Figure 5-18 改进的“参数查询-指定国家的客户”窗体,“指定国家的客户”窗体作为子窗体,做法:1、根据查询“参数查询-指定国家的客户”建立一个窗体“指定国家的客户” (see Figure 5-19),Figure 5-19 “指定国家的客户”窗体数据表式,做法: 2、在“参数查询-指定国家的客户”窗体中建立子窗体控件(以现有的“指定国家的客户”窗体来建立); 3、利用参数文本框“国家”的更新后事件属性,执行如下代码: Private Sub 国家_AfterUpdate() Me!指定国家的客户.Requery End Sub 其目的是每当“国家”控件的值更新后,子窗体控件指定国家的客户的数据来源刷新( Requery :再查询),从而实现每选一个国家,子窗体中显示该国家的客户。 作业:在“季度订单”窗体中增加“年度”参数控件,该控件的默认值为1995,行来源为“订单”表中的所有年度。目的:每当重新选择了“年度”值后,窗体中显示该年度相关的信息。,例八, “各年销售额对话框”窗体受限的参数窗体。 (see Figure 5-20) 目的:为“各年销售额”报表提供参数,并且由该报表激活。,Figure 5-20 “各年销售额对话框”窗体,“各年销售额对话框”窗体运行情况: 1、若“各年销售额”报表打开则可以正常工作,提供参数,并且由该报表激活; 2、若“各年销售额”报表未打开(预览或打印),按“确定”按钮时会出现如下的提示信息:,这是在“确定”按钮的单击事件中设计了如下页所示的代码。,Private Sub 确定_Click() On Error GoTo Err_OK_Click Dim strMsg As String, strTitle As String Dim intStyle As Integer 如果“各年销售额”报表未被打开用于预览或打印,导致产生一个错误。 (仅当报表的 Open 事件被执行时,blnOpening 变量的才为 true。 If Not Reports!各年销售额.blnOpening Then Err.Raise 0 Me.Visible = False 隐藏窗体。 Exit_OK_Click: Exit Sub Err_OK_Click: strMsg = “要使用此窗体,您必须从数据库窗口或设计视图中预览或打印” strMsg = strMsg & “ “各年销售额“报表。“ intStyle = vbOKOnly strTitle = “从报表中打开“ MsgBox strMsg, intStyle, strTitle Resume Exit_OK_Click End Sub,
展开阅读全文