1004 ODBC General Error when columns are added to a query - excel

Original Query that works
CSQL2 = CSql1 & vbNewLine & _
"SELECT CASE WHEN TRIM ( y.fbn ) LIKE '%ABC%' THEN ( bd.build_id ) ELSE TRIM ( y.fbn ) END AS fbn, y.number, y.title, y.owner, y.type, y.open_closed," & vbNewLine & _
"y.date_approved, y.date_submitted_for_approval, y.expires_after, y.status, y.reg," & vbNewLine & _
"y.site, y.lines, y.cap, y.committed, y.invoiced, CASE" & vbNewLine & _
"WHEN y.Lines = 'Electrical' THEN 'Electrical_Equipment'" & vbNewLine & _
"WHEN y.Lines = 'Mechanical' THEN 'Mechanical_Equipment'" & vbNewLine & _
"WHEN y.Lines = 'Construction' THEN 'Base'" & vbNewLine & _
"ELSE y.Lines END category, " & vbNewLine & _
"CASE WHEN y.Lines = 'Electrical' THEN 'Other' " & vbNewLine & _
"WHEN y.Lines = 'Mechanical' THEN 'Other' " & vbNewLine & _
"WHEN y.Lines = 'Construction' THEN 'Base' ELSE y.Lines " & vbNewLine & _
"END subcategory " & vbNewLine & _
"FROM s1.line_new y LEFT JOIN (SELECT build_id, fbn FROM bd) bd ON bd.fbn = y.fbn " & vbNewLine & _
"WHERE Lines <> 'NW' AND ( y.Cap <> 0 OR y.Committed <> 0 OR y.Invoiced <> 0 )" & vbNewLine & _
"AND y.fbn IN ( SELECT DISTINCT fbn FROM bd )" & vbNewLine & _
"AND y.number NOT IN (SELECT num FROM s1.att) AND" & vbNewLine & CARFilters
New query that doesn't work to refresh the list object:
CSql2 = CSql1 & vbNewLine & _
"SELECT CASE WHEN TRIM ( bd.fbn ) LIKE '%ABC%' THEN ( bd.build_id ) ELSE TRIM ( bd.fbn ) END AS fbn, y.number, y.title, y.owner, y.type, y.open_closed," & vbNewLine & _
"y.date_approved, y.date_submitted_for_approval, y.expires_after, y.status, y.reg," & vbNewLine & _
"y.site, y.lines, ROUND(SUM(y.capex) OVER ( PARTITION BY y.fbn, lines ),2) AS cap, y.committed, y.invoiced, CASE" & vbNewLine & _
"WHEN y.Lines = 'Electrical' THEN 'Electrical_Equipment'" & vbNewLine & _
"WHEN y.Lines = 'Mechanical' THEN 'Mechanical_Equipment'" & vbNewLine & _
"WHEN y.Lines = 'Construction' THEN 'Base'" & vbNewLine & _
"ELSE y.Lines END category, " & vbNewLine & _
"CASE WHEN y.Lines = 'Electrical' THEN 'Other' " & vbNewLine & _
"WHEN y.Lines = 'Mechanical' THEN 'Other' " & vbNewLine & _
"WHEN y.Lines = 'Construction' THEN 'Base' ELSE y.Car_Lines " & vbNewLine & _
"END subcategory ,mcl.currency_id mcl_currency ,fx_2.local_currency ,fx_2.conversion_rate 2_fx_rate " & vbNewLine & _
",mcl.exchange_rate_effective_date,ROUND(fx.rate,4) conversion_rate " & vbNewLine & _
",ROUND(SUM(y.cap*fx.rate) OVER ( PARTITION BY y.fbn, lines ),2) AS cap_local " & vbNewLine & _
"FROM s1.mcl_carline_new y LEFT JOIN (SELECT build_id, fbn FROM bd) bd ON bd.fbn = y.fbn " & vbNewLine & _
"LEFT JOIN (SELECT number, exchange_rate_effective_date, currency_id FROM s1.mcl_new) mcl ON y.number = mcl.number " & vbNewLine & _
"LEFT JOIN s1.fx_rate_2 fx_2 ON fx_2.reg = y.reg " & vbNewLine & _
"LEFT JOIN s2.prcmt_exchange_rate fx ON fx_2.local_currency = fx.to_currency_id " & vbNewLine & _
"AND mcl.currency_id = fx.from_currency_id AND mcl.exchange_rate_effective_date = fx.effective_date " & vbNewLine & _
"WHERE Lines <> 'NW' AND ( y.Cap <> 0 OR y.Committed <> 0 OR y.Invoiced <> 0 ) " & vbNewLine & _
"AND y.fbn IN ( SELECT DISTINCT fbn FROM bd ) " & vbNewLine & _
"AND y.number NOT IN (SELECT num FROM s1.att) AND " & vbNewLine & CARFilters
Listobject Refresh section
With CData.ListObjects.Add(SourceType:=0, Source:=CS, Destination:=CData.Range("$A$1")).QueryTable
.CommandText = CSql2
.RefreshStyle = xlInsertDeleteCells
.AdjustColumnWidth = True
.ListObject.DisplayName = "CData"
.Refresh BackgroundQuery:=False
End With
During this LO existed and I had to modify the query, which added some columns; this refresh worked find until the columns got added. Now I get a 1004 General OBDC, but when I Debug.Print the query and run it AS IS in my SQL client it works without issue.
How can I tell what is causing the 1004? Note - CSql1 is unchanged.

12 hours of working on this and less than an hour after the bounty; I find the issue. Someone had removed permissions for the user running the query on the table required to retrieve the results.

Related

Having a Token Error when creating PowerQuery

This sub creates a powerquery with the source from a folderpath, it gets all the excel files from the folder and consolidate and filter it.
I am having an error whenever I make Source = Folder.Files("Dir") dynamic
Is there a way to make the paths dynamic so that I will just refresh the query page and update the value of the SourcePath for Folder.Files in my Settings sheet.
I have this error
Runtime-error '1004':
[Expression.Error] The name 'Source' wasn't recognized, Make sure it's spelled correctly.
in this line .Refresh BackgroundQuery:=False
If I create a value for SourcePath referencing a cell in a sheet with this format "C:\Path" ,It has this error
Error:
Runtime Error '1004':
Application Defined or Object-defined error
in this line
.ListObject.DisplayName = "Data"
Sub AddSaveQuery()
'Set Objects
Dim SourcePath As String
Dim SavePath As String
SourcePath = Worksheets("Home").Range("G21").Value
SavePath = Worksheets("Home").Range("G8").Value
Sub AddSaveQuery()
'Set Objects
Dim SourcePath As String
Dim SavePath As String
SourcePath = Worksheets("Home").Range("G21").Value
SavePath = Worksheets("Home").Range("G8").Value
'Will set first condition for sheet2
'Filter Column2 Begins with FilterValue1
ActiveWorkbook.Queries.Add Name:="Data", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""" & SourcePath & """)," & Chr(13) & "" & Chr(10) & " #""Filtered Hidden Files1"" = Table.SelectRows(Source, Each [Attributes]?[Hidden]? <> true)," & Chr(13) & "" & Chr(10) & " #""Invoke Custom Function1"" = Table.AddColumn(#""Filtered Hidden Files1"", ""Transform File"", Each #""Transform File""([Content]))," & Chr(13) & "" & Chr(10) & " #""Renamed Columns1""" & _
" = Table.RenameColumns(#""Invoke Custom Function1"", {""Name"", ""Source.Name""})," & Chr(13) & "" & Chr(10) & " #""Removed Other Columns1"" = Table.SelectColumns(#""Renamed Columns1"", {""Source.Name"", ""Transform File""})," & Chr(13) & "" & Chr(10) & " #""Expanded Table Column1"" = Table.ExpandTableColumn(#""Removed Other Columns1"", ""Transform File"", Table.ColumnNames(#""Transform File""(#""Sample File"")))," & Chr(13) & "" & _
"" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Expanded Table Column1"",{{""Source.Name"", Type text}, {""Column1"", Type any}, {""Column2"", Type any})," & Chr(13) & "" & Chr(10) & " #""Removed Other Columns"" = Table.SelectColumns(#""Column1" & _
"ed Type"",{""Column2""})," & Chr(13) & "" & Chr(10) & " #""Filtered Rows"" = Table.Sel" & _
"ectRows(#""Removed Other Columns"", Each Text.StartsWith([Column2], ""FilterValue1""))" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Filtered Rows"""
ActiveWorkbook.Queries.Add Name:="Parameter1", Formula:= _
"#""Sample File"" meta [IsParameterQuery=true, BinaryIdentifier=#""Sample File"", Type=""Binary"", IsParameterQueryRequired=true]"
ActiveWorkbook.Queries.Add Name:="Transform Sample File", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(Parameter1, null, true)," & Chr(13) & "" & Chr(10) & " Page1_Sheet = Source{[Item=""Page1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Page1_Sheet, [PromoteAllScalars=true])" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Promoted Headers"""
ActiveWorkbook.Queries.Add Name:="Sample File", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""" & SourcePath & """)," & Chr(13) & "" & Chr(10) & " Navigation1 = Source{0}[Content]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Navigation1"
ActiveWorkbook.Queries.Add Name:="Transform File", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = (Parameter1) => let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(Parameter1, null, true)," & Chr(13) & "" & Chr(10) & " Page1_Sheet = Source{[Item=""Page1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Page1_Sheet, [PromoteAllScalars=true])" & Chr(13) & "" & Chr(10) & " in" & Chr(13) & "" & Chr(10) & " #""Promoted Headers""" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Data;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [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 = "Data"
.Refresh BackgroundQuery:=False
End With
Workbooks("CognosDataCleaner2.xlsm").Connections.Add2 "Query - Parameter1", _
"Connection To the 'Parameter1' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Parameter1;Extended Properties=""""" _
, "SELECT * FROM [Parameter1]", 2
Workbooks("CognosDataCleaner2.xlsm").Connections.Add2 _
"Query - Transform Sample File", _
"Connection To the 'Transform Sample File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform Sample File"";Extended Properties=""""" _
, "SELECT * FROM [Transform Sample File]", 2
Workbooks("CognosDataCleaner2.xlsm").Connections.Add2 "Query - Sample File", _
"Connection To the 'Sample File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File"";Extended Properties=""""" _
, "SELECT * FROM [Sample File]", 2
Workbooks("CognosDataCleaner2.xlsm").Connections.Add2 "Query - Transform File" _
, "Connection To the 'Transform File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform File"";Extended Properties=""""" _
, "SELECT * FROM [Transform File]", 2
'Rename New Query Sheet to TEST1
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "TEST1"
Sheets("TEST1").Select
Workbooks("Book1").Connections.Add2 "Query - Transform File", _
"Connection To the 'Transform File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Transform File"";Extended Properties=""""" _
, "SELECT * FROM [Transform File]", 2
Workbooks("Book1").Connections.Add2 "Query - Sample File", _
"Connection To the 'Sample File' query in the workbook.", _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Sample File"";Extended Properties=""""" _
, "SELECT * FROM [Sample File]", 2
Sheets("TEST1").Copy
'SAVE PATH
ChDir SavePath
ActiveWorkbook.SaveAs Filename:= _
SavePath _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
I have created an Object SourcePath and in that path I concat it so it does have the value like this:
"Directory"
A1 = "
A2 = "
A3 = Directory or path
=CONCAT(A1 & A3 & A2)
I also have set Source as String to make it an object aswell.
You need quotes round the path, so:
"let" & Chr(13) & "" & Chr(10) & " Source = Folder.Files(""" & SourcePath & """),"

Humble business student - msgbox failing - SOS

The last line is causing some trouble.
MsgBox ("Book Code = " & ISBN & vbNewLine & _
"Book Title= " & title & vbNewLine & _
"Unit Price= " & title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & (Format(CustomerInput * UnitCost), Currency))
Move the close-parenthesis after UnitCost in the Format statement to the end, and Currency in the format statement needs to be a string (i.e: in quotes):
MsgBox ("Book Code = " & ISBN & vbNewLine & _
"Book Title= " & Title & vbNewLine & _
"Unit Price= " & Title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & (Format(CustomerInput * UnitCost, "Currency")))
Although the above works, you can omit some of your parentheses, and possibly make the code a bit easier to understand:
MsgBox "Book Code = " & ISBN & vbNewLine & _
"Book Title= " & Title & vbNewLine & _
"Unit Price= " & Title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & Format(CustomerInput * UnitCost, "Currency")

How to Bold or color text on auto email body from Excel VBA

I am sending mail linked to Excel data. i need to bold and red only few words and i am trying and i am not able to do it. Hope someone from here can help me out. Cells(i,13) has to get bold and Red when i am sending.
toList = Cells(i, 4) 'gets the recipient from col D
eSubject = "Request for KBR transport to " & Cells(i, 2) & " on " & Format(Cells(i, 3), "dd-mmm-yy")
eBody = "Dear " & Cells(i, 1) & vbCrLf & vbCrLf & "Can you please arrange KBR Transport for below PASSENGERS." & vbNewLine & vbNewLine & _
" " & Cells(i, 13) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
"Full Name : " & Cells(i, 6) & vbNewLine & vbNewLine & _
"Nationality : " & Cells(i, 7) & vbNewLine & vbNewLine & _
"Departure/Arrival Date : " & "" & Format(Cells(i, 8), "dd-mmm-yy") & vbNewLine & vbNewLine & _
"Airline : " & Cells(i, 9) & vbNewLine & vbNewLine & _
"Flight Number : " & Cells(i, 10) & vbNewLine & vbNewLine & _
"Departure/Arrival Time : " & Cells(i, 11) & vbNewLine & vbNewLine & _
"Escort required: " & Cells(i, 14) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
" Contact Number: " & Cells(i, 12) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
"Please confirm pick up time and availability." & vbNewLine & vbNewLine ##
Use HTML body instead, then you can use the HTML <b> and <font> tags. It may suit better as you can use a HTML <table> for the data displayed also.

How to change date format in outlook when I am sending mail linked with Excel data?

Can someone help me out here with formatting date when I am sending email? In Cell 8 I have date but when I'm sending email date will show as 10/03/2016 but I wanted to show date as 03-Oct-2016. I tried putting code as & Format(Date, "dd/mmm/yy") but when I run the program it will show today's date next to 10/03/2016.
Please see below my coding:
## "Full Name : " & Cells(i, 6) & vbNewLine & vbNewLine & _
"Nationality : " & Cells(i, 7) & vbNewLine & vbNewLine & _
"Departure/Arrival Date : " & Cells(i, 8) & vbNewLine & vbNewLine & _
"Airline : " & Cells(i, 9) & vbNewLine & vbNewLine & _
"Flight Number : " & Cells(i, 10) & vbNewLine & vbNewLine & _
"Departure/Arrival Time : " & Cells(i, 11) & vbNewLine & vbNewLine & _
"Escort required: " & Cells(i, 14) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
" Contact Number: " & Cells(i, 12) & vbNewLine & vbNewLine & _ ##
Change
"Departure/Arrival Date : " & Cells(i, 8) & vbNewLine & vbNewLine & _
to
"Departure/Arrival Date : " & Format(Cells(i, 8), "dd-mmm-yyyy") & vbNewLine & vbNewLine & _

Format vba msgbox to show cells with two decimal houses

The cells which the code is pointing (b2, b3 ..) are already showing up 2 decimal houses, but the msg box shows many . How can I fix it? I had an attempt with & _
Format(dTotalArea, "0.00"), but didnt work.
Many thanks in advance.
MsgBox "First Response Time (hours)= " & _
Worksheets("Results").Range("B2").Value & _
vbCrLf & "Investigation Time (hours)= " & _
Worksheets("Results").Range("B3").Value & _
vbCrLf & "Nr of Incidents Resolved= " & _
Worksheets("Results").Range("B4").Value & _
vbCrLf & "Resolved Same Day (%)= " & _
Worksheets("Results").Range("B5").Value & _
Format(dTotalArea, "0.00"), , "title_of_msgbox"
Try this
MsgBox "First Response Time (hours)= " & _
Worksheets("Sheet1").Range("B2").Value & _
vbCrLf & "Investigation Time (hours)= " & _
Format(Worksheets("Sheet1").Range("B3").Value, "0.00") & _
vbCrLf & "Nr of Incidents Resolved= " & _
Format(Worksheets("Sheet1").Range("B4").Value, "0.00") & _
vbCrLf & "Resolved Same Day (%)= " & _
Format(Worksheets("Sheet1").Range("B5").Value, "0.00") _
, , "title_of_msgbox"

Resources