Check on the input in an inputbox

L

Les Stout

Could somebody please help me with some code to be able to check the
date format the user puts into an input box.
I need to compare two dates and get the amount of days, so they need to
enter the date as 2004/01/14 and i need to confirm that it has been
entered correctly, if not return to the beginning & re-enter.

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
J

Jake Marx

Hi Les,

Here's some code that should work:

Public Function gbIsValidDate(rsDate As String) As Boolean
Dim avDate As Variant

If IsDate(rsDate) Then
avDate = Split(rsDate, "/")
If UBound(avDate) = 2 Then
gbIsValidDate = Len(avDate(0)) = 4 _
And Len(avDate(1)) = 2 _
And Len(avDate(2)) = 2
End If
End If
End Function

Sub test()
Dim sDate As String
Dim bValid As Boolean

Do While Not bValid
sDate = InputBox$("Please enter a date in " _
& "yyyy/mm/dd format", "Enter Date", _
Format$(Date, "yyyy/mm/dd"))
bValid = gbIsValidDate(sDate)
Loop
End Sub


It's not elegant, but it should force the user to enter a date in yyyy/mm/dd
format before the code proceeds.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top