Solved

Convert string to date

Posted on 2004-10-24
106,068 Views
Last Modified: 2012-05-05
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
0
Question by:prettykittyq
    18 Comments
     
    LVL 27

    Expert Comment

    by:jjafferr
    Hi prettykittyq,

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

    Hope this helps

    Jaffer
    0
     
    LVL 48

    Expert Comment

    by:Gustav Brock
    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
     
    LVL 15

    Expert Comment

    by:Colosseo
    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
     
    LVL 48

    Expert Comment

    by:Gustav Brock
    Scott is right. Still:

    StartDate2.Value = datStartDate2

    /gustav
    0
     
    LVL 27

    Expert Comment

    by:jjafferr
    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
     
    LVL 41

    Expert Comment

    by:shanesuebsahakarn
    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
     
    LVL 41

    Expert Comment

    by:shanesuebsahakarn
    Oh yes - should mention that odding Option Explicit to the top of the module will help troubleshoot these little typos...
    0
     
    LVL 2

    Author Comment

    by:prettykittyq
    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
     
    LVL 41

    Assisted Solution

    by:shanesuebsahakarn
    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
     
    LVL 27

    Expert Comment

    by:jjafferr
    Shane, I don't think you need the CDate anywhy, as long as the fields in CleanCalendar are set to Date/Time
    0
     
    LVL 48

    Accepted Solution

    by:
    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
     
    LVL 2

    Author Comment

    by:prettykittyq
    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
     
    LVL 27

    Assisted Solution

    by:jjafferr
    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
     
    LVL 2

    Author Comment

    by:prettykittyq
    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
     
    LVL 48

    Expert Comment

    by:Gustav Brock
    > 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
     
    LVL 27

    Expert Comment

    by:jjafferr
    keep the fields set to date/time, otherwise you will face another issue when using these fields
    0
     
    LVL 2

    Author Comment

    by:prettykittyq
    Thanks, gustav and jaffer and shane.  It worked just fine once I followed directions.

    Kitty
    0
     
    LVL 27

    Expert Comment

    by:jjafferr
    I am glad you got it going,
    Thanks for the points and the grade,

    Good luck with your project

    jaffer
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
    With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now