Date comparisons

I have an access database that has a column of dates.

As I display these records, I want to compare the date in that field to the current date and if it is less than today, I want to alter a seperate label to indicate that the item on the record is expired.

I'm having a problem comparing the date in the db to the current date.

the data from my db is being displayed in a text box, txtexpirydate. When I try to get the value of that as val(txtexpirydate.text) I get an error. that's even before trying to compare to anything.

can someone please point me in the right direction.

thanks

tattoo



Answer this question

Date comparisons

  • astrocrep

    tattoo wrote:

    I have an access database that has a column of dates.

    As I display these records, I want to compare the date in that field to the current date and if it is less than today, I want to alter a seperate label to indicate that the item on the record is expired.

    I'm having a problem comparing the date in the db to the current date.

    the data from my db is being displayed in a text box, txtexpirydate. When I try to get the value of that as val(txtexpirydate.text) I get an error. that's even before trying to compare to anything.

    can someone please point me in the right direction.

    thanks

    tattoo

    Hi tattoo,

    Here is a simple example that will only compare the date of today to a date you place in a variable. The variable can come from a textbox or directly from you database. I am including an example of how to use it with a textbox. The CDate in the function is the key part; this operator converts the variable to a simple date format of M/D/YYYY. Do be aware this is setup for US date formats and will not accept words as the month; so if you were to enter Sept. 17, 2006 or 17/9/06 then it will produce an error; the date would need to be entered as 09/17/06, or 9/17/06, or 9/17/2006, etc...

    Public Function IsDateToday(ByVal DateToCompare As Date) As Boolean

    Dim dtToday, dtCompareDate As Date
    dtToday = CDate(Today)
    dtCompareDate =
    CDate(DateToCompare)
    If dtCompareDate = dtToday Then IsDateToday = True
    Return IsDateToday

    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    Dim blnResults As Boolean
    Dim dtexpirydate As Date
    dtexpirydate = txtexpirydate.Text
    blnResults = IsDateToday(dtexpirydate)
    If blnResults = True Then
    MessageBox.Show("Date is today.")
    Else
    MessageBox.Show("Date is NOT today.")
    End If

    End Sub

    I hope this helps you.

    Thank you,

    James



  • somegai

    get the date/time object from the database then try to do a TryParse to see if it can be converted to a datetime object.....

    Dim theResult as new DateTime()

    If DateTime.TryParse(Me.txtexpirydate, ByRef theResult) then

    'successfully parsed datetime, result stored in theResult - now do your thing

    end if

    does this help



  • Marcin Książek

    you are better to use TryParse rather than a direct convert to (CDate) as well as the added value of less code. I guess there are many ways for a solution but tryparse would be the better option and best practice



  • Date comparisons