Vba 若组合框值为Sample,则搜索与Sample匹配的列
我正在尝试创建搜索功能,它允许用户从组合框中选择所需的列值(例如,名称),键入他们正在搜索的内容(即Alex),然后按搜索按钮,程序将选择“Name”列中包含“Alex”的所有行 这就是我到目前为止所做的,With部分在没有If语句的情况下工作,所以我想我做错了什么。lngColumn和lngRow最初设置为Long,但它给了我无效的null error用法,所以我将它们更改为variantVba 若组合框值为Sample,则搜索与Sample匹配的列,vba,ms-access,Vba,Ms Access,我正在尝试创建搜索功能,它允许用户从组合框中选择所需的列值(例如,名称),键入他们正在搜索的内容(即Alex),然后按搜索按钮,程序将选择“Name”列中包含“Alex”的所有行 这就是我到目前为止所做的,With部分在没有If语句的情况下工作,所以我想我做错了什么。lngColumn和lngRow最初设置为Long,但它给了我无效的null error用法,所以我将它们更改为variant Private Sub cmdSearch_Click() Dim strSearch As Varia
Private Sub cmdSearch_Click()
Dim strSearch As Variant, lngColumn As Variant, lngRow As Variant
Dim strValue As String
strSearch = Me.txtSearch
strValue = Me.cboSearch
If strValue = "Name" Then
lngColumn = Me.lstTest.Column(1)
ElseIf strValue = "Occupation" Then
lngColumn = Me.lstTest.Column(2)
ElseIf strValue = "Location" Then
lngColumn = Me.lstTest.Column(3)
With Me.lstTestReports
For lngRow = 0 To .ListCount - 1
If (.Column(lngColumn, lngRow)) = strSearch Then
.Selected(lngRow) = True
Else
.Selected(lngRow) = False
End If
Next
End With
End If
End Sub只要您在
.Column(lngColumn, lngRow)
稍后,所以不能存储listbox值,而是存储列号
另外,带有Me.lstestreports的部分不属于If
Private Sub cmdSearch_Click()
Dim strSearch As Variant, lngColumn As Long, lngRow As Long
Dim strValue As String
strSearch = Me.txtSearch
strValue = Me.cboSearch
If strValue = "Name" Then
lngColumn = 1
ElseIf strValue = "Occupation" Then
lngColumn = 2
ElseIf strValue = "Location" Then
lngColumn = 3
End If
With Me.lstTestReports
For lngRow = 0 To .ListCount - 1
If (.Column(lngColumn, lngRow)) = strSearch Then
.Selected(lngRow) = True
Else
.Selected(lngRow) = False
End If
Next
End With
这应该在ms access中吗?