Database Query from Macro and from MicrosoftQuery

Damian86

New Member
Joined
Jun 30, 2014
Messages
47
Hi all,

I have recently noticed that when I run a query from a macro, like...:

Code:
Sub My_Q()
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:="ODBC;DRIVER=XXX Server;SERVER=XXX;", Destination:=Range("$A$1")).QueryTable
        .CommandText = ("SELECT Table.ID" & Chr(13) & "" & Chr(10) & "FROM MyDB.dbo.Data Data")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Kwerenda_ListyPlac"
        .Refresh BackgroundQuery:=False
    End With
End Sub

... I get cells wita a date value as numbers. When I use Microsoft Query directly, I get date values as dates. Why is that so? I would also like to gat gates from macro (and I know I can add this to the code - but what is the point in adding another text of code).

Cheers!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top