VBA + checkbox to select report

Rik_StHelens

Registered User.
Local time
Today, 20:54
Joined
Sep 15, 2009
Messages
164
Hi

i am new to using VBA, so please excuse my ignorance.

I am trying to use the following code to select a full or summary report:


Private Sub Command25_Click()

If Me.Check20 = False Then DoCmd.OpenReport (rptRemainingRubber)
Else: If Me.Check20 = True Then DoCmd.OpenReport (rptRemainingRubberSummary)

End Sub


It says there is an error with the "Else without IF" line.

Any help is appreciated.

Thanks!
 
So, from what I understand the of the structure, if I changed the Else to and ElseIf that would work.

However, I get the same error.

Im probably missing something obvious, but I never ever write VBA so i'm a newbie to it.
 
Code:
If Me.Check20 = False Then 
    DoCmd.OpenReport(rptRemainingRubber)
Else
    DoCmd.OpenReport  (rptRemainingRubberSummary)
End If
 
ahh ok,

i see where I was going wrong now.

I get an error as follows now:

action or method requires argument report name

it picks up this line on the debugger:

DoCmd.OpenReport (rptRemainingRubberSummary)


I have verified that this is the correct report name and can't see the problem
 
My bad - needed quotes!

However, it now prints the report, rather than opens it!

I never expected this to be so awkward.
 
Code:
DoCmd.OpenReport "rptRemainingRubberSummary", [B][COLOR="Blue"]acViewPreview[/COLOR][/B]
 
I prefer to use a combo box for the user to select their report [less form design mess and maintenance when adding or removing reports ] and in the after update of the combo box I use a Select Case to determine which report to open. Then I use an If statement asking the user if they want to preview the selected report or print it.

Code:
Private Sub cboPrintReports_AfterUpdate()
On Error GoTo Err_cboPrintReports_AfterUpdate

    Me.txtHidden.SetFocus

    Dim sReportName As String
    
    Select Case cboPrintReports
        
        Case "Report #1"
            sReportName = "rptReport1"
        
        Case "Report #2"
            sReportName = "rptReport2"
    
        Case "Report #3"
            sReportName = "rptReport3"

        Case Else
            'Do nothing
    End Select

    If MsgBox("Do you want print or preview the ''" & cboPrintReports & "'' report?" & vbCrLf & vbCrLf & "Click the Yes button to print the selected report." & vbCrLf & "Click the No button to preview the select report.", vbQuestion + vbYesNo, "Print Or Preview Selected Report") = vbYes Then
            DoCmd.OpenReport sReportName, acViewNormal
            MsgBox "The selected report has been sent to your default printer.", vbInformation, "Printed " & cboPrintReports & " Report"
    Else
            DoCmd.OpenReport sReportName, acViewPreview
    End If

    cboPrintReports = Null

Exit_cboPrintReports_AfterUpdate:
    Exit Sub

Err_cboPrintReports_AfterUpdate:
    MsgBox Err.Number & " - " & Err.Description, vbCritical, "cboPrintReports_AfterUpdate()"
    Resume Exit_cboPrintReports_AfterUpdate

End Sub
 

Users who are viewing this thread

Back
Top Bottom