3

I have an Excel macro that copies data from several Excel Worksheets to a single Access table. Everything is working well except that the data in access should be converted to mmmm yyyy whilst when I copy it to access, it converts always to dd/mm/yyyy.

The fields are "Start Date" and "End Date".

Here is an extract of my code:

Dim Catalog As Object
Dim cn As ADODB.connection
Dim dbPath as String, scn as String

dbPath = CPTwb.Path & "\Flatfile.accdb"`
scn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath & ";"

Set Catalog = CreateObject("ADOX.Catalog")`
Catalog.Create scn
Set Catalog = Nothing
Set cn = New ADODB.connection

With cn
    .Open scn
    .Execute "CREATE TABLE Flatfile ([Tracker Product] text(255) WITH 
         Compression, " & "[Contract ID] text(255) WITH Compression, " & _
         "[Client] text(255) WITH Compression, " & _
         "[Start Date] datetime, " & "[End Date] datetime)"
End With

TheSQL = "INSERT INTO Flatfile ([Tracker Product], [ContractID], [Client],[Start Date], [End Date])"
TheSQL = TheSQL & "SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & flatPath & "]." & "[" & Application.ActiveSheet.name & "$]"

cn.Execute TheSQL
cn.close

I understand the property "datatime" always gets the values as system date and that it is not possible to edit via DDL.

Is there a way to apply via VBA the property like in this screenshot below?

enter image description here

1 Answer 1

2

The format is for display only. A true date value is a numeric value.

So use this:

"SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & flatPath & "]." & "[" & Application.ActiveSheet.name & "$]"

as source in a simple select query. Add conversion (and filtering) as needed to make sure that you have true date values for [Start Date]. For example, the dates may be read as text, then convert with DateValue:

TrueStartDate: DateValue([DateColumnFromExcel])

Now, save that query and use it as source when you build TheSQL.

Sign up to request clarification or add additional context in comments.

Comments

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.