Ms access 大型数据库的MS Access搜索表单

Ms access 大型数据库的MS Access搜索表单,ms-access,search,vba,Ms Access,Search,Vba,我正在构建一个能够容纳大量记录的数据库,所以我想要一个在服务器上最简单的搜索功能。我正在使用下面的代码,但我知道这对于一个更大的数据库来说是不可持续的。它查看搜索框并运行查询以缩小搜索结果范围: Private Sub SearchFor_Change() 'Create a string (text) variable Dim vSearchString As String 'Populate the string variable with the text entered in the

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

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
Private Sub search for_Change()
'创建字符串(文本)变量
Dim vSearchString作为字符串
'使用在文本框中输入的文本填充字符串变量以搜索
vSearchString=SearchFor.Text
'将字符串变量中包含的值传递给隐藏的文本框SrchText,
'用作查询QRY_SearchAll的sear4ch条件
SrchText.Value=vSearchString
'重新查询列表框以显示在文本框中输入的文本的最新结果
“寻找
Me.SearchResults.Requery
'测试尾随空格,并在此时退出子程序
'以便保留尾随空间,如果焦点从
'文本框搜索
如果Len(Me.SrchText)0和InStr(Len(SrchText),SrchText,“,vbTextCompare),则
'将焦点设置在列表框中的第一项上
Me.SearchResults=Me.SearchResults.ItemData(1)
Me.SearchResults.SetFocus
'重新查询表单以刷新可能正在输入的任何未绑定文本框的内容
'关闭列表框的记录源
文件查询
'将光标返回到文本框SearchFor中文本的末尾,
'并恢复焦点转移到列表框时丢失的尾随空间
Me.SearchFor=vSearchString
Me.SearchFor.SetFocus
Me.SearchFor.SelStart=Me.SearchFor.SelLength
出口接头
如果结束
'将焦点设置在列表框中的第一项上
Me.SearchResults=Me.SearchResults.ItemData(1)
Me.SearchResults.SetFocus
'重新查询表单以刷新任何未绑定文本框的内容,该文本框可能
'从列表框的记录源馈送
文件查询
'将光标返回到文本框SearchFor中文本的末尾
Me.SearchFor.SetFocus
如果不是IsNull(Len(Me.SearchFor)),那么
Me.SearchFor.SelStart=Len(Me.SearchFor)
如果结束
理想情况下,我需要一个具有多个搜索字段的表单,以及一个运行查询以在列表框中返回结果的“查找”按钮

我也不知道如何设置它,以便当用户双击搜索结果中的一个选择时,所选记录以编辑模式打开


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

首先,你在一篇帖子中问了两个问题。我建议您删除第二个关于双击以编辑模式打开选择的问题

据我所知,您关心的是当前代码的性能以及它所提供的功能或灵活性的缺乏

关于绩效:

  • 不要使用更改方法执行筛选。如果确实要使用change方法,请仅将计时器间隔设置为500(ms)左右,然后对计时器事件执行筛选。这是在用户停止键入半秒钟后才会出现的筛选器
  • 避免“模糊”搜索(在文本字段中使用星号/百分比)。看起来你现在没在用它们。虽然模糊搜索通常会使软件更加用户友好,但当它们对性能造成重大影响时,会使软件变得不那么用户友好
  • 在处理大量数据时,大多数性能提升都来自于仔细地重新调整应用程序的工作方式,升级到SQL Server,以及将服务器和网络升级到更好的硬件。当使用JET/ACE后端数据库容器时,您只能改进这么多。与使用JET/ACE的DAO相比,使用ADO和ODBC链接表的SQL Server都提供了一些优势。ODBC链接表提供了延迟加载,而ADO提供了诸如断开连接的记录集之类的功能,这些记录集可以在不向服务器进行额外回调的情况下进行过滤(这有一些限制)
  • 如上所述,您可能需要仔细反思应用程序的工作方式和设计方式。最好尽量限制所需的复杂查询量和允许/需要的基于文本的搜索量。使用更多查找/参考表。而不是将类思考类存储为文本,而是考虑将它们存储为长号分类ID。对索引数字字段的查询通常比对基于文本的字段的查询性能更好,尤其是在查询中使用带星号的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
Private Sub-cmdSearch\u Click()
调用SetRowSource
端接头
私有子txtSearch_AfterUpdate()
调用SetRowSource
端接头
私有子cboCategoryID_AfterUpdate()
调用SetRowSource
端接头
私有子txtBrand_AfterUpdate()
调用SetRowSource
端接头
私有子集合rowsource()
将sSQL设置为字符串
sSQL=“从tblItems中选择项目ID、描述、品牌”
sSQL=sSQL&GetWhere
Me.lstSearchResults.RowSource=sSQL
端接头
私有函数GetWhere()作为字符串
像绳子一样在这里摇曳
如果Nz(Me.cboCategoryID,0)0,则
sWhere=sWhere&“CategoryID=“&Me.cboCategoryID&”和
如果结束
如果Nz(Me.txtSearch,“”),则
sWhere=sWhere&“类似于“*”的描述&Replace(Me.txtSearch、“”)&“*”和”
如果结束
如果新西兰(Me.txtBrand,“”),则
sWhere=sWhere&“Brand=”&替换(Me.txtBrand、“”、“”)&“AND”
如果结束
如果这里是“”,那么