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:
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!
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!