View Full Version : VBA+SQL: Help for beginner


gebli
10-15-2008, 11:57 AM
Hi - I hate to ask this question because I'm sure somewhere in the forum is the answer, but I just could not find it and I need to start moving...

Can anybody help me by providing a simple example showing how I can attach a query to a form object? I mean: I need to show several boxes containing several results (of several queries). For example: a box could display the total number of orders placed in Florida, another one the total for New York, etc. So, I think what I need is a piece of VBA code that should run a query based on a parameter (e.g. STATE).

Thanks in advance,

Gerry

MSAccessRookie
10-15-2008, 12:06 PM
Hi - I hate to ask this question because I'm sure somewhere in the forum is the answer, but I just could not find it and I need to start moving...

Can anybody help me by providing a simple example showing how I can attach a query to a form object? I mean: I need to show several boxes containing several results (of several queries). For example: a box could display the total number of orders placed in Florida, another one the total for New York, etc. So, I think what I need is a piece of VBA code that should run a query based on a parameter (e.g. STATE).

Thanks in advance,

Gerry


The following code might be behind a Form Button titled "Intl. Shipping / Route":


Private Sub IntlShippingbyRouteButton_Click()
DoCmd.OpenQuery "qryInternationalShippingByRoute", acViewNormal, acReadOnly
End Sub


The query qryInternationalShippingByRoute provides a report based on input parameters including shipper name and date range

gebli
10-15-2008, 12:33 PM
Thanks MSAccessRookie for your prompt feedback.
You did clarify how to run a query from within VBA but where is the returning result stored? That is: if the query is a count(*), how can I display the result in a text box?

Thanks again.

MSAccessRookie
10-16-2008, 05:07 AM
Thanks MSAccessRookie for your prompt feedback.
You did clarify how to run a query from within VBA but where is the returning result stored? That is: if the query is a count(*), how can I display the result in a text box?

Thanks again.


Now that's another question entirely! The example that I gave will create a pop-up datasheet containing the results of the query. To display the result in a text box, you need to create a text box with properties that include a recordsource based on the query, and a control source based on the column that contains that data that you want to display.

gebli
10-16-2008, 11:29 AM
I see... I'll do some research in that direction. Thanks!