如何更改此 Access SQL 以选择相似而不是相同的记录

问题描述 投票:0回答:1

以下代码有效,当我输入数据时,下拉列表中填充的记录越来越少,直到只剩下一条记录,或者我单击以从少量记录中选择一条记录。但是有时我会点击错误的名称,例如John Jones 而不是 John Smith,然后 Comment_Enter() 代码只是用 John Jones 填充下拉菜单;如何更改代码,以便 LIKE 选择忽略记录字符串中的最后几个(或 x)个字符,用围绕我真正想要选择的记录的几条记录填充下拉列表?

Private Sub Comment_Change()

Dim strRowSource As String

   If Len(Me!Comment.Text) > 2 Then 'when more than 2 characters are entered the following code runs
        strRowSource = "SELECT DISTINCT [Purchase ledger].Comment FROM [Purchase ledger]"
        
        strRowSource = strRowSource & " WHERE Comment Like '"
        strRowSource = strRowSource & Me!Comment.Text
        strRowSource = strRowSource & "*'"
        
        Me!Comment.RowSource = strRowSource
        Me!Comment.Dropdown
   End If
End Sub

Private Sub Comment_Enter()

Dim strRowSource As String

    If Len(Me!Comment.Text) > 2 Then 'when more than 2 characters are entered the following code runs
        strRowSource = "SELECT DISTINCT [Purchase ledger].Comment FROM [Purchase ledger]"

        strRowSource = strRowSource & " WHERE Comment Like '"
        strRowSource = strRowSource & Me!Comment.Text
        strRowSource = strRowSource & "*'"
        
        Me!Comment.RowSource = strRowSource
        Me!Comment.Dropdown
    End If
End Sub

没什么,因为我不明白 LIKE 选择是如何工作的;我从其他地方改编的。

sql ms-access sql-like dropdownbox
1个回答
0
投票

为了严格回答你的问题,这个例子。 如果过滤字符串的长度超过 6 个字符,此代码会将与给定字符串减去最后 3 个字符相匹配的结果添加到搜索中:

   strRowSource = strRowSource & " WHERE Comment Like '"
   strRowSource = strRowSource & Me!Comment.Text
   strRowSource = strRowSource & "*' "
   if(not isnull(Me!Comment.Text) and len(Me!Comment.Text) > 6) then        
    strRowSource = strRowSource & " OR Comment Like '" & left(Me!Comment.Text, len(Me!Comment.Text)-3)
    strRowSource = strRowSource & "*' "
   end if
© www.soinside.com 2019 - 2024. All rights reserved.