We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Date conversion

swansonplace
swansonplace asked
on
Medium Priority
180 Views
Last Modified: 2010-05-01
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

Comment
Watch Question

Commented:
strDate = "03/21/2006"
You have declared dTmpDate as long

try Dim dTmpDate as Date

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
>Dim dTmpDate as Long
Because you have this as a Long, to convert to a date Variable start with...
Dim dTmpDate as Date
Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
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
Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
Questions, comments...

Commented:
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)

Commented:
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
...are we done here?
Dan - I think the points should be split between myself (for my first reply) and to jimhorn for his IsDate reply
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.