Date conversion

Why can't I convert a string of format #mm/dd/ccyy" to a date variable?

Dim dTmpDate as Long
Dim strDate as String

strDate = "#" & "03/21/2006" & "#"
dTmpDate = CDate(strDate)

Error: Type conversion

swansonplaceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EDDYKTCommented:
strDate = "03/21/2006"
0
cubixSoftwareCommented:
You have declared dTmpDate as long

try Dim dTmpDate as Date

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>Dim dTmpDate as Long
Because you have this as a Long, to convert to a date Variable start with...
Dim dTmpDate as Date
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You can also do some validating like this...

If Isdate(strDate)
   dTmpDate = CDate(strDate)
else
   msgbox "strDate is not a valid Date.  Handle this gracefully."
end if
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Questions, comments...
0
ptan218Commented:
This code will work for you.

Dim dTmpDate as DATE       ';<-- CHANGE DATATYPE TO DATE
Dim strDate as String

strDate = "03/21/2006"   '<--- DO NOT PUT POUND SIGN HERE. IT ONLY WORKS IN ACCESS
dTmpDate = CDate(strDate)
0
EDDYKTCommented:
don't think you need to change to

Dim dTmpDate as DATE      

since if you not deal with hour and second, it should be number and not decimal number
0
cubixSoftwareCommented:
...are we done here?
0
cubixSoftwareCommented:
Dan - I think the points should be split between myself (for my first reply) and to jimhorn for his IsDate reply
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.