0

我正在构建一个可以容纳大量记录的数据库,所以我想要一个在服务器上最简单的搜索功能。我正在使用以下代码,但我知道它对于更大的数据库是不可持续的。它正在查看搜索框并运行查询以缩小搜索结果:

Private Sub SearchFor_Change()
'Create a string (text) variable
Dim vSearchString As String

'Populate the string variable with the text entered in the Text Box SearchFor
vSearchString = SearchFor.Text

'Pass the value contained in the string variable to the hidden text box SrchText,
'that is used as the sear4ch criteria for the Query QRY_SearchAll
SrchText.Value = vSearchString

'Requery the List Box to show the latest results for the text entered in Text Box
'SearchFor
Me.SearchResults.Requery


'Tests for a trailing space and exits the sub routine at this point
'so as to preserve the trailing space, which would be lost if focus was shifted from   
'Text Box SearchFor
 If Len(Me.SrchText) <> 0 And InStr(Len(SrchText), SrchText, " ", vbTextCompare) Then
 'Set the focus on the first item in the list box
 Me.SearchResults = Me.SearchResults.ItemData(1)
 Me.SearchResults.SetFocus
 'Requery the form to refresh the content of any unbound text box that might be feeding 
 'off the record source of  the List Box
 DoCmd.Requery
 'Returns the cursor to the the end of the text in Text Box SearchFor,
 'and restores trailing space lost when focus is shifted to the list box
  Me.SearchFor = vSearchString
  Me.SearchFor.SetFocus
  Me.SearchFor.SelStart = Me.SearchFor.SelLength

  Exit Sub
  End If
  'Set the focus on the first item in the list box
   Me.SearchResults = Me.SearchResults.ItemData(1)
   Me.SearchResults.SetFocus

   'Requery the form to refresh the content of any unbound text box that might be 
   'feeding off the record source of  the List Box
  DoCmd.Requery

'Returns the cursor to the the end of the text in Text Box SearchFor
 Me.SearchFor.SetFocus

If Not IsNull(Len(Me.SearchFor)) Then
Me.SearchFor.SelStart = Len(Me.SearchFor)
End If

理想情况下,我想要一个具有多个搜索字段的表单,以及一个运行查询以在列表框中返回结果的“查找”按钮。

我也不确定如何设置它,以便当用户双击搜索结果中的选择时,所选记录以编辑模式在表单中打开。

任何帮助将不胜感激,谢谢!

4

1 回答 1

2

首先,您在一篇文章中提出了两个问题。我建议您提出第二个问题,即双击在编辑模式下打开选择。

据我所知,您担心当前代码的性能以及它提供的功能或灵活性不足。

关于性能:

  • 不要使用 change 方法来执行过滤器。如果您确实想使用 change 方法,请仅使用它来将计时器间隔设置为 500 (ms) 之类的值,然后对 Timer 事件执行过滤器。这是过滤器在用户停止输入半秒后才会出现。
  • 避免“模糊”搜索(在文本字段中使用星号/百分比)。你现在看起来不像在使用它们。虽然模糊搜索通常会使软件更加用户友好,但当它们对性能造成重大影响时,它们会降低用户友好性。
  • 在处理大量数据时,大部分性能提升来自仔细重构应用程序的工作方式、升级到 SQL Server,以及将服务器和网络升级到更好的硬件。使用 JET/ACE 后端数据库容器时,您只能改进这么多。带有 ADO 和 ODBC 链接表的 SQL Server 都比带有 JET/ACE 的 DAO 提供了一些优势。ODBC 链接表提供延迟加载,而 ADO 提供诸如断开连接的记录集之类的东西,无需额外回调服务器即可过滤(对此有限制)。
  • 正如上面已经提到的,您可能需要仔细重新考虑您的应用程序是如何工作的以及它是如何设计的。最好尝试限制所需的复杂查询数量以及允许/需要的基于文本的搜索数量。使用更多查找/参考表。与其将 think like category 存储为文本,不如考虑将它们存储为 Long Number CategoryID。对索引数字字段的查询通常比对基于文本的字段的查询执行得更好,尤其是当您在查询中使用带星号的 LIKE 时。

至于您的问题的其余部分(灵活性和功能),请考虑创建一个程序,该程序根据多个控件的值为您构建标准/where 语句。在像你这样的情况下,我的代码看起来像这样(如下)。请注意,我在描述搜索/过滤器中确实使用了星号(模糊搜索)。如果它表现不佳,您需要考虑将其删除并允许用户输入他们自己的星号。

Private Sub cmdSearch_Click()
    Call SetRowSource
End Sub

Private Sub txtSearch_AfterUpdate()
    Call SetRowSource
End Sub

Private Sub cboCategoryID_AfterUpdate()
    Call SetRowSource
End Sub

Private Sub txtBrand_AfterUpdate()
    Call SetRowSource
End Sub

Private Sub SetRowSource()
    Dim sSQL as String
    sSQL = "SELECT ItemID, Description, Brand FROM tblItems "
    sSQL = sSQL & GetWhere
    Me.lstSearchResults.RowSource = sSQL
End Sub

Private Function GetWhere() as String
    Dim sWhere as String
    If Nz(Me.cboCategoryID, 0) <> 0 Then
        sWhere = sWhere & "CategoryID = " & Me.cboCategoryID & " AND "
    End If
    If Nz(Me.txtSearch, "") <> "" Then
        sWhere = sWhere & "Description LIKE '*" & Replace(Me.txtSearch, "'", "''") & "*' AND "
    End If
    If Nz(Me.txtBrand, "") <> "" Then
        sWhere = sWhere & "Brand = '" & Replace(Me.txtBrand, "'", "''") & "' AND "
    End If
    If sWhere <> "" Then
        sWhere = Left(sWhere, Len(sWhere)-5)
        GetWhere = "WHERE " & sWhere
    End If
End Function

我认为我在 Access 社区中可能有点奇怪,但我通常不允许我的控件引用其他控件。在您的情况下,列表框中的 RowSource 引用了它所在表单的控件。出于多种原因,我更喜欢在 VBA 代码中构建我的 SQL 语句,尤其是当它们需要更改/过滤时。您可能会考虑做的另一件事是使用数据表表单而不是列表框。您可以设置表单的 RecordSource,然后将 WHERE 语句应用于表单的 Filter 属性。数据表表单对用户来说更加灵活,因为它们可以调整列的大小并进行排序,而无需程序员的任何帮助。您始终可以锁定控件,使其无法进行任何编辑。当我以这种方式使用数据表时,我认为使用 DoubleClick 事件来允许他们打开记录,

于 2013-10-05T14:11:43.900 回答