Convert string to date

I'm working with calendar data exported from Outlook.  For some reason, Outlook exports the data as text, not a date/time variable, so I need to convert the data to date/time before I can work with it.

I tried the following:

Private Sub Clean1_Click()
Dim datStartDate2 As Date
Dim strStartDate As String

StartDate.SetFocus
strStartDate = StartDate.Text
datStartDate2 = CDate(strStartDate)
StartDate2.SetFocus
StartDate2.Text = datStartDate

End Sub

but keep coming up with a type mismatch error.

I also tried DateValue instead of CDate but still got a type mismatch error.

I've tried dim-ing datStartDate2 as Date and as Var but still get that error.

Any suggestions?

Kitty
LVL 1
prettykittyqAsked:
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.

jjafferrCommented:
Hi prettykittyq,

try
datStartDate2 = Format(strStartDate,"mm-dd-yy")

Hope this helps

Jaffer
0
Gustav BrockCIOCommented:
For the date conversion there is no reason to set focus.
Also, you need to assign to the Value property:

'StartDate.SetFocus
strStartDate = StartDate.Text
datStartDate2 = CDate(strStartDate)
'StartDate2.SetFocus
StartDate2.Value = datStartDate

/gustav
0
ColosseoCommented:
Hi

don't know where this is part of your problem or was just a mis type in your post but you are using

StartDate2.Text = datStartDate

but datStartDate doesnt exist so you need

StartDate2.Text = datStartDate2

Cheers

Scott
0
Ultimate Tool Kit for Technology Solution Provider

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 now.

Gustav BrockCIOCommented:
Scott is right. Still:

StartDate2.Value = datStartDate2

/gustav
0
jjafferrCommented:
if you are reffering to a field on a Form then it should be either

[StartDate2]
OR
me.StartDate2

But I assume these are NOT fields, but variables used to get the data from Outlook.

jaffer
0
shanesuebsahakarnCommented:
Why not just assign the value directly rather than going through all of the extra variable declarations?

Me!StartDate2=CDate(Me!StartDate)

I have omitted the .Value since that property is implicit (but so is Me!, it's just a matter of style unless you're using .net).
0
shanesuebsahakarnCommented:
Oh yes - should mention that odding Option Explicit to the top of the module will help troubleshoot these little typos...
0
prettykittyqAuthor Commented:
Thanks for the input.

I see I didn't give enough information since none of those suggestions helped so let me try again.

The data has been exported from Outlook Calendar to an Excel file which is linked to an Access table.  In Excel, the dates are showing up as:

'10/25/2004

The apostrophe at the beginning is causing Access (and Excel) to see the data as a string rather than numbers.

In an earlier version of this project, we solved that problem in Excel by using a macro based on Excel's DATEVALUE function which "recognizes" the number part of the string and creates the date equivalent.

I'm now trying to achieve the same end with VBA code in Access, so my user doesn't have to run the Excel macro in Excel.

I have a table called Calendar that is the raw data from Excel.  I am trying to take the string in the StartDate field of the Excel-based table Calendar, run it through the appropriate routine, and store the resulting date in the StartDate2 field of a new table CleanCalendar so that Access will recognize it as a date/time field and not a text field.

I tried using Access's DATEVALUE function but kept getting a type mismatch error.

Then I tried using Access's CDATE function but I keep getting a type mismatch error.

The "set focus" code comes up because I'm retrieving the value of the StartDate field of the current row through the StartDate textbox on my form.

So, the task is really:
1.  Retrieve the string StartDate from table Calendar.
2.  Convert the string StartDate into a date/time value.
3.  Store the date/time value in StartDate2 in table CleanCalendar.

If there's a better way to do this, I'm happy to start over but I'm stymied.

As it happens, each record in Calendar has three of these little devils:  StartDate (a string with date information) and StartTime and EndTime (strings with time information), so I'm going to need to do all of them.

Again, thanks but none of your comments have worked so far.

Kitty
0
shanesuebsahakarnCommented:
Hmm, you could run this SQL statement:

INSERT INTO CleanCalendar (StartDate, StartTime, EndTime) SELECT CDate(Mid$(StartDate,2)), CDate(Mid$(StartTime,2)), CDate(Mid$(EndTime,2)) FROM Calendar

I think that will do it.
0
jjafferrCommented:
Shane, I don't think you need the CDate anywhy, as long as the fields in CleanCalendar are set to Date/Time
0
Gustav BrockCIOCommented:
First, are you aware that you can link an Excel worksheet or Named Range as a table in Access?
This would save you the import.

Second, the append query could be something like this:

  INSERT INTO tblCalendarA
    (StartDate,
    StartTime,
    EndTime)
  SELECT
    MID(StartDate, 2),
    MID(StartTime, 2),
    MID(EndTime, 2)
  FROM
    Calendar

/gustav
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
prettykittyqAuthor Commented:
Shane, the SQL statement looks promising but I don't know how to use it.

Would it be part of a query that I'd run to append the new data to the CleanCalendar table?

Or do I need to create a recordset and run a routine with that?

Sorry, I do need a lot of coaching on this.

Thanks for the help.

Kitty
0
jjafferrCommented:
Kitty
Please backup your Data before you do this.

Copy the SQL,
Open a new query,
Menu Bar > View > SQL and paste the SQL
Run the Query using the ! button.

jaffer
0
prettykittyqAuthor Commented:
Thanks, jaffer.  I tried it and got the error message

"Calendar Project set 240 fields to Null due to a type conversion failure."

The relevant fields in the receiving table (CleanCalnder) were set to date/time.  I went back and changed them to text and that didn't help.

You remarked earlier that I might not need to use CDate.  Should I delete the CDates from the SQL statement?

Kitty
0
Gustav BrockCIOCommented:
> You remarked earlier that I might not need to use CDate.  Should I delete the CDates from the SQL statement?

Yes, Jaffer is right. Try this:

 INSERT INTO CleanCalendar
    (StartDate,
    StartTime,
    EndTime)
  SELECT
    MID(StartDate, 2),
    MID(StartTime, 2),
    MID(EndTime, 2)
  FROM
    Calendar

/gustav
0
jjafferrCommented:
keep the fields set to date/time, otherwise you will face another issue when using these fields
0
prettykittyqAuthor Commented:
Thanks, gustav and jaffer and shane.  It worked just fine once I followed directions.

Kitty
0
jjafferrCommented:
I am glad you got it going,
Thanks for the points and the grade,

Good luck with your project

jaffer
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
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.