Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Throwing date errors

Status
Not open for further replies.

harris79

Technical User
Apr 27, 2001
208
GB
I have written a small app which converts day number (1-366) to actual date and vice-versa. It works OK, so I started to think what would happen if the user types an invalid date (29/02/2010 or 33/09/2010 etc) and how do I detect this.

If I do a DateDiff on the input date, I can throw and trap these errors. Great.
But this seems like a bodge to me. After all I'm not interested in the output from the DateDiff.

I was wondering how other people would go about detecting this type of error, without having to code every eventuality?



Steve
 
Start with a MaskedTextBox to handle the format and data type validation. Pass the ValidateText value to a variable with data type Date. If DateVariable.Ticks = 0 then the value was invalid. The only issue you still have to contend with is 10/10/5555 is still a legitimate date, but may not a be a viable date for the record at hand. It's not perfect, but has worked for us so far.
Code:
			RecDate = txtRecvDate.ValidateText
			If RecDate.Ticks = 0 Then
				StatBarEllipse(txtMsg, "Invalid Receiving Date. Correct and try again.", True)
				txtRecvDate.SelectAll()
				txtRecvDate.Focus()
				Exit Try
			End If

--------------------------------------------------
Stubbornness is a virtue -- if you are right. --Chuck Noll
--------------------------------------------------
 
ousoonerjoe thanks for this.

I had been using a string for the input date, and then splitting it into day, month & year to work out the day number. Using a date type allowed me to use DatePart to get the number, 1 line of code replaced about 70!

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top