One Command Button for many Reports

  • Thread starter Francis Cunningham, Jr.
  • Start date
F

Francis Cunningham, Jr.

How do I call up a specific report based on 2 fields on a form?

To explain myself. I already have the form made, and on the form are the
fields ContactID and ContractType. I would like the create a command button
on this form and when clicked it would open the report based on the ContactID
and Contracttype (on the report will be the contacts mailing info, and the
correct contract will be chosen because of the Contracttype) there are 5
report possibilities. The ContactID and Contracttype are fields on two
different tables.

I have 5 reports, each design is different. I would like the form field
‘Contracttype’ determine which report opens. The ContactID will supply the
mailing address.
This is the code I am using:

Private Sub Print_Click()
Dim strReportName As String
Select Case Me.ContractType
Case "Electric"
Me!ContactID = ContactID
DoCmd.OpenReport "Electric"
Case "Gas"
Me!ContactID = ContactID
DoCmd.OpenReport "Gas"
Case "Oil"
Me!ContactID = ContactID
DoCmd.OpenReport "Oil"
Case "HeatPump"
Me!ContactID = ContactID
DoCmd.OpenReport "HeatPump"
Case "Cooling"
Me!ContactID = ContactID
DoCmd.OpenReport "Cooling"
End Select

End Sub

Could someone correct my errors. It’s getting frustrating.
Frank
 
S

Steve Schapel

Frank,

If the name of the report is always the same as the value of the
ContractType control on the form, then I think you can shortcut the code
like this:

Private Sub Print_Click()
DoCmd.OpenReport Me.ContractType
End Sub

I don't understand the meaning of the ContactID or what you are trying
to achieve... can you try again to explain please?
 
F

Francis Cunningham, Jr.

For Steve
I going to start over. These field names are correct, disregard previous.
I have two fields, among others, on a particular form. One field
‘ContractNumber’ represents the specific customer and the ‘ContractType’
which represents the specific report. None of the reports are the same. Based
on the ‘ContractNumber’ I would like to have a command button open the
‘ContractType’ for the particular customer. I realize I can have a many
command buttons, one for each report and have the ‘ContractNumber’
Do the search for the report. I want one button to open different reports.

The reports are named: Gas, Electric, Oil, HeatPump and Cooling.
The ContractNumber is actually the customers phone number.
The ContractType’s are: Gas, Electric, Oil, HeatPump and Cooling.
 
S

Steve Schapel

Frank,

So, you mean you want to enter the required ContractNumber and
ContractType in the controls (there is no such thing as a "field" on a
form - fields are in tables) on the form. And then you want the
applicable report to print for the selected customer? So we could
select customer "12345" and then for that customer, we choose whether
it's a Gas or Electric or whatever report you want for that customer?
And the ContractNumber field is included in the queries that the reports
are based on? Ok, like this:

DoCmd.OpenReport Me.ContractType, , , "[ContractNumber] = '" &
Me.ContractNumber & "'"
(assumes ContractNumber is a text data type, which would be expected for
a phone number)

By the way, just to clarify, this is using a VBA procedure, not a macro.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top