【译文】使用一个组合框来搜索你输入的内容-周芳
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-查询/SQL语句


【译文】使用一个组合框来搜索你输入的内容

发表时间:2012/6/4 9:46:13 评论(3) 浏览(18576)  评论 | 加入收藏 | 复制
   
摘 要:当你的用户输入一个组合框你可以使用连续过滤或拆分窗体来完成。根据用户的输入,窗体过滤器将更新以显示完整的或部分的来匹配输入的值。
正 文:

原作者:Emily Warn 翻译:周芳


【译文】使用一个组合框来搜索你输入的内容

 这里有一个巧妙的花招,当你的用户输入一个组合框你可以使用连续过滤或拆分窗体来完成。根据用户的输入,窗体过滤器将更新以显示完整的或部分的来匹配输入的值。


       这个示例使用一个略微的修改版Northwind 2007模板,微软上可供下载。“Customer List”窗体是一个拆分窗体,显示一个目录中的所有客户。我们想让用户过滤该列表来轻松地找到客户,即使不知道完整的客户名称。


       要做到这一点,我们将添加一个组合框命名为cboFilter,使用以下显著的属性:


以下是我们的窗体的截图:


然后在改变事件的组合框,我们添加以下代码:

Private Sub cboFilter_Change()

  ' 如果组合框被清空,那就清空窗体的过滤器

  If Nz(Me.cboFilter.Text) = "" Then
    Me.Form.Filter = ""
    Me.FilterOn = False
   
 ' 如果是查找到一条组合框项,那就显示一个精确的匹配。
  ' 如果这个值是在列表里,就使用ListIndex属性进行检索。

  ElseIf Me.cboFilter.ListIndex <> -1 Then
    Me.Form.Filter = "[Company] = '" & _
                     Replace(Me.cboFilter.Text, "'", "''") & "'"
    Me.FilterOn = True
  
  Else
    Me.Form.Filter = "[Company] Like '*" & _
                     Replace(Me.cboFilter.Text, "'", "''") & "*'"
    Me.FilterOn = True

  End If
 
  Me.cboFilter.SetFocus
  Me.cboFilter.SelStart = Len(Me.cboFilter.Text)

End Sub

        现在,根据用户的输入,客户列表将自动更新。如果用户键入一个精确的匹配,这份名单是经过过滤的精确匹配。如果用户类型部分匹配,这份名单是经过过滤出的是其名称包含字符串的任何公司。如果用户清除组合框,过滤器被清除。

        注意这同样的事情可以用一个文本框来完成,但使用组合框可以添加额外的在下拉框里面的值。
 

【原文】Using a Combo Box to search as you type

    Here's a neat trick that you can use to filter a Continuous or Split form while your users are typing in a Combo Box. As the user types, the form filter updates to display full or partial matches for the value entered. 

    This example uses a slightly modified version of the Northwind 2007 Template, available for download from Microsoft. The "Customer List" form is a Split form that displays a list of all customers. We want to allow users to filter this list to easily find a customer, even without knowing the full customer name.

    To do this, we add a Combo Box named cboFilter, with the following notable properties:

    Here's what our form looks like:

    Then in the Change event of the Combo Box, we add the following code:

    Private Sub cboFilter_Change()

  ' If the combo box is cleared, clear the form filter.
  If Nz(Me.cboFilter.Text) = "" Then
    Me.Form.Filter = ""
    Me.FilterOn = False
   
  ' If a combo box item is selected, filter for an exact match.
  ' Use the ListIndex property to check if the value is an item in the list.
  ElseIf Me.cboFilter.ListIndex <> -1 Then
    Me.Form.Filter = "[Company] = '" & _
                     Replace(Me.cboFilter.Text, "'", "''") & "'"
    Me.FilterOn = True
   
  ' If a partial value is typed, filter for a partial company name match.
  Else
    Me.Form.Filter = "[Company] Like '*" & _
                     Replace(Me.cboFilter.Text, "'", "''") & "*'"
    Me.FilterOn = True

  End If
 
  ' Move the cursor to the end of the combo box.
  Me.cboFilter.SetFocus
  Me.cboFilter.SelStart = Len(Me.cboFilter.Text)

End Sub
 
    Now as the user types, the customer list automatically updates. If the user types an exact match, the list is filtered for an exact match. If the user types a partial match, the list is filtered for any company whose name contains the string. If the user clears the combo box, the filter is cleared.

    Note that this same thing can be done with a text box, but using a combo box adds the extra perk of making all values available in the drop down.


Access软件网交流QQ群(群号:198465573)
 
 相关文章
有效利用组合框传递管理信息   【竹笛  2004/4/14】
组合框模糊筛选  【钱玉伟  2007/6/26】
组合框的模糊筛选  【咱家是猫  2007/11/28】
组合框上边输入边模糊筛选  【咱家是猫  2009/3/12】
组合框模糊查询实例  【一杯绿茶  2010/11/13】
组合框模糊筛选改进  【小胡  2011/5/25】
Access查询 交流QQ群群号:54525238 欢迎学习acc...  【宏鹏  2012/10/26】
常见问答
技术分类
相关资源
文章搜索
关于作者

周芳

文章分类

文章存档

友情链接