The best thing to do is to get your query working in Postman, then copy that into Excel and create your Connection while recording it as a macro.
What I did after that was to parameterize the URL. I removed the actual data and replaced it with “$name” placeholders. Finally, I created a button that calls the VBA routine that replaces the $parms with actual values from cells. You can try to build the formula from scratch but the quotes get real hairy really fast.
Here’s some of my initial testing code as an example:
' Get Data and format to REST format
sStartDt = Format(Range("StartDate"), "yyyy-MM-dd")
sEndDt = Format(Range("EndDate"), "yyyy-MM-dd")
sPeriod = Format(DateAdd("d", -1, sStartDt), "yyyy-MM-dd")
' Change Instance as neede
sURI = "https://ausdtspilot100.epicorsaas.com/SaaS000Pilot"
' BAQ to call
sBAQ = "000-Backlog"
' Create a new formula from the template
sTemplate = "let" & Chr(13) & "" & Chr(10) & _
" Source = OData.Feed(""$URI/api/v1/BaqSvc/$BAQ?Company='101'&Period='$PERIOD'&Start_Date='$START'&End_Date='$END'"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
' Update Formula with parameters
sFormula = sTemplate
sFormula = Replace(sFormula, "$URI", sURI)
sFormula = Replace(sFormula, "$BAQ", sBAQ)
sFormula = Replace(sFormula, "$PERIOD", sPeriod)
sFormula = Replace(sFormula, "$START", sStartDt)
sFormula = Replace(sFormula, "$END", sEndDt)
' Update Formula
ActiveWorkbook.Queries("BacklogBAQ").Formula = sFormula
' Clear Data tab
Set wsData = Sheets("Data")
wsData.Activate
Cells.Select
Selection.ListObject.QueryTable.Delete
Selection.ClearContents
' Refresh data
With wsData.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=BacklogBAQ;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [BacklogBAQ]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "BacklogBAQ"
.Refresh BackgroundQuery:=False
End With
Mark W.