Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write SQL in VBA for rowsource of chart

Status
Not open for further replies.

nederbragt

Technical User
Dec 19, 2003
2
NL
I want to make the rowsource of a chart on a form variable.
I want to choose for wich product the chart must drawn.

I do have the next SQL statement.

TRANSFORM Count(qryCrediteringenVT.Product) AS
CountOfProduct
SELECT (Format([Datum],"mmm"" '""yy")) AS Expr1
FROM qryCrediteringenVT
WHERE (((qryCrediteringenVT.Product)="Test"))
GROUP BY (Year([Datum])*12+Month([Datum])-1), (Format
([Datum],"mmm"" '""yy"))
PIVOT qryCrediteringenVT.Product;

Can anyone help me to write this SQL statement in VBA, so I can make the criteria of the product variable ?

Any suggestions welcome, otherwise I have to make a new query for every new product.
 
One method is to create a combo box on your form for your WHERE clause. Call it txt_WHERECLAUSE. Then create a command button which will change the rowsource for your chart. You can reference your txt_WHERECLAUSE in your SQL statement like this...

[Forms]![Your form name]![Your chart object name].rowsource=TRANSFORM Count(qryCrediteringenVT.Product) AS
CountOfProduct
SELECT (Format([Datum],"mmm"" '""yy")) AS Expr1
FROM qryCrediteringenVT
WHERE (((qryCrediteringenVT.Product)= " & txt_WHERECLAUSE & "))
GROUP BY (Year([Datum])*12+Month([Datum])-1), (Format
([Datum],"mmm"" '""yy"))
PIVOT qryCrediteringenVT.Product;
DoCmd.RepaintObject acForm,"Your form name"
DoEvents

This should refresh your chart and update it with your newly selected criteria.
 
Thank you for the response.

I only have one more problem.
There are to many quotes in de code.

The SQL statement I have written is the one from a query.
I need to transform the code in VBA language.

 
replace the " in your format statements with '

This should then work - he says confidently

If at first you don't succeed, try for the answer.
 
...a little too confidently.

What you want to do is enclose the text parameter in single quotes, just like you would if you were writing the sql directly in the query window. It's just two small changes to one line:

WHERE (((qryCrediteringenVT.Product)= '" & txt_WHERECLAUSE & "'))

Note the single quotes.

Also, you'll have to concatenate all of that, or else put it all on one, long, ugly line in your code.

You can do that like this:
[Forms]![Your form name]![Your chart object name].rowsource= "TRANSFORM Count "_
& " (qryCrediteringenVT.Product) AS " _
& " CountOfProduct " _

etc.

Oh, now I see what the last poster was saying. The quotes in all of the format commands _will_ have to be changed to single quotes.

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top