Here is some code. I have a form with a standard VB ListBox
(lstVersions) and a standard VB Command Button (cmdDelete). If you are
using third party controls, you will need to substitute their
properties and methods for those used by the standard VB controls.
Form Load:
Populates the list box with 24 dates.
'*********************************
Private Sub Form_Load()
With lstVersions
.AddItem "12/01/2002"
.AddItem "11/01/2002"
.AddItem "10/01/2002"
.AddItem "09/01/2002"
.AddItem "08/01/2002"
.AddItem "07/01/2002"
.AddItem "06/01/2002"
.AddItem "05/01/2002"
.AddItem "04/01/2002"
.AddItem "03/01/2002"
.AddItem "02/01/2002"
.AddItem "01/01/2002"
.AddItem "12/01/2001"
.AddItem "11/01/2001"
.AddItem "10/01/2001"
.AddItem "09/01/2001"
.AddItem "08/01/2001"
.AddItem "07/01/2001"
.AddItem "06/01/2001"
.AddItem "05/01/2001"
.AddItem "04/01/2001"
.AddItem "03/01/2001"
.AddItem "02/01/2001"
.AddItem "01/01/2001"
.Refresh
End With
End Sub
' *********************************
Function: OKToDelete
Tests the selected date against the required conditions of
20 or more versions and version age. Returns a boolean indicating
whether the conditions for deletion are met.
' *********************************
Private Function OKToDelete() As Boolean
Dim version_count As Integer
Dim bOldEnough As Boolean
bOldEnough = False
' Determine how many versions we have
version_count = lstVersions.ListCount
If version_count > 19 Then
' We have at least 20 versions, so we can
' delete anything we want
bOldEnough = True
Else
If DateDiff("d", Date,
CDate(lstVersions.List(lstVersions.ListIndex))) < -182 Then
bOldEnough = True
End If
End If
OKToDelete = bOldEnough
End Function
' *******************************
Click event of ListBox:
Enables or disables cmdDelete based on item selected
' *******************************
Private Sub lstVersions_Click()
Dim bOldEnough As Boolean
bOldEnough = False
' Disable the Delete button to start
cmdDelete.Enabled = False
bOldEnough = OKToDelete()
If bOldEnough = True Then
cmdDelete.Enabled = True
End If
End Sub
' ********************************
Click Event of Command Button:
Per your request, this again tests the conditions and pops
up a message box on failure. This is included as an example of
technique. Currently, the conditions that enable or disable the
command button prevent the message from ever appearing.
' ********************************
Private Sub cmdDelete_Click()
Dim response As Integer
Dim bOldEnough As Boolean
bOldEnough = False
bOldEnough = OKToDelete()
If bOldEnough = False Then
response = MsgBox("This version is less than 6 months old.
Delete it anyway?", vbYesNoCancel)
Else
response = vbYes
End If
If response = vbYes Then
' Do whatever you do to delete a Version!
With lstVersions
.RemoveItem (lstVersions.ListIndex)
.ListIndex = -1
.Refresh
End With
End If
End Sub
' *******************************
Please request clarification if you need further explanation on any of
the above code. Good luck with your project!
- Hammer |