[2005] Getting rid of a repeat?
Hi, I have written some coding that will not allow the dtp to select a date before today. It changes the date to todays date and shows a msgbox say "This date has passed, please select another".
I need to insert it into the valuechanged sub, so that it checks everytime the dtp changes.
What happens is --> that the msgbox appear twice. I have thought about it and realise that as it the script realises that the date is before todays date , it changes the value to todays date it and as it changes it runs the script again to check if the date is before todays and finds the previous date and the msgbox appears again.
VB Code:
Private Sub Ffrom_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ffrom.ValueChanged
If Ffrom.Value < Now() Then
Ffrom.Value = Now()
MsgBox("This date has passed, please select another")
End If
End Sub
I may be wrong, I have only been started learning vb a few weeks ago.
Thanks,
Mutasim
Re: [2005] Getting rid of a repeat?
Use a flag that will allow/disallow the event to run Here is an example
VB Code:
Private m_AllowEvent As Boolean = True
Private Sub Dtp_ValueChanged(sender as object, e as eventargs) Handles dtp.ValueChanged
If m_AllowEvent = True AndAlso Dtp.Value < Now Then 'first checks if the flag is true if it is then compare dates
m_AllowEvent = False 'set the flag to false. This way the event won't be raised again when you fix the date
'Do your stuff
End If
m_AllowEvent = True 'set the flag back to true so the event can be raised again
End Sub
Re: [2005] Getting rid of a repeat?
your diagnose of the problem is spot on... the problem arises because NOW() includes the time, down to the milisecond.... what you should be comparing is the date portion. Because even if you set it to a date, it won't have the time, so it assumes midnight, which is less than Now() (which is the current date time) and you get the message.
try this:
VB Code:
If Ffrom.Value < System.Data.Date.Today Then
I think that's the right namespace path.....
-tg
Re: [2005] Getting rid of a repeat?
Quote:
Originally Posted by techgnome
your diagnose of the problem is spot on... the problem arises because NOW() includes the time, down to the milisecond.... what you should be comparing is the date portion. Because even if you set it to a date, it won't have the time, so it assumes midnight, which is less than Now() (which is the current date time) and you get the message.
try this:
VB Code:
If Ffrom.Value < System.Data.Date.Today Then
I think that's the right namespace path.....
-tg
It says 'Date' is not a member of 'Data'. and it doesn't solve it. Thanks for trying though. After reading your post i tried, and this still doesn't solve it. :sick:
Thanks,
Mutasim
Re: [2005] Getting rid of a repeat?
Quote:
Originally Posted by mpdeglau
Use a flag that will allow/disallow the event to run Here is an example
VB Code:
Private m_AllowEvent As Boolean = True
Private Sub Dtp_ValueChanged(sender as object, e as eventargs) Handles dtp.ValueChanged
If m_AllowEvent = True AndAlso Dtp.Value < Now Then 'first checks if the flag is true if it is then compare dates
m_AllowEvent = False 'set the flag to false. This way the event won't be raised again when you fix the date
'Do your stuff
End If
m_AllowEvent = True 'set the flag back to true so the event can be raised again
End Sub
This seems to work, but the only problem is that an error occur and I think if solved it will work,
Quote:
Originally Posted by VB ERROR
Private' is not valid on a local variable declaration.
I have now emmbedded the code itno a differnet sub, if you could please have a look and see if you notice anything wrong:
VB Code:
Sub check()
Private m_AllowEvent As Boolean = True
Dim days As Integer = Tto.Value.Day - Ffrom.Value.Day + 1
If m_AllowEvent = True AndAlso Ffrom.Value < Now.Date() Or _
Tto.Value < Now.Date() Then
m_AllowEvent = False
Ffrom.Value = Now.Date()
Tto.Value = Now.Date()
MsgBox("This date has passed, please select another")
m_AllowEvent = True
else if ....
Re: [2005] Getting rid of a repeat?
m_AllowEvent has to be declared at the class level, like I showed.
Re: [2005] Getting rid of a repeat?
Quote:
Originally Posted by mpdeglau
m_AllowEvent has to be declared at the class level, like I showed.
Hi, I added to to class level instead of in the sub and it hasn't fixed the problem.
Will anyone have a look at the problem for me? PM me and I will send you the project :D
Thanks,
Mutasim