Check the expiry date entered


I have two comboboxes for MM and YY . I have already coded it so that the years dropdown as for next 20 years .Any suggestions on how to check  ** Must not expire today or in the past.

 Public Sub LoadMonthsandYear()
        Dim i As Integer
        Dim myvar As String = String.Empty
        i = 1 'initialise counter to 1
        Do While i <= 12
            myvar = MonthName(i, True)
            i = i + 1
        'Add next 20 YEARS to expiration year combobox
        'Must not be more than 20 years in the future
        i = 0 'initialise counter to 0
        Do While i <= 20
            Me.CmbYear.Items.Add(DateTime.Now.Year + i) 'Add years to combobox
            i = i + 1
    End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RIASAuthor Commented:
Need textbox for expiry date,comboboxes was a workaround

Why not use a dateTimepicker rather than combo boxes, you can then limit the dates available so that dates earlier than tomorrow are not selectable


for details about the dateTimePicker control
RIASAuthor Commented:
Hello ChloesDad,
Thanks for the advise but the requirement is to use textboxes for MM/YY .

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Try playing around with these:

Form1.vb -
Imports System.Globalization

Public Class Form1
	Private months As New List(Of DateTime)
	Private years As New List(Of DateTime)
	Private [now] As DateTime = DateTime.Now

	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		Dim seed As DateTime = New DateTime([now].Year, 1, 1)
		For i As Integer = 0 To 11 Step 1

		For i As Integer = 0 To 19 Step 1

		cmbMonth.DataSource = months
		cmbYear.DataSource = years
	End Sub

	Private Sub OnValidating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles tb2M2Y.Validating, tb2M4Y.Validating, DateTimePicker1.Validating, cmbYear.Validating, cmbMonth.Validating
		Dim [selected] As DateTime = DateTime.MinValue
		If sender.GetType().Equals(GetType(MaskedTextBox)) Then
			Dim tb As MaskedTextBox = DirectCast(sender, MaskedTextBox)
			If tb.Equals(tb2M2Y) Then
				DateTime.TryParseExact(tb.Text, "MM/yy", CultureInfo.CurrentCulture, Globalization.DateTimeStyles.AssumeLocal, [selected])
			ElseIf tb.Equals(tb2M4Y) Then
				DateTime.TryParseExact(tb.Text, "MM/yyyy", CultureInfo.CurrentCulture, Globalization.DateTimeStyles.AssumeLocal, [selected])
			End If
		ElseIf sender.GetType().Equals(GetType(DateTimePicker)) Then
			Dim tp As DateTimePicker = DirectCast(sender, DateTimePicker)
			[selected] = tp.Value
		End If

		If [selected] <> DateTime.MinValue Then
			If [selected].Year >= [now].Year AndAlso [selected].Year <= years(years.Count() - 1).Year Then
				If [selected].Year = [now].Year AndAlso [selected].Month >= [now].Month Then
					MessageBox.Show("Selected year and month are good")
				ElseIf [selected].Year > [now].Year Then
					MessageBox.Show("Selected year and month are good")
					MessageBox.Show("Selected year and month are not good")
				End If
				MessageBox.Show("Selected year and month are not good")
			End If
			MessageBox.Show("Selected year and month are not good")
		End If
	End Sub

	Private Sub OnFormat(sender As Object, e As ListControlConvertEventArgs) Handles cmbYear.Format, cmbMonth.Format
		If sender.GetType().Equals(GetType(ComboBox)) Then
			Dim cmb As ComboBox = DirectCast(sender, ComboBox)
			If cmb.Equals(cmbMonth) Then
				e.Value = CType(e.ListItem, DateTime).ToString("MM")
			ElseIf cmb.Equals(cmbYear) Then
				e.Value = CType(e.ListItem, DateTime).ToString("yy")
			End If
		End If
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
            End If
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker()
		Me.cmbMonth = New System.Windows.Forms.ComboBox()
		Me.cmbYear = New System.Windows.Forms.ComboBox()
		Me.tb2M4Y = New System.Windows.Forms.MaskedTextBox()
		Me.tb2M2Y = New System.Windows.Forms.MaskedTextBox()
		Me.DateTimePicker1.Location = New System.Drawing.Point(13, 13)
		Me.DateTimePicker1.Name = "DateTimePicker1"
		Me.DateTimePicker1.Size = New System.Drawing.Size(259, 20)
		Me.DateTimePicker1.TabIndex = 0
		Me.cmbMonth.FormattingEnabled = True
		Me.cmbMonth.Location = New System.Drawing.Point(186, 66)
		Me.cmbMonth.Name = "cmbMonth"
		Me.cmbMonth.Size = New System.Drawing.Size(40, 21)
		Me.cmbMonth.TabIndex = 1
		Me.cmbYear.FormattingEnabled = True
		Me.cmbYear.Location = New System.Drawing.Point(232, 66)
		Me.cmbYear.Name = "cmbYear"
		Me.cmbYear.Size = New System.Drawing.Size(40, 21)
		Me.cmbYear.TabIndex = 2
		Me.tb2M4Y.Location = New System.Drawing.Point(150, 40)
		Me.tb2M4Y.Mask = "00/0000"
		Me.tb2M4Y.Name = "tb2M4Y"
		Me.tb2M4Y.Size = New System.Drawing.Size(122, 20)
		Me.tb2M4Y.TabIndex = 3
		Me.tb2M2Y.Location = New System.Drawing.Point(13, 40)
		Me.tb2M2Y.Mask = "00/00"
		Me.tb2M2Y.Name = "tb2M2Y"
		Me.tb2M2Y.Size = New System.Drawing.Size(121, 20)
		Me.tb2M2Y.TabIndex = 4
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 100)
		Me.Name = "Form1"
		Me.Text = "Form1"

	End Sub
	Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker
	Friend WithEvents cmbMonth As System.Windows.Forms.ComboBox
	Friend WithEvents cmbYear As System.Windows.Forms.ComboBox
	Friend WithEvents tb2M4Y As System.Windows.Forms.MaskedTextBox
	Friend WithEvents tb2M2Y As System.Windows.Forms.MaskedTextBox

End Class

Open in new window

Which produces the following output -I have two masked text boxes here, one will accept month and year in two digit format.  The other month in two digit and year in four digit.Choosing a valid date.Choosing and invalid date.-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You could do the same with just a single text box, you just want to parse the date and ensure that you accept multiple formats.

RIASAuthor Commented:
Cheers mate will give a try and get back!!
RIASAuthor Commented:
Thanks a million mate!!!worked like charm!!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.