HMote


Could anyone point me in the right direction I just need a way to see if a date exists...example: "6/31/07" doesn't exist, whereas "6/30/07" does exist. Thanks!




Re: How to check if a date exists

Adamus Turner


Code Snippet

Try

Dim mydate As Date = "06/31/2007"

Catch ex As InvalidCastException

MessageBox.Show("Date does not exist")

End Try

A date will throw a cast exception if the date isn't valid. It's built in. I bet you thought it would be difficult

Happy coding,

Adamus







Re: How to check if a date exists

HMote

I can't seem to use the Try/Catch block...VBA, right




Re: How to check if a date exists

ADG

Hi

Below example of error handling may help.

Sub test()
Dim str, dte, rsp


str = "06/31/07"
On Error Resume Next
dte = DateValue(str)
If Err.Number = 13 Then
rsp = MsgBox("Invalid Date", vbOKOnly, "Error")
Else
Debug.Print dte, str
End If

End Sub





Re: How to check if a date exists

Adamus Turner

ADG wrote:

Hi

Below example of error handling may help.

Sub test()
Dim str, dte, rsp


str = "06/31/07"
On Error Resume Next
dte = DateValue(str)
If Err.Number = 13 Then
rsp = MsgBox("Invalid Date", vbOKOnly, "Error")
Else
Debug.Print dte, str
End If

End Sub

Yes sorry. Try Catch is not available in VBA but this code should work for you.

Adamus






Re: How to check if a date exists

HMote

Awesome, that works perfectly.