473,467 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to write a VBA procedure to output a Report based on parameters?

9 New Member
Greets,

I am wondering how I can go about creating a VBA procedure that will accept a parameter, provide that parameter to a query and then output to HTML a form based on that query.

The report "YourSelection" is created with a record source "Query1". Query1 will prompt the user to select a userID number and return a recordset of all records in the db with that ID. I want to be able to save that report as HTML without having to use the propmt.

Here's what I have so far, perhaps someone can help explain why it isn't working as I expected it to.

Expand|Select|Wrap|Line Numbers
  1. Public Function Test(ByVal selection As Integer) As Boolean
  2.     Dim qdf As QueryDef
  3.  
  4.     Set qdf = CurrentDb.QueryDefs("Query1")
  5.     qdf.Parameters(0) = selection
  6.  
  7.     DoCmd.OutputTo acOutputReport, "YourSelection", _    
  8.         acFormatHTML, "request.html"
  9.  
  10.     qdf.Close
  11.     Set qdf = Nothing
  12.  
  13.     ReturnTest = True
  14. End Function
  15.  
Two things are not working here. The first is that I still get prompted to provide the parameter to Query1. Second, no matter what the function always returns False. Though the second issue isn't really that big, if anyone has any ideas on that, I'd appreciate those as well.

Thanks a lot,
Jason
Feb 2 '11 #1

✓ answered by Stewart Ross

At least as an initial comment I can explain why your parameter query is not apparently working. It's because there is no actual relation at all between the Querydef object you have initialised and set in lines 4 and 5, and the (independent) copy of the same query which is actually used as the recordsource of the report opened in line 7.

The local object variable qdf is not the recordsource of the report. It's just a local copy of the query that happens to underlie your report. You are actually creating a separate copy of the querydef concerned, and unfortunately there is no way I know of you can assign the parameter-supplied qdf variable to your report through VBA code as it stands. The recordsource of your report, which you can set using VBA, is simply the name of an existing query, or an SQL statement which itself provides the equivalent query. It is not a querydef object as such.

This is one of those occasions when the apparent advantages of allowing users to enter parameter values directly into a query are more than offset by the problems of using the same query to base a report upon.

One way to handle this is to remove the parameters from the query and instead use an unbound form to allow the user to select the relevant parameters for the report. You can use the form's On Load event to populate the various unbound controls with default values for the user should the user not wish to make selections before opening the report.

Using the values of the controls on the form it is straightforward to construct a filter string in VBA which can be applied as an argument of the OpenReport metod to restrict the records to the range concerned. The filter string is really an SQL WHERE clause but without the keyword WHERE.

Another way is to use the form controls as parameters of your report's recordsource query in the same way as the user-entered parameters would be, but this is less flexible because the query itself will only function without requiring parameter subtitution if the form concerned is open at the time.

The reason your function is returning False is that you have a misnamed (and untyped) variable reference at line 13
Expand|Select|Wrap|Line Numbers
  1. ReturnTest = True
Your function is actually named Test.

The VBA compiler would have warned you of this undeclared variable if you were using the Option Explicit compiler directive at the top of your code module (in which case all instances of undeclared variables would be treated as errors).

-Stewart

1 2141
Stewart Ross
2,545 Recognized Expert Moderator Specialist
At least as an initial comment I can explain why your parameter query is not apparently working. It's because there is no actual relation at all between the Querydef object you have initialised and set in lines 4 and 5, and the (independent) copy of the same query which is actually used as the recordsource of the report opened in line 7.

The local object variable qdf is not the recordsource of the report. It's just a local copy of the query that happens to underlie your report. You are actually creating a separate copy of the querydef concerned, and unfortunately there is no way I know of you can assign the parameter-supplied qdf variable to your report through VBA code as it stands. The recordsource of your report, which you can set using VBA, is simply the name of an existing query, or an SQL statement which itself provides the equivalent query. It is not a querydef object as such.

This is one of those occasions when the apparent advantages of allowing users to enter parameter values directly into a query are more than offset by the problems of using the same query to base a report upon.

One way to handle this is to remove the parameters from the query and instead use an unbound form to allow the user to select the relevant parameters for the report. You can use the form's On Load event to populate the various unbound controls with default values for the user should the user not wish to make selections before opening the report.

Using the values of the controls on the form it is straightforward to construct a filter string in VBA which can be applied as an argument of the OpenReport metod to restrict the records to the range concerned. The filter string is really an SQL WHERE clause but without the keyword WHERE.

Another way is to use the form controls as parameters of your report's recordsource query in the same way as the user-entered parameters would be, but this is less flexible because the query itself will only function without requiring parameter subtitution if the form concerned is open at the time.

The reason your function is returning False is that you have a misnamed (and untyped) variable reference at line 13
Expand|Select|Wrap|Line Numbers
  1. ReturnTest = True
Your function is actually named Test.

The VBA compiler would have warned you of this undeclared variable if you were using the Option Explicit compiler directive at the top of your code module (in which case all instances of undeclared variables would be treated as errors).

-Stewart
Feb 6 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Richard | last post by:
Hi, I have a form based on a table. When I filter the form I want to run a report based on the same table with the same filter as the form. No problem until I want to filter a combo box where...
0
by: Richard Hollenbeck | last post by:
I have a crosstab query that shows all the scores of all the activities of all the students in all courses, with the students being in the rows and the activities being in the columns and the...
1
by: gregoryenelson | last post by:
Hi all. I have created a Report with a graph also on it. The users, working through a form only (not the DB window) will want to view that Report run numbers of times against different criteria,...
0
by: mike11d11 | last post by:
I have a form with a dataset that contains account records in one table and another table that contains transactions for accounts that are in the other table. I'm trying to create a report based...
0
by: ivce970 | last post by:
Hi everyone, I'm wruiting this message again cause I really need some help. I have installed VS NET 2003 and SQL Server 2005 Expess edition. I made a db for my small business with books,...
5
by: jonosborne | last post by:
Hi, i have managed to filter a report based on selections made in a list box but am totally confused with a message box that appears everytime i run my report. Let me explain (i apologise for...
1
by: zoro25 | last post by:
Hi, I created a report based on a crosstab query and I need to add some filtering either in the report or in the query but can't find how to do it. For example, my report contains Site Name,...
13
by: masteraccess2008 | last post by:
I create in Access report based on user defined function(UDF) in SQL Server which returns table. CREATE FUNCTION MyFunc_VP (@VP varchar(12)) RETURNS TABLE AS RETURN (SELECT...... In Access,...
4
by: anchu | last post by:
Respected sir/madam i want to generate a report and display the information in report based on specific id for example: if i enter roll no of a student in textbox so only this information should...
1
by: reachravi70 | last post by:
Hello Users, I am new to PERL. There is a requirement to write a PERL script to query the Oracle data based on the user input date parameter and write the output to Excel. I have the following...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.