0

Normally I figure out my DateTime issues in VBA and SQL by trial and error, but in this case I have hit a wall.

I am not even sure I can do what I want.

I have a temporary table with fields F1, F2, etc.

Field F7 contains a date as a string n the format dd/MM/yyyy.

Now, I want to copy the data from the temporary table, to a table where the data in field F7 should be stored in a Date/Time field. The format of this field is Short Date (dd-MM-yyyy).

I have tried using CDate(), Format() and Trim() alone and in several combinations, along with padding my date data with ## and adding brackets [] to my table fieldname, but I simply cannot find the correct combination.

This is my code. The importedDate works fine because I am using a variable. But for the field F7, I am mixing the SQL query with the VBA date conversion, and it is not working for me:

Sub MoveImportedData()
Dim SQLStr As String


SQLStr = "INSERT INTO TblDebitorSaldoListe " _
& "(CompanyCode,CompanyName, CustomerNumber, CustomerName, OneTimeCustomerName, TermsOfPayment, NetDueDate, Reference, " _
& "DunningBlock,Comment, ReminderOne, ReminderTwo, DebtCollection, TotalAmountDKK, TotalNotYetDueDKK, TotalOverdueDKK, " _
& "ReminderOneFile, ReminderTwoFile, NoReminder, ImportedDate) " _
& "SELECT F1, F2, F3, F4, F5, F6, #" & CDate("F7") & "#, F8, F9, '', '', '', '', F10, F11, F12, '', '', 0, '" & CDate(Date) & "' " _
& "FROM TEMP WHERE Len(F1)=4" _

MsgBox SQLStr

DoCmd.RunSQL SQLStr

End Sub

Any help would be much appreciated. I am using Access 2013.

Thanks.

7
  • Can you add in the code you've tried using to convert the DataType? It could be that there was something wrong with the code you were using here? Commented Feb 20, 2017 at 9:59
  • I have tried this combination among others: & "SELECT F1, F2, F3, F4, F5, F6, 'CDate(Format(Trim(F7), " & Chr(34) & "dd-mm-yyyy" & Chr(34) & "))', F8, F9, '', '', '', '', F10, F11, F12, '', '', 0, '" & CDate(Date) & "' " _ Commented Feb 20, 2017 at 10:01
  • Can you declare a variable, (newDate, for example) as type of DateTime. Run a SELECT query to get the value of F7, then run a VBA Convert command on the value, then insert it this way? It may require a loop to do it for each row, so not the most efficient way, but just to check it works. Commented Feb 20, 2017 at 10:07
  • Hi David. Thanks for the reply. I will try it. Commented Feb 20, 2017 at 10:13
  • 1
    Hi again. I think my issue is that my dates are on the form '23.10.2016' which we use in Denmark. Using Replace(date, ".", "-") seems to do the trick for me and allows me to go on. Commented Feb 20, 2017 at 10:38

2 Answers 2

1

It's really quite simple:

& "SELECT F1, F2, F3, F4, F5, F6, DateValue(F7), F8, F9, '', '', '', '', F10, F11, F12, '', '', 0, Date() " _

or, if F7 is dotted (corrected to have single-quotes with Replace):

& "SELECT F1, F2, F3, F4, F5, F6, DateValue(Replace(F7, '.', '-')), F8, F9, '', '', '', '', F10, F11, F12, '', '', 0, Date() " _
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, got the quotes for Replace mixes up. Corrected.
0

Working with Gustav's suggestion.

DateValue did not work, perhaps because of my danish locale. I think it returned a wrong date format (for me), which resulted in NULL being inserted into the F7 field of the table. That is this did not work, but might work for others with different date formats:

& "SELECT F1, F2, F3, F4, F5, F6, DateValue(Replace(F7, ""."", ""-"")), F8, F9, '', '', '', '', F10, F11, F12, '', '', 0, Date() " _

But CDate solved my issue along with adding extra ". This worked for me:

& "SELECT F1, F2, F3, F4, F5, F6, CDate(Replace(F7, ""."", ""-"")), F8, F9, '', '', '', '', F10, F11, F12, '', '', 0, Date() " _

Thank you both very much for quick and useful help!

3 Comments

DateValue will convert strings like '23-10-2016'. Also, except for pure integers, there should not be an expression that DateValue can't convert which CDate can. So what does your data really look like, please?
It contains data on the form: 25.02.2017 08.10.2016 01.12.2016 02.01.2017 21.10.2016
DateValue will convert these after replacing the dots, so something else must be going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.