Trouble with combo box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a combo box on my form. Based on the user clicking the choice in the list, I want a report to run. I can not come up with the code to do this. If I go into the event procedure "on click" and write a docmd.open report, this report opens each time a different choice is made. How do I write it so that it opens a different report for each choice made? Thanks!
 
Try something along these lines

select case combo.ListIndex
case 0 'The first item in the list has been selected
DoCmd.OpenReport <report1>
case 1 ' The second item in the list has been selected
DoCmd.OpenReport <report2>
and so on
end select

Hope That Helps
Gerald Stanley MCSD
-----Original Message-----
I have a combo box on my form. Based on the user clicking
the choice in the list, I want a report to run. I can not
come up with the code to do this. If I go into the event
procedure "on click" and write a docmd.open report, this
report opens each time a different choice is made. How do I
write it so that it opens a different report for each
choice made? Thanks!
 
My recommendation is to use a Select Case statement but put put it behind a CommandButton instead of the ComboBox - knowing how many times users can click or select different values, do you want reports to potentially launch with each one

Try something like this

Private Sub cmdRunReport(

' if a value has not been selected in the combo box, display an error messag
If IsNull([cboSelectReport]) Or [cboSelectReport] = "" The
MsgBox "You forgot to select which report to run. Please try again.",,"Oops!
cboSelectReport.SetFocu
Exit Su
End I

' otherwise, run the report specified in the combo bo
Dim strReport As Strin

Select Case [cboSelectReport
Case
strReport = "rptSalesReport
Case
strReport = "rptCommissionReport
Case
strReport = "rptEmployeeTurnover
Case Els
strReport = rptYouGetTheIdea
End Selec

DoCmd.OpenReport strReport, acViewPrevie

End Su

Hope this helps

Howard Brod


----- karen wrote: ----

I have a combo box on my form. Based on the user clicking the choice in the list, I want a report to run. I can not come up with the code to do this. If I go into the event procedure "on click" and write a docmd.open report, this report opens each time a different choice is made. How do I write it so that it opens a different report for each choice made? Thanks!
 
I have a combo box on my form. Based on the user clicking the choice in the list, I want a report to run. I can not come up with the code to do this. If I go into the event procedure "on click" and write a docmd.open report, this report opens each time a different choice is made. How do I write it so that it opens a different report for each choice made? Thanks!

I'd suggest using a Combo Box (or listbox), cboLaunch, based on a
Query with two fields: the "internal" name of the report (following a
naming convention, such as rptMonthlySales) and a human readable
field, "Monthly Sales Report - enter a date in the textbox". Make the
first column the bound column, with a width of zero, so the user sees
the human-meaningful name.

In the Combo's AfterUpdate event put code like

Private Sub cboLaunch_AfterUpdate()
DoCmd.OpenReport Me!cboLaunch <optional parameters>
End Sub

For instance, you can set the parameters to open the report in Preview
mode or print it directly, etc. - see the online help for OpenReport
for details.
 
I had tried to write different sets of codes using the select case statements, but in the click even procedure on the combo box and I kept getting errors. Now I never thought about doing it behind a different command button. I'll play with it this way and see what happens. Thanks for everyone's input!
 
I had tried to write different sets of codes using the select case statements, but in the click even procedure on the combo box and I kept getting errors. Now I never thought about doing it behind a different command button. I'll play with it this way and see what happens. Thanks for everyone's input!

A button click event, or the Combo's AfterUpdate event would probably
be better.
 
Okay, now this is the code I've put in (2 ways) and I get an error with both that says: Compile error: "Argument not optional"
1st way:
Private Sub cmdrunreport_Click()
On Error GoTo Err_cmdrunreport_Click

Dim stDocName As String

Select Case [cboSelectReport]
Case "MA1"
stDocName = "MA1 All"
Case "MA2"
stDocName = "MA2 All"
Case "MA3"
stDocName = "MA3 All"
End Select

DoCmd.OpenReport , stDocName, acViewPreview

Exit_cmdrunreport_Click:
Exit Sub

Err_cmdrunreport_Click:
MsgBox Err.Description
Resume Exit_cmdrunreport_Click

End Sub
-------
2nd way:
Private Sub cmdrunreport_Click()
On Error GoTo Err_cmdrunreport_Click

Dim stDocName As String

Select Case [cboSelectReport]
Case "MA1"
stDocName = "MA1 All"
DoCmd.OpenReport , stDocName, acViewPreview

Case "MA2"
stDocName = "MA2 All"
DoCmd.OpenReport , stDocName, acViewPreview

Case "MA3"
stDocName = "MA3 All"
DoCmd.OpenReport , stDocName, acViewPreview

End Select



Exit_cmdrunreport_Click:
Exit Sub

Err_cmdrunreport_Click:
MsgBox Err.Description
Resume Exit_cmdrunreport_Click

End Sub
 
Back
Top