DoCmd.OpenReport Code Error

cejeff06

New member
Local time
Today, 08:15
Joined
Aug 21, 2008
Messages
3
Hello! I have a question regarding the VBA code for opening a repoprt (DoCmd.OpenReport). I am using MS Access 2007.

I have button on my form that opens a report when it is clicked. I am using VBA code because I want the query behind the form to change based on a choice on the form. Specifically, the report being opened is called "accessqueryreport" and the query behind it is called "accessquery". The query has 3 variables: "state", "rating", and "gender". It has many records of people rating the state they live in. I want there to be only one average rating for each state, so (in query design view) I have the 'Total' value for "state" set to 'Group By' and the 'Total' value for "rating" set to 'Avg'. If the user selects to only review ratings from men, I want the query to select only those records that have "gender=1" and if the user selects to only review ratings from women, I want the query to select only those records that have "gender=2". So I wrote the following code:

If Choice=1 Then
DoCmd.OpenReport "accessqueryreport", acViewPreview, , "gender = 1"
Else
DoCmd.OpenReport "accessqueryreport", acViewPreview, , "gender = 2"
End If

When I run this code, I get a popup window asking for the parameter value of "gender". What am I doing wrong with this code? Does the fact that I am averaging and grouping cause a problem? I've tried a bunch of different things, but nothing worked. So I decided to ask you all. Any help you can share would be greatly appreciated!

Thanks in advance!
 
Is Gender in the Table the query is working from?
Is Gender spelled the same as it is in Table?
Do you have a Field in the Report (hidden or not) that holds the Gender?
Is Gender in table actually holding a numerical value or is it holding string value such as "Male" or "Female"? If it is then your code should be:

Code:
If Choice=1 Then
   DoCmd.OpenReport "accessqueryreport", acViewPreview, , "gender = [COLOR="Red"][B]'[/B][/COLOR]Male[B][COLOR="Red"]'[/COLOR][/B]"
Else
   DoCmd.OpenReport "accessqueryreport", acViewPreview, , "gender = [B][COLOR="Red"]'[/COLOR][/B]Female[COLOR="Red"][B]'[/B][/COLOR]"
End If

.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom