PDA

View Full Version : Solved: cancel on inputbox



philfer
01-25-2008, 12:44 PM
I am using an input box to get the report date as follows :-

Dim reportdate as Date

reportdate = InputBox "Enter the report date......etc

After this I do :-

If reportdate = "" Then

End If

to try to capture the scenario that the user presses Cancel

I get a Type Mismatch

I need the reportdate as a date as I do date calculations on it later.

I have tried :-

If Cstr(reportdate) = "" Then

but it still doesnt work

Help!!

Paul_Hossler
01-25-2008, 02:24 PM
Let to itself, Inputbox returns a string. Since you assigned it to a String variable, VBA will be nice and convert it for you if it can.

When you return an empty string, VBA can't do the conversion (and neither can you :whistle:

However, you can test for it, and take the appropriiate action

Paul


Sub Test()
Dim sInput As String
Dim dateInput As Date

sInput = InputBox("Date = ", "Date Is")

If IsDate(sInput) Then
dateInput = CDate(sInput)
Else
dateInput = 0
End If
End Sub