James Kevin De Jesus
James Kevin De Jesus

Reputation: 19

VB6 - Expiration Date Logic

The code I wrote in VB6 is to check if the expiration date is still earlier than now, hence the product isn't expired. Otherwise, the product is expired. However, the product's details are always reused in the system.

So, my code is this:

Global database As Database
Global recordset As Recordset
Dim intResponse As Integer

Private Sub Form_Load()
Set database = OpenDatabase("C:\Location\Database.mdb")
Set recordset = database.OpenRecordset("Table")
recordset.Index("PrimaryKey")
End Sub

Private Sub txtProductID_LostFocus()
If txtProductID <> "" Then
    recordset.Seek "=", txtProductID
    If recordset.NoMatch Then
        MsgBox("Record not found!", vbInformation + vbOKOnly, "Record not found")
        txtProductID = ""
        txtProductID.SetFocus
    Else
        If recordset("Expiration_Date") > DateValue(Now) Then
            Enable_Details ' Sub Function
        Else
            intResponse = MsgBox("Product is expired!", vbExclamation + vbOKOnly, "Product is Expired")
            If intResponse = vbOK Then
                frmNewExpiryDate.Show
            End If 
        End If
    End If
End If
End Sub

When I debug the code, I cross-reference with the database and noticed that even if some products are expired, the code directly goes to Enable_Details sub. I need help!

Upvotes: 2

Views: 1495

Answers (3)

rashidnk
rashidnk

Reputation: 4292

Private Function IsExpiry() As Boolean

IsExpiry = False

'set your expiry date here
ExpiryYear = 2017
ExpiryMonth = 10
ExpiryDay = 10


    If Year(Date) > ExpiryYear Then
        IsExpiry = True
        GoTo last
    End If

    If Month(Date) > ExpiryMonth Then
         IsExpiry = True
         GoTo last
    End If


    If Month(Date) = ExpiryMonth Then
        If Day(Date) > ExpiryDay Then
            IsExpiry = True
            GoTo last
        End If
    End If

last:
End Function



Private Sub cmdCheckExpiry_Click()

  If IsExpiry then
     MsgBox("Expired")
  Else
     MsgBox("Not Expired")
  Endif

End Sub

Upvotes: 0

SuperDre
SuperDre

Reputation: 194

What is the type of the database field "Expiration_Date"? is it a String or is it a Date? If the type is a String, then you'll need to convert it to a Date by splitting up the string and using DateSerial, and if it IS a Date, the code should work. So I think the actual type is a String and therefore it won't work.

Also just use recordset("Expiration_Date") > Date rather than recordset("Expiration_Date") > DateValue(Now), although both do exactly the same, but Date is more optimized.

Upvotes: 0

user1844933
user1844933

Reputation: 3417

try this

Dim sDate as date
sDate = format(recordset("Expiration_Date"),"mm/dd/yyyy")
if datediff("d",Format(Now, "mm/dd/yyyy"),sDate) < 0 then
'already expired
end if

Upvotes: 3

Related Questions