CrossTab Query (SQL View) in VBA

L

Leo

Hi -

No one has answered this message posted yesterday. Please Help

Any idea why the SQL queries that are created by the Cross Tab Query Wizard
cannot be ran in VBA?

It starts off with the 'TRANSFORM' statement, as opposed to the usual
'SELECT' statement
IS this the reason why? Or is it because I have 'PARAMETERS' defined in the
SQL statement?

How would I ran an SQL statement created by a Cross Tab query in VBA?

Please Advise
 
M

[MVP] S.Clark

Only Action queries get "ran" from VBA.

Crosstabs, or any other Select query can be loaded into a recordset in VBA.

HTH,
 
G

Gary Walter

Leo said:
No one has answered this message posted yesterday. Please Help

Any idea why the SQL queries that are created by the Cross Tab Query Wizard
cannot be ran in VBA?

It starts off with the 'TRANSFORM' statement, as opposed to the usual
'SELECT' statement
IS this the reason why? Or is it because I have 'PARAMETERS' defined in the
SQL statement?

How would I ran an SQL statement created by a Cross Tab query in VBA?
Hi Leo,

I believe you have a xtab query that expects a parameter
which you can run in the query designer with no problem.

But, when you try to open a recordset to it in VBA,
you find that you don't get a prompt for the parameter,
or, it cannot find the parameter if it references a form control.

In VBA you must provide the parameter value yourself.

There are several scenarios, depending on if you were
using DAO or ADO, and, if the parameter referred to
a form control or not.

========================================
DAO, parameter refers to form control of form that is open
========================================

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Set db = CurrentDb
Set qdf = db.QueryDefs("nameofyourquery")
For Each prm In qdf.Parameters
'following only works if parameter(s) are controls on open form
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset(dbOpenDynaset)

========================================
DAO, parameter(s) do not refer to form control
========================================

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Set db = CurrentDb
Set qdf = db.QueryDefs("nameofyourquery")
For Each prm In qdf.Parameters
'following works if parameter(s) are not controls on open form
prm.Value = InputBox("Value for " & prm.Name)
Next prm
Set rst = qdf.OpenRecordset(dbOpenDynaset)

If you are using ADO, please provide copy of
your code and will try to modify for you.

Good luck,

Gary Walter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Executing SQL from Cross Tab Wizard in VBA 1
crosstab queries 1
Union Query with two crosstabs 2
Crosstab - What am I doing wrong? 10
SQL query 1
Crosstab query 4
Ambiguous outer join 6
Query question 3

Top