473,382 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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 2140
Stewart Ross
2,545 Expert Mod 2GB
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.