我正在构建一个系统,用于检查某个项目是否接近其到期日期。
我仍然使用find方法收到错误。错误说,无效的属性值
Private Sub Command1_Click()
Dim strCurrDate As String
strCurrDate = Format(Date, "MM/dd/yyyy")
Dim list As ListItem
Dim x As Integer
ConnectDB
rs.Open "Select * from Table1 Order by Expiry ASC", db, 3, 3
Do Until rs.EOF
Set list = ListView1.ListItems.Add(, , rs(0))
For x = 1 To 3
list.SubItems(x) = rs(x)
Next x
rs.MoveNext
Loop
Set rs = Nothing
db.Close: Set db = Nothing
ListView1.ListItems.Clear
Set list = Me.ListView1.FindItem(strCurrDate, lvwSubItem, 1, lvwPartial) ' Error here
If Not list Is Nothing Then
'Select the row
list.Selected = True
'Auto scrolling the Scrollbar if we have so much rows
'and not show on ListView
myList.EnsureVisible
MsgBox "Data Found:"
Else
MsgBox "Data not Found"
End If
End Sub
答案 0 :(得分:0)
使用FindItem方法:
Dim xItem As ListItem Set xItem = ListView1.FindItem("01/01/1900", lvwSubItem, 1, lvwPartial) If Not xItem Is Nothing Then 'Condition when item is not found End If
答案 1 :(得分:0)
这个样本:
Private Sub Form_Load()
With ListView1
.HideSelection = False
End With
With ListView1.ColumnHeaders
.Add , , "ID", 500
.Add , , "Product Name", 1500
.Add , , "Current Stock", 1200
.Add , , "Expiration Date", 1500
End With
'just assume we have 3 records
Dim myList As ListItem
Set myList = Me.ListView1.ListItems.Add(, , "001")
With myList
.SubItems(1) = "Product One"
.SubItems(2) = "10"
.SubItems(3) = "02/01/2014"
End With
Set myList = Me.ListView1.ListItems.Add(, , "002")
With myList
.SubItems(1) = "Product Two"
.SubItems(2) = "11"
.SubItems(3) = "03/27/2014"
End With
Set myList = Me.ListView1.ListItems.Add(, , "003")
With myList
.SubItems(1) = "Product Three"
.SubItems(2) = "12"
.SubItems(3) = "01/28/2014" 'Current date
End With
End Sub
Private Sub Command1_Click()
Dim strCurrDate As String
Dim myList As ListItem
'Formating date to "MM/dd/yyyy", eg. 01/28/2014
strCurrDate = Format(Date, "MM/dd/yyyy")
'Finding item
Set myList = Me.ListView1.FindItem(strCurrDate, lvwSubItem, 1, lvwPartial)
'If we got the item
If Not myList Is Nothing Then
'Select the row
myList.Selected = True
'Auto scrolling the Scrollbar if we have so much rows
'and not show on ListView
myList.EnsureVisible
MsgBox "Data Found: " & vbCrLf & _
"ID: " & myList.Text & vbCrLf & _
"Current Stock: " & myList.SubItems(2) & vbCrLf & _
"Expiration Date: " & myList.SubItems(3)
Else
MsgBox "Data not Found"
End If
End Sub
答案 2 :(得分:0)
为什么要清除列表视图?
ListView1.ListItems.Clear
请删除它