我正在尝试修改发布在网络https://www.microsoft.com/en-us/microsoft-365/blog/2012/05/03/using-a-combo-box-to-search-as-you-type/上的代码
想法是通过查找人员的姓氏来在MS Access表单中查找记录。
我的VBA如下,但出现编译错误:语法错误消息
Private Sub cboLastNameFind_Change()
' If the combo box is cleared, clear the form filter.
If Nz(Me.cboLastNameFind.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.cboLastNameFind.ListIndex <> -1 Then
Me.Form.Filter = "[LastName] = '" &
Replace(Me.cboLastNameFind.Text, "'", """) & "‘"
Me.FilterOn = True
' If a partial value is typed, filter for a partial company name match.
Else
Me.Form.Filter = "[LastName] Like '*" & _
Replace(Me.cboLastNameFind.Text, "'", """) & "*‘"
Me.FilterOn = True
End If
' Move the cursor to the end of the combo box.
Me.cboLastNameFind.SetFocus
Me.cboLastNameFind.SelStart = Len(Me.cboLastNameFind.Text)
End Sub
问题似乎出在Me.Form.Filter =“ [LastName] ='”&(至少,错误消息突出显示了该问题。任何想法和解决方法都非常受欢迎。
谢谢, 西蒙
答案 0 :(得分:1)
很明显,您所引用的页面包含由文本处理器引起的多个错误(右单引号或左单引号应使用撇号,而双引号应使用两个单引号)。
永远不要在与Word之类的文本处理器之间复制粘贴代码。显然微软犯了这个错误。
更正后的代码如下:
Private Sub cboLastNameFind_Change()
' If the combo box is cleared, clear the form filter.
If Nz(Me.cboLastNameFind.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.cboLastNameFind.ListIndex <> -1 Then
Me.Form.Filter = "[LastName] = '" &
Replace(Me.cboLastNameFind.Text, "'", "''") & "'"
Me.FilterOn = True
' If a partial value is typed, filter for a partial company name match.
Else
Me.Form.Filter = "[LastName] Like '*" & _
Replace(Me.cboLastNameFind.Text, "'", "''") & "*'"
Me.FilterOn = True
End If
' Move the cursor to the end of the combo box.
Me.cboLastNameFind.SetFocus
Me.cboLastNameFind.SelStart = Len(Me.cboLastNameFind.Text)
End Sub
答案 1 :(得分:0)
不确定您是否知道;通过查找人员的姓氏来在MS Access表单中查找记录是Combobox控件的标准功能。当您将其拖动到表单中时,向导弹出窗口会为您提供查找选项-其中之一是从绑定记录源中查找表单的记录。