Kathy Dz said:
I have 4 radio buttons
Feasibility
Project Plan
Change Request
Status Report
Then I have a list box of 300+ projects
and the radio buttons and listbox are in an option group
What I would like to do is
Click radio button for project plan and 1 project from the list box and
then click command button to print report.
Can this be done???
It's easy with VBA code. Because macros do not have the capability for
error handling, the only ones I ever use are AutoKeys and AutoExec (and
rarely use AutoExec any more, now that the Tools | Startup options are
available).
The Command Button Wizard will give you a start, by writing some basic
error handling and a basic DoCmd.OpenReport statement. Here's some example
code
On Error GoTo Err_cmdPreviewReport_Click
Dim stDocName As String
stDocName = "Employees"
DoCmd.OpenReport stDocName, acPreview
Exit_cmdPreviewReport_Click:
Exit Sub
Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click
To see the code, after you generate a Command Button, in the button's
Properties, in the Event tab, click the three dots at the far right of the
Click event. I'm going to assume the name of your OptionGroup is
grpReport, that the values associated with the reports are as follows, 1 -
Feasibility, 2-ProjectPlan, 3-ChangeRequest, and 4-StatusReport, and the
names of the reports are: rptFeasibility, rptProjectPlan,
rptChangeRequest, and rptStatusReport. For conservation of screen real
estate and for the extremely helpful AutoExpand feature, where the list
scrolls to the first match as you type, I'd suggest you use a ComboBox
instead of the List Box. (It's not very user-friendly to force the user to
scroll through a list of hundreds of projects, or any other item for that
matter.) I'm going to call that Combo Box, cboProjects. I am also going
to assume that the value returned from the Combo is a long integer that is
the Field named Project in the table or query you use for the RecordSource
of the Report. (If it's a text Field rather than numeric, post back and
I'll add the necessary quote marks.)
(NOTE: It's just a personal preference, but I would use a Combo Box to
select the report, too -- it's no more difficult for the user and would
save some code, I think.)
You'll need to modify the generated code for selection of the Report name,
and to add a WhereCondition clause (see Access Help by putting the cursor
in the OpenReport and pressing F1). The following is the generated code,
as modified:
On Error GoTo Err_cmdPreviewReport_Click
Dim stDocName As String
Dim strWhere as String 'ADDED: to contain the WhereCondition
If Me.optReport = 1 Then 'ADDED: to select the Report based on
the
stDocName = "rptFeasibility" ' Option Group value
ElseIf Me.optReport = 2 Then
stDocName = "rptProjectPlan"
ElseIf Me.optReport = 3 Then
stDocName = "rptChangeRequest"
ElseIf Me.optReport = 4 Then
stDocName = "rptChangeRequest"
Else
MsgBox = "Please select a Report and try again"
GoTo Exit_cmdPreviewReport_Click
End If
If Not IsNull (Me.cboProject) Then 'ADDED: to specify which Project
is
' to be the
subject of this Report
strWhere = "[Project] = " & Me.cboProject
DoCmd.OpenReport stDocName, acPreview,,strWhere
Else
MsgBox "Please select a Project and try again"
End If
Exit_cmdPreviewReport_Click:
Exit Sub
Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click
Now, the caution is... this is untested "air code", but I have done a lot
of similar code to preview reports and it won't be far off (I strongly
suggest you preview the report and let the user look it over before they
decide whether to print it; the user might have accidentally clicked on
the wrong project or report, and want to ignore this and try again before
they print.) Substitute your Control and Report names, and see if that
doesn't do what you want.
Larry Linson
Microsoft Access MVP