Google Answers Logo
View Question
 
Q: VB6 & DateDiff function ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: VB6 & DateDiff function
Category: Computers > Programming
Asked by: rbrian2-ga
List Price: $50.00
Posted: 03 Dec 2002 08:54 PST
Expires: 02 Jan 2003 08:54 PST
Question ID: 118472
I have a piece of code which I need to modify:

If Status = "History" Then
        If (DateDiff("d", CurentDate,
CDate(lstVersions.ListColumnText(nIndex, 3)))) < -182 Then bOldEnough
= True
end if

Where lstVersions.ListColumnText in my case is "History"

cmdDelete.Enabled = True

This code is allowing delete a history version of database through our
application in VB.
This piece of code is checking if the difference between current date
and previous (desired to be deleted) is not less than 6 month old and
if it is greater, then button “Delete” becomes enabled to delete the
history version

I need to add a condition to allow delete history versions which 
1. to delete ANY history versions, if there are more than 20 versions
in the  history list (even if some of them are less than 1 month old).
2. When deleting history versions less than 6 month old and there are
less than 20 versions on the system, give a warning msgbox.

Appreciate any help!
Brian

Request for Question Clarification by hammer-ga on 03 Dec 2002 10:15 PST
So, there is a list box with a list of History versions, right?

1. Can you select more than one history version at a time for
deletion?
2. If there are more than 20 history versions available, you can
delete any one you want, regardless of age?
3. I don't see any way for your condition #2 to actually occur. If
there are less than 20 versions available, your current code prevents
the deletion of any version less than 6 months old. Under what
condition would you actually need a MsgBox to appear?
Answer  
Subject: Re: VB6 & DateDiff function
Answered By: hammer-ga on 04 Dec 2002 06:38 PST
Rated:5 out of 5 stars
 
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

Clarification of Answer by hammer-ga on 04 Dec 2002 06:40 PST
Please note that the answer box has wrapped the DateDiff and the
MsgBox statements. These should both be one line.

- Hammer
rbrian2-ga rated this answer:5 out of 5 stars and gave an additional tip of: $50.00
Thank you hammer-ga very much for your help! Your info and code are
great! They helped and decided my problem complitely!  Have a great
day!  Brian.

Comments  
Subject: Re: VB6 & DateDiff function
From: iso8601-ga on 13 Dec 2002 15:52 PST
 
Are those dates for the first of the month, and in MM/DD/YYYY format?

Europe has used DD/MM/YYYY for many years, which always creates
problems with the international flow of data.

Now there is an International Standard for dates, ISO 8601, which
removes the confusion by using only YYYY-MM-DD for dates and 24-hour
HH:MM:SS for times.

This is adopted all over the world. A Google web search will find a
lot more information on this.
Subject: Re: VB6 & DateDiff function
From: rbrian2-ga on 16 Dec 2002 08:45 PST
 
Our application supports the international standarts. Thanks for the question.
Brian

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy