Hi, im new to this forum, and as far as I know. this hasn't been posted.
What I am trying to create is a log in system, and in one of my forms I want to limit the date entered into a form.

For example a user cannot enter a date more than seven days from todays date
Which is what i achieved but what i am stuck on is
preventing a user entering a date in the future

Anyone who can help, I would be very greatful

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dateDifference As Integer ' this variable keeps the difference between the dates.

        Dim todaydte As Date = Now()   'Current date'

        ' Checks if date is valid
        If (IsDate(TxtBox1.Text)) Then
            ' Get the difference between the dates (in days)
            dateDifference = DateDiff("d", TxtBox1.Text, todaydte)
            ' Date difference more than 7 days?
            If (dateDifference > 7) Then
                ' if its then displays error message'
                MessageBox.Show("Date is out of range", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
                'and form closes'
                Me.Close()
            Else
                'if the date is 7 days or less message box will say
                MessageBox.Show("Continue", "Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
                'next screen will show
                'form hides
                Form5.Show()
                Me.Hide()

            End If
        Else
            'if a date is not entered then this will display
            MessageBox.Show("Please input a valid date", "Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
        End If
If (dateDifference > 7 Or dateDifference < -7) Then

will trap dates outside of the 7 day range in either direction.

Hi, sorry for the delay
I tried that code you suggested. But it still allows me to go onto the next form :(
Also attempted another IF statement, but then none of the dates were working.

Because I can't see the entire program, I'm confused with what is or isn't working in your code. In your original post, line 11 checks for a date more than 7 days ago. If dateDifference > 7 (date more than 7 days old), the error "Date is out of range" pops up and then the form closes. Is that what it is supposed to do for dates more than 7 days in the future also?

Yes, thats right
So say if the 5th of July was entered (as its the 28th Today)
the program would close also ..

I'm really confused. Please replace line 11 of your original posted code with the following line

If (dateDifference > 7 Or dateDifference < -7) Then

and then in detail tell me what the code is doing step by step, both right and wrong.

For example,

Date entered is more than 7 days in the future...
Line 13 - message is displayed - correct.
Line 15 - form closes - correct.

If the program is still not working after swapping line 11, please repost the current version.

This article has been dead for over six months. Start a new discussion instead.