Hi All,

I have recorded a macro from excel by using Data-Import External Data-> Import Data and the connection I have established from SQL server and while importing data I have edited the query with my query but when I try to run the macro it shows and .i.e table does not exists but actually I have checked my database name and other tables present in my sql server and the name convention in my query is my query is exactly same but still it shows the above error. Following is my code and I don't what is going wrong. Please help.

[VBA]Sub ExtractDatafromSQL()

With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=DB-77716EFB0313\SQLEXPRESS;Use Procedure for" _
, _
" Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=DB-77716EFB0313;Use Encryption for Data=False;Tag with column col" _
, "lation when possible=False;Initial Catalog=meta_data"), Destination:=Range( _
"A1"))
.CommandType = xlCmdTable
.CommandText = Array("SELECT mydata.*, [Cost Center mapping].[Product UBR Code], [Cost Center mapping].[Sub Product UBR Code], [Cost Center mapping].[Sub-sub Product UBR Code], [Cost Element Mapping].FSI_LINE1_code, [Cost Element Mapping].FSI_LINE2_cod" _
, _
"e, [Cost Element Mapping].FSI_LINE3_code, [Country and Region Mapping].Country, [Country and Region Mapping].Region" & Chr(10) & "FROM ((mydata INNER JOIN [Cost Center mapping] ON mydata.[Cost Center] = [Cost Center mapping].[Cost Center]) IN" _
, _
"NER JOIN [Cost Element Mapping] ON mydata.[Unique Indentifier 1] = [Cost Element Mapping].CE_SR_NO) INNER JOIN [Country and Region Mapping] ON mydata.[Company Code] = [Country and Region Mapping].[Company Code]" & Chr(10) & "WHERE ((([Cost Ce" _
, _
"nter mapping].[Product UBR Code])=""P_6957"") AND (([Cost Center mapping].[Sub Product UBR Code])=""P_8456"" Or ([Cost Center mapping].[Sub Product UBR Code])=""P_8453"") AND (([Cost Center mapping].[Sub-sub Product UBR Code])=""P_6975"" " _
, _
"Or ([Cost Center mapping].[Sub-sub Product UBR Code])=""P_6984"") AND ((([Cost Element Mapping].[FSI_LINE1_code])=""F1750000000"") AND (([Cost Element Mapping].[FSI_LINE2_code])=""F1753000000"" Or ([Cost Element Mapping].[FSI_LINE2_code])=""F1757001000"") " _
, _
"AND (([Cost Element Mapping].[FSI_LINE3_code])=""F1753007000"" Or ([Cost Element Mapping].[FSI_LINE3_code])=""F1757002000"") AND ((mydata.Period)=1) AND ((mydata.Year)=2010)));" _
)
.Name = "DB-77716EFB0314_SQLEXPRESS meta_data mydata"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceConnectionFile = _
"C:\DB-77716EFB0314_SQLEXPRESS meta_data mydata.odc"
.Refresh BackgroundQuery:=False
End With
End Sub[/VBA]

Thanks for your help in advance.