表单展示界示例_在表单上过滤的示例。

表单展示界示例

第1章-目录表(包括附加的数据库) 第2章-简介 第3章-表布局 第4章-表单布局 第5章-表单模块 第6章-代码讨论(过滤器控件) 第7章-代码讨论(REST)

-------------------------------------------------- ------------------------------

第2章-简介

这是一个基本示例,并且可以在没有复杂的SubForms或其他任何东西的情况下,以其他方式绑定表单上的一些未绑定选择项。

在此示例中,我将一个名为frmAccount的表单绑定到表tblAccount。

表单具有显示为(绑定)控件的所有表字段(主要是TextBox-名称以'txt'开头,还有ComboBox-cboAccountType)。 它还具有三个额外的控件,使操作员可以通过AccountCode,CreationDate和AccountType选择项目。 选择这些字段仅出于说明目的,不应准确反映任何现实情况。

为避免混淆,最好选择具有含义的名称,以反映其内容,同时避免使用特殊字符(包括空格)和已经用于其他用途的名称。 后者的一个很好的例子是“日期”。 在等待中将其用作字段或控件名称是一个问题。

-------------------------------------------------- ------------------------------

第3章-表格布局
 Table Name=tblAccount 
AccountID; Autonumber; PK
AccountCode; String
AccountName; String
CreationDate; Date/Time
AccountType; Numeric; 0==>Control (Internal Usage); 1==>Supplier; 2==>Customer
SalesThisYear; Numeric
-------------------------------------------------- ------------------------------ 第4章-表单布局
 Form Name=frmAccount 
txtFindCode; Unbound
txtFindCreationDate; Unbound
cboFindAccountType; Unbound
txtAccountID; Bound to tblAccount.AccountID
txtAccountCode; Bound to tblAccount.AccountCode
txtAccountName; Bound to tblAccount.AccountName
txtCreationDate; Bound to tblAccount.Creation
cboAccountType; Bound to tblAccount.AccountType
txtSalesThisYear; Bound to tblAccount.SalesThisYear
-------------------------------------------------- ------------------------------ 第5章-表格模块
Option Compare Database
Option Explicit 
Private Sub txtFindAccountCode_AfterUpdate()
    Call CheckFilter
End Sub 
Private Sub txtFindCreationDate_AfterUpdate()
    Me!txtFindCreationDate = IIf(IsDate(Me!txtFindCreationDate), _
                                 Format(Me!txtFindCreationDate, "d mmm yyyy"), _
                                 "")
    Call CheckFilter
End Sub 
Private Sub cboFindAccountType_AfterUpdate()
    Call CheckFilter
End Sub 
'CheckFilter produces the new Filter depending on the values currently in
'txtFindAccountCode, txtFindCreationDate & cboFindAccountType.
Private Sub CheckFilter()
    Dim strFilter As String, strOldFilter As String 
    strOldFilter = Me.Filter
    'txtFindAccountCode - Text
    If Me!txtFindAccountCode > "" Then _
        strFilter = strFilter & _
                    " AND ([AccountCode] Like '" & _
                    Me!txtFindAccountCode & "*')"
    'txtFindCreationDate - Date
    If Me!txtFindCreationDate > "" Then _
        strFilter = strFilter & _
                    " AND ([CreationDate]=" & _
                    Format(CDate(Me!txtFindCreationDate), _
                           "\#m/d/yyyy\#") & ")"
    'cboFindAccountType - Numeric
    If Me!cboFindAccountType > "" Then _
        strFilter = strFilter & _
                    " AND ([AccountType]=" & _
                    Me!cboFindAccountType & ")"
    'Debug.Print ".Filter = '" & strOldFilter & "' - ";
    'Debug.Print "strFilter = '" & strFilter & " '"
    'Tidy up results and apply IF NECESSARY
    If strFilter > "" Then strFilter = Mid(strFilter, 6)
    If strFilter <> strOldFilter Then
        Me.Filter = strFilter
        Me.FilterOn = (strFilter > "")
    End If
End Sub
-------------------------------------------------- ------------------------------ 第6章-代码讨论(过滤器控件)

每个控件的过滤器部件代码均以其开头为基础,以检查其是否为空。 选中大于“”的任何值(文本,日期或数字)将返回true。

每个都以“ AND”开头,但是与后来应用之前找到的第一个相比,这是去除的。

构建的每个检查都用括号()包围,以确保逻辑与其他检查不混淆。

文本

文本文字的分隔符是(')。

文本检查是作为“类似”构造完成的,因为帐户代码可能是唯一的,因此=作为复合检查不能与其他代码很好地混合。 我们从字符串“ AND([AccountCode] Like'”开始,然后添加(附加字符串)来自txtFindAccountCode控件的值,然后(最后)添加字符串“ *”)。 使用txtFindAccountCode =“ ABC”,结果为:

 AND ([AccountCode] Like 'CUS*')
这将找到[AccountCode]以“ CUS”开头的任何记录。 日期

日期文字的分隔符是(#)。

首先,为了描述对txtFindCreationDate控件的操作以返回版本,我们可以有用地传递给SQL。 在代码中显示为“ Format(CDate(Me!txtFindCreationDate),“ \#m / d / yyyy \#”)&“)”。 由于我们使用TextBox输入日期,因此我们几乎无法控制操作员输入的内容。 他们可以输入“ 2007年2月1日”,也可以输入英格兰的“ 1/2/2007”或美国的“ 2/1/2007”。 无论输入什么,CDate()都会使用本地设置将其转换为有效的日期/时间值。 SQL要求任何日期文字都必须明确,但是坚持要求使用美国短日期格式(m / d / y),而不要使用本地日期格式。 阅读(

文字日期时间及其定界符(#)。 )进行更详细的说明。

因此,我们以字符串“ AND([CreationDate] =“然后添加(字符串附加)”来自txtFindCreationDate控件的值开始,但首先(通过上面的解释)通过几个函数,然后(最后)添加字符串“)”。 使用txtFindCreationDate =“ 2007年2月1日”,结果为:

 AND ([CreationDate]=#2/1/2007#)
它将找到[CreationDate]为2007年2月1日的任何记录。NUMBER

数字文字不需要分隔符。

我们以字符串“ AND([AccountType] =”然后从txtFindAccountType控件中添加(字符串附加)”值开始,然后(最后)添加字符串“)”。 使用txtFindAccountType =“ 1”,结果为:

 AND ([AccountType]=1)
这将找到[AccountType]为1(客户)的任何记录。

-------------------------------------------------- ------------------------------

第7章- 更新事件后的 代码讨论(REST)

前三个过程都调用相同的子例程(CheckFilter),因为每当需要重新构建整个过滤器时,

任何组成部分均已更改。

为了始终显示(逻辑上没有必要),我们检查在txtFindCreationDate中输入的数据。 如果是有效日期,则将其格式设置为“ d mmm yyyy”。 否则将被重置。

我们将详细介绍过滤器的每个控件部分,但首先,我注意到许多人在区分VBA处理的内容和传递给SQL引擎(解释器)的内容方面存在问题。 它可以帮助您了解是否取消注释

从“查找”控件中选择一个或多个值后, “调试。打印行并查看结果。 这介于VBA处理和SQL之间,因此在该阶段可能会有所帮助。 应用过滤器

首先,从开始剥离第一个“与”后,我们检查过滤器是否确实与当前过滤器不同。

如果是的话,我们需要设置过滤器,并根据strFilter是否为空来确定是否应使用过滤器(.FilterOn)。

-------------------------------------------------- ------------------------------

附加的文件
文件类型:zip FormFiltering.Zip (16.3 KB,8861视图)

翻译自: https://bytes.com/topic/access/insights/590551-example-filtering-form

表单展示界示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值