Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-查询/SQL语句

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

时 间:2012-06-04 09:46:13
作 者:周芳   ID:24526  城市:上海
摘 要:当你的用户输入一个组合框你可以使用连续过滤或拆分窗体来完成。根据用户的输入,窗体过滤器将更新以显示完整的或部分的来匹配输入的值。
正 文:

原作者: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交流群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助