views:

365

answers:

3

Here is my query:

PARAMETERS ...
TRANSFORM ...
SELECT ...
...
PIVOT Mytable.type In ("Other","Info");

This is a cross query and I need to set all the column headings with this row: PIVOT Mytable.type In ("Other","Info") and now I have hard-coded the headings, Other and Info.

But I want to do this dynamically. So what I want to do is to call a vba-function that returns all the headings I need.

Something like this:

PIVOT Mytable.type In (myVbaFunction());

So my question is: How to call a vba-function inside the sql-query?

A: 

As you have stated that you're using Access, then (out of the top of my head) yes, it's possible to use VBA functions in queries.

Check on the documentation and MSDN.

Paulo Santos
Do you know how I should do the call or where, more specific than MSDN, I can find more information about this?
Johan
+1  A: 

Yes, it is possible.
However, I don't think it's possible with "WHERE IN (...)".

Here is an example for a normal WHERE query:

Public Function Test() As String
    Test = "Smith"
End Function

...and then:

SELECT * FROM Users WHERE Name = Test();

It works, as long as the function only returns one value.
But I think it's not possible to let your function return something like "Smith, Miller" and use that like:

SELECT * FROM Users WHERE Name In (Test());

(at least I don't know how to do it)

haarrrgh
You are correct -- a function cannot be used for IN () clauses. The only solution within Access is to write the SQL on-the-fly.
David-W-Fenton
A: 

Why not adding those headings in a table and join that table in your xtab query ?
That's probably easier to maintain that hard coding it in a function.

iDevlop
Because the format must be `PIVOT Mytable.type In ("Heading 1", "Heading 2")` and with `PIVOT Mytable.type In (SELECT name FROM Mytable)` I don't get that format.
Johan
Ok, but if you join a table that includes a field like "ColHeader" that has only the 2 or 3 values that you want to allow,you won't need the in() criteria. Plus, if you setup that table properly, the inner join will automatically exclude the non relevant records.
iDevlop