1

I am learning how to use parameters in an excel-driven SQL query (in fact I am still learning SQL in general). Thanks to the nice people that helped me build my query to modify the results as I need, I want to take this a step further and supply a parameter in Excel to filter the results.

Here is my query:

SELECT 
    fun.FUNCTION_ID
    ,COALESCE(fun.parent_function, fun2.function_id) as PARENT_FUNCTION
    ,fun.MODULE_ID
    ,fun.DESCRIPTION
    ,fun.FUNCTION_PURPOSE
    ,fun.PB_OBJECT
    ,sec.GROUP_ID
    ,sec.ACCESS_LEVEL
from 
    MODULE_FUNCTION fun

    LEFT JOIN MODULE_FUNCTION fun2
    ON fun.function_id = fun2.function_id
    AND fun2.function_id IN (SELECT parent_function FROM MODULE_FUNCTION)

    LEFT OUTER JOIN FUNCTION_SECURITY sec
    ON fun.FUNCTION_ID = sec.FUNCTION_ID
    AND sec.GROUP_ID = 'GROUP_NAME'

What I need to do is allow people from a team to run this query in the excel sheet and supply their group name for the "GROUP_NAME" in the second JOIN. Unfortunately I cannot use the syntax WHERE (sec.GROUP_ID = ?) (found here) as I need to pull all results from the MODULE_FUNCTION table and only insert results on the right from the FUNCTION_SECURITY table when there is a match on the supplied group (leaving null when there is no match).

When I try to use AND (sec.GROUP_ID = ?) at the end I get a "Invalid Parameter Number" in Excel. From what I have gathered, the "?" can only be use with WHERE (and works find for me in test queries).

I have tried many things, including declaring a @parameter, but no avail.

I'm tempted to try this technique but I'd like to avoid VB if possible.

Community
  • 1
  • 1
alteredNate
  • 89
  • 2
  • 8

1 Answers1

4

I know you said you want to avoid VB, but it isn't too complicated for what you want to do.

You can have the sheet have a cell for the group name, then a button that calls a macro where you would change the sql query to adjust for the group_id.

Something like:

Dim sql As String

sql = "select ... from ... and sec.GROUP_ID = '?'"
sql = Replace(sql, "?", Worksheets("Analysis").Range("A1").Value)

With ActiveWorkbook.Connections("connection name").OLEDBConnection
    .CommandText = sql
    .Refresh
End With

Where:

Worksheets("Analysis").Range("A1").Value

is the Group_ID. You can set this to a specific cell in any sheet in your workbook. I would create a button right next to it called "Refresh table" or something like that.

If you already made a table that links to a database, then there is a connection object in Excel. Go to the Data tab, then click "Connections". A new window will pop up. Find the connection that matches to the SQL query. Click on that connection and click "Properties" then change the connection name to something easy (it's usually some long name based on the server/table you connect to). Use that for the

ActiveWorkbook.Connections("connection name")

section.

Link to create button on worksheet and link to macro:

http://office.microsoft.com/en-us/excel-help/add-a-button-and-assign-a-macro-to-it-in-a-worksheet-HP010236676.aspx

Joseph
  • 5,070
  • 1
  • 25
  • 26
  • Is it really that simple? I am mystified by VB but this seems easy. I will try later and let you know. – alteredNate Dec 11 '12 at 16:21
  • @alteredNate well, it can be confusing at first, but it's not so bad once you get into the swing of things. I wasn't sure how much you knew about this, so if you need more help with implementing my answer, please let me know. I'll be happy to explain in greater detail. – Joseph Dec 11 '12 at 16:40
  • Well I know very little actually :) I was about to dive into your code and the one I linked to and try and understand. If you are up for some more detailed explination for my specific case, I would certianly be grateful! Between SQL queries, pivot tables, and database management I'm learning alot at the moment, and VB wasn't initially on that list! ha! – alteredNate Dec 11 '12 at 20:38
  • @alteredNate Got it. I updated the answer to include more info and a link to how to create a button that is linked to a macro. Also, YouTube is a great place to find easy videos on starting with VBA. example: http://www.youtube.com/watch?v=zt3Yan14ERs – Joseph Dec 11 '12 at 20:53
  • Thanks for that! I have followed everything as best as I can, I got the connection name, sheet name containing the value that I want, and I wrapped your VB into a Sub to make it a macro to link to the button. I carefully inserted my SQL into the string on one long line, but I get a "Type Mismatch" error. (I tried the VB debug and apparently it gets stuck on the `.CommandText` step ("Run-time Error '13' - Type Mismatch). I know it's not the connection string or the sheet name because if I fake those I get a different error. Any idea? – alteredNate Dec 11 '12 at 21:38
  • @alteredNate try adding this right before the .CommandText line: ".CommandType = xlCmdSql" (without quotes - it might currently be xlCmdTable or something else) – Joseph Dec 11 '12 at 21:48
  • @alteredNate, you know what? can you post the code you wrote? sounds like it's not a string that you end up passing to the .CommandText section. – Joseph Dec 11 '12 at 21:50
  • The .CommandType didn't change anything. However I think I may have found something, I tried this out initially on a new worksheet where I created a new connection from scratch to the server. Looking in the properties of the connection, it says "Connection Type: Office Data Connection" - I just tried to add the VB & macro to my existing sheet, and its Connection Type says "Database Query" - they have quite different connection strings. On the original sheet, with everything set up the same, I get a different VB error "Error '445' - Object doesn't support this action"? – alteredNate Dec 11 '12 at 22:01
  • here is the VB: `Sub Run_Query() Dim sql As String sql = "select ... sec.GROUP_ID = '?'" sql = Replace(sql, "?", Worksheets("groups").Range("A1").Value) With ActiveWorkbook.Connections("permissions").OLEDBConnection .CommandType = xlCmdSql .CommandText = Array(sql) .Refresh End With End Sub` – alteredNate Dec 11 '12 at 22:03
  • @alteredNate tell you what, check out this post i made about creating pivot tables using SQL: http://www.spreadsheetsmadeeasy.com/excel-create-pivot-table-using-sql/ If you make the connection like this, and then modify the connection with VBA, I think it will work for you. Also, I would like to go to chat with you, but you don't have a rep of 20 yet :/ maybe you can email me? (check my profile) – Joseph Dec 11 '12 at 22:15
  • yep, new here, and having a really hard time answering anything as I'm a new coder but I keep looking at questions. I did in fact set up my connection exactly as you describe on that site. One dumb question, in my case, do I need the query to be in the connection? Meaning, I understood that the query is entirely generated by the VB, so I didn't put it in the connection..? We can go to email :) – alteredNate Dec 11 '12 at 22:38
  • Can't find an email link, must not have enough reputation. arg! – alteredNate Dec 11 '12 at 23:13
  • Didn't think to look for it in your personal site. I'm off to bed, I'll write you in the morning :) – alteredNate Dec 11 '12 at 23:43