2014-01-26 19 views
0

我正在構建一個系統,用於檢查項目是否接近其到期日期。VB6.0將日期與列表視圖子項中的數據進行比較

我仍然使用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

爲什麼你清除列表視圖?

ListView1.ListItems.Clear

請刪除

+0

我添加了它,因爲它將顯示的記錄加倍。 甚至我刪除它。它仍然說沒有找到數據。 最新的問題? – Francis

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 
+0

我不明白這一點。你可以先向我解釋一下嗎? 什麼是「01/01/1900」,其餘的是什麼? – Francis

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 
+0

如何將我的整個數據庫設置爲ListItem?我打算使用循環將每個數據添加到ListItem中? – Francis

+0

我看到你正在調用一個程序「LoadData」。 –

+0

Set myList = Me.ListView1.FindItem(strCurrDate,lvwSubItem,1,lvwPartial) 這是我的問題。我無法理解這個邏輯。 :/ – Francis

相關問題