If you simply use this...
customdate = (dd.Text & "/" & mm.Text & "/" & yyyy.Text)
it should fix your problem...
Saurabh...
I've created a userform with 3 textboxes (input boxes). One for the month, one for the day, and one for the year. I'm trying to have my code look at it, and see if it's a valid date, otherwise clear it out and start over.
Here's the Code:
When I put in the following 25(m), 3(d), and 2009(y), it switches the month and day around, lists it as a valid date, and spits out 3/25/2009, when I'm wanting it to reject the date as it should read 25/3/09. How do I fix it? I've tried using CDATE, and format date ("mm dd yyyy"), but it's still swapping the month and day if the month value is less than 31.
Public Sub Commandbutton1_Click
If mm.Text = "" And dd.Text = "" And yyyy.Text = "" Then
customdate = 0 'It's also throwing out a message box for this once the button is clicked. How do I disable it?
DayOption.Hide
ElseIf IsDate(Format(dd.Text & "/" & mm.Text & "/" & yyyy.Text,"mm dd yyyy")) = True Then
customdate = CDate(dd.Text & "/" & mm.Text & "/" & yyyy.Text)
DayOption.Hide
Else
MsgBox ("The date you have chosen is not valid. Please try again")
mm.Text = ""
dd.Text = ""
yyyy.Text = ""
End If
End Sub
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ryancys,
I originally had it listed as the first one (CDate(mm.Text & "/" & dd.Text & "/" & yyyy.Text)) which gave me the error. I used 5/27/09 and 27/5/09 as dates, and the CDATE result was 5/27/09 with both.
Saurabh726,
Same Issue as above but with a slightly different result. With m = 27, d = 5, y = 2009, here's the code and result:
ElseIf IsDate(dd.Text & "/" & mm.Text & "/" & yyyy.Text) = True Then ' Validates the date as 5/27/09
customdate = (dd.Text & "/" & mm.Text & "/" & yyyy.Text) 'Produces 27/5/09 which isn't valid, but says it is above.
Ryancys,
I tried the last change as well, but moved the format to work and got the same result as the first one you sent over:
ElseIf IsDate(mm.Text & "/" & dd.Text & "/" & yyyy.Text) = True Then
customdate = CDate(mm.Text & "/" & dd.Text & "/" & yyyy.Text)
I just tried it (switched the second line) using m = 5, d = 27 y = 2009 which produced 5/27/2009
and also tried m = 27, d = 5, y = 2009 which produced 5/27/2009
ElseIf IsDate(DateValue(mm.Text & "/" & dd.Text & "/" & yyyy.Text)) = True Then ' This needs to reject (27/5/09) and move to the
customdate = (mm.Text & "/" & dd.Text & "/" & yyyy.Text) 'last clause of the If-Then stmt but it still accepts it : (
DayOption.Hide
Else
MsgBox ("The date you have chosen is not valid. Please try again")
mm.Text = ""
dd.Text = ""
yyyy.Text = ""
Saurabh726,
Thanks for your help! I modified your code a little to work, but I'd completely overlooked the idea of using a function to catch the error before the date switch happened. I'll have to tweak it just a bit as the function takes the left two parts of the month, but if ony 1 is entered, it grabs the next digit and invalidates the date. Not a big deal.
Now if I could just figure out now why it's giving me a message box with the customdate result in it, and turn it off, I'll be set!!!
Here's my final code:
you can try use DateSerial function:
http://www.techonthenet.co
http://msdn.microsoft.com/
Business Accounts
Answer for Membership
by: ryancysPosted on 2009-11-04 at 09:37:15ID: 25741785
to use CDATE function, your date string should be in format of "mm/dd/yyyy" or other formats which can explicitly differentiate the day and month, like: "dd mmm yyyy", "yyyy/mm/dd" etc.