SOLVED

ODBC data connection with parameter from drop down

%3CLINGO-SUB%20id%3D%22lingo-sub-1644537%22%20slang%3D%22en-US%22%3EODBC%20data%20connection%20with%20parameter%20from%20drop%20down%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1644537%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20there%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EVia%20ODBC%20I%20am%20connecting%20to%20a%20%3CSTRONG%3EPervasive%20(version%2013)%3C%2FSTRONG%3E%20database.%20Is%20there%20a%20way%20for%20me%20to%20somehow%20make%20use%20of%20a%20parameter%3F%20For%20example%2C%20in%20the%20below%20screenshot%20I've%20shown%20(although%20it%20is%20not%20syntactically%20correct)%20that%20I'd%20like%20to%20somehow%20use%20the%20named%20cell%20selected_pperiod%20as%20a%20parameter%20in%20my%20query.%20Is%20this%20even%20possible%3F%20And%20if%20it%20is%2C%20how%20would%20I%20go%20about%20passing%20a%20parameter%20in%20my%20SQL%20query%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22screenshot.png%22%20style%3D%22width%3A%20554px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F217011iA74A2FDA4B84163C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22screenshot.png%22%20alt%3D%22screenshot.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMany%20thanks%2C%3C%2FP%3E%3CP%3EMichelle%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1644537%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1644564%22%20slang%3D%22en-US%22%3ERe%3A%20ODBC%20data%20connection%20with%20parameter%20from%20drop%20down%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1644564%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F783767%22%20target%3D%22_blank%22%3E%40michellepace%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EGenerate%20query%20with%20any%20hardcoded%20parameter%2C%20e.g.%20102.%20After%20that%20in%20advanced%20editor%20add%20getting%20parameter%20string%20and%20correct%20generated%20query%20with%20it%2C%20like%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3Elet%0A%20%20%20%20parameter%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22selected_pperiod%22%5D%7D%5BContent%5D%7B0%7D%5BColumn1%5D%2C%0A%0A%20%20%20%20....%22start%20query%22%20%26amp%3B%20parameter%20%26amp%3B%20%22end%20query%22%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1644657%22%20slang%3D%22en-US%22%3ERe%3A%20ODBC%20data%20connection%20with%20parameter%20from%20drop%20down%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1644657%22%20slang%3D%22en-US%22%3EThank%20you%20very%20much%20for%20such%20a%20prompt%20reply.%20I%20am%20quite%20new%20at%20connecting%20to%20data%20from%20within%20excel.%20When%20you%20say%20%E2%80%9Cgenerate%20query%E2%80%9D...%20quite%20honestly%20all%20I%20know%20how%20to%20do%20is%20click%20the%20%E2%80%9CGet%20Data%E2%80%9D%20button%20and%20enter%20my%20sql%20in%20there.%20Could%20you%20please%20tell%20me%20how%20to%20generate%20the%20query%20and%20where%20to%20write%20that%20code%3F%3CBR%20%2F%3E%3CBR%20%2F%3E(Sorry%20I%E2%80%99m%20catching%20up%20on%20years%20here%20of%20excel%20inactivity.)%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1644875%22%20slang%3D%22en-US%22%3ERe%3A%20ODBC%20data%20connection%20with%20parameter%20from%20drop%20down%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1644875%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F783767%22%20target%3D%22_blank%22%3E%40michellepace%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20you%20initiate%20connection%20as%20(in%20your%20case%20from%20ODBC)%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20389px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F217028i0C384B0F32829C18%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EOn%20next%20step%20click%20Transform%20(not%20Load)%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20299px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F217029iB9050E4DA1BFADDD%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EOpen%20Home-Advanced%20Editor%20for%20Power%20Query%2C%20it'll%20be%20something%20like%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Sql.Database(%22server%22%2C%20%22database%22%2C%20%5BQuery%3D%22select%20*%20from%20Department%20where%20Id%3D8%22%5D)%0Ain%0A%20%20%20%20Source%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EModify%20code%20as%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3Elet%0A%20%20%20%20parameter%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22selected_pperiod%22%5D%7D%5BContent%5D%7B0%7D%5BColumn1%5D%2C%0A%20%20%20%20Source%20%3D%20Sql.Database(%22server%22%2C%20%22database%22%2C%0A%20%20%20%20%20%20%20%20%5BQuery%3D%22select%20*%20from%20Department%20where%20Id%3D%22%20%26amp%3B%20parameter%20%5D)%0Ain%0A%20%20%20%20Source%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EClick%20Done.%20If%20works%20you%20may%20load%20the%20query%20from%20here%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20220px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F217031iB54C3E5FEC6F9871%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1649598%22%20slang%3D%22en-US%22%3ERe%3A%20ODBC%20data%20connection%20with%20parameter%20from%20drop%20down%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1649598%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%2C%20thank%20you%20once%20again%20for%20your%20reply.%20I've%20made%20some%20progress%20by%20following%20your%20suggestion.%20However%2C%20now%20I%20have%20the%20below%20error.%20Sorry%20to%20ask%20for%20your%20help%20for%20a%20third%20time%2C%20could%20you%20please%20help%20me%20resolve%20it%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22err.png%22%20style%3D%22width%3A%20555px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F217337i2958070DFA2B2793%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22err.png%22%20alt%3D%22err.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22screenshot.png%22%20style%3D%22width%3A%20330px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F217336i050A479567F92EE0%2Fimage-dimensions%2F330x243%3Fv%3D1.0%22%20width%3D%22330%22%20height%3D%22243%22%20title%3D%22screenshot.png%22%20alt%3D%22screenshot.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20for%20what%20it's%20worth%2C%20I%20played%20with%20the%20query%20by%20converting%20the%20parameter%20to%20Text.%20The%20error%20changed%20(a%20little)%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22x.png%22%20style%3D%22width%3A%20929px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F217339i70A88021E4759BCD%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22x.png%22%20alt%3D%22x.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi there,

 

Via ODBC I am connecting to a Pervasive (version 13) database. Is there a way for me to somehow make use of a parameter? For example, in the below screenshot I've shown (although it is not syntactically correct) that I'd like to somehow use the named cell selected_pperiod as a parameter in my query. Is this even possible? And if it is, how would I go about passing a parameter in my SQL query?

 

screenshot.png

 

Many thanks,

Michelle

 

8 Replies

@MichellePace 

Generate query with any hardcoded parameter, e.g. 102. After that in advanced editor add getting parameter string and correct generated query with it, like

let
    parameter = Excel.CurrentWorkbook(){[Name="selected_pperiod"]}[Content]{0}[Column1],

    ...."start query" & parameter & "end query"
Thank you very much for such a prompt reply. I am quite new at connecting to data from within excel. When you say “generate query”... quite honestly all I know how to do is click the “Get Data” button and enter my sql in there. Could you please tell me how to generate the query and where to write that code?

(Sorry I’m catching up on years here of excel inactivity.)

@MichellePace 

First you initiate connection as (in your case from ODBC)

image.png

On next step click Transform (not Load)

image.png

Open Home-Advanced Editor for Power Query, it'll be something like

let
    Source = Sql.Database("server", "database", [Query="select * from Department where Id=8"])
in
    Source

Modify code as

let
    parameter = Excel.CurrentWorkbook(){[Name="selected_pperiod"]}[Content]{0}[Column1],
    Source = Sql.Database("server", "database",
        [Query="select * from Department where Id=" & parameter ])
in
    Source

Click Done. If works you may load the query from here

image.png

@Sergei Baklan , thank you once again for your reply. I've made some progress by following your suggestion. However, now I have the below error. Sorry to ask for your help for a third time, could you please help me resolve it? 

 

err.png

 

screenshot.png

 

And for what it's worth, I played with the query by converting the parameter to Text. The error changed (a little):

x.png

@MichellePace 

Not clear from the screenshot, it could be if [Column1] is missed on first step. What do you see as result if you stay on first step, is it 108?

 

In any case please add one more step in the middle, I forgot you parameter in Excel is number, we need to convert it to text

parameter = Text.From(PPeriod_parameter),

and use the latest in query string.

Best Response confirmed by MichellePace (Occasional Contributor)
Solution

@Sergei Baklan thank you that is exactly it! I added the line you mentioned and it works! For anyone else looking for this solution, here it is:

 

 

let
    pperiod = Excel.CurrentWorkbook(){[Name="reneirs_pperiod"]}[Content]{0}[Column1],
    pperiod_text = Text.From(pperiod),
    Source = Odbc.Query("dsn=PASTEL.odbc", "select * from LedgerTransactions where PPeriod = " & pperiod_text)
in
    Source

 

 

solution.png

I have one Laaaaast Question @Sergei Baklan ... Every time I select a new PPeriod in my yellow dropdown, I have to go and refresh my query for the new data to be reflected in the table. Question: Am I correct that I'll need to write some VBA code to "force" automatic query refresh every time my user selects a new PPeriod?

 

W.png

@MichellePace 

Great to know it finally works, thanks for the result sharing.

 

Yes, if automatic upon parameter is changed that's VBA.  Can't advise with concrete code, but you may google for the pattern or ask as separate question this forum people.