Pergunta

I'm building a system that will check if an item is near to its expiration date.

I still getting error using the find method. error says, invalid property value

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
Foi útil?

Solução

Why do you clear the listview?

ListView1.ListItems.Clear

Please remove it

Outras dicas

Use the FindItem methode:

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

This the sample:

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top