Search code examples
ms-accessreport

Open Report if field contains certain word


We have a form with 2 buttons pointing to 2 reports, ideally we would like one button on the form to prevent user printing incorrect certificates

Is there a way to have only one button so when a user clicks on the button to run a report, that

if [type] contains "semi" open report "semi cert" & if [type] contains "spring" open report "spring cert"

these are our currents buttons

Private Sub semi_Click()
    DoCmd.OpenReport "Quick Hitch (SEMI) Certificate", acViewPreview, , "[no] = " & Me.[no]
    DoCmd.PrintOut
    DoCmd.Close
End Sub

Private Sub spring_Click()
    DoCmd.OpenReport "Quick Hitch (SPRING) Certificate", acViewPreview, , "[no] = " & Me.[no]
    DoCmd.PrintOut
    DoCmd.Close
End Sub

Solution

  • Assuming that type is the name of your text box

    Private Sub OpenReport_Click()
        Dim reportName As String
    
        Select Case Me.[type].Value
            Case "semi"
                reportName = "Quick Hitch (SEMI) Certificate"
            Case "spring"
                reportName = "Quick Hitch (SPRING) Certificate"
            Case Else
                MsgBox "Report type '" & Me.[type].Value & "'is unknown"
                Exit Sub
        End Select
        DoCmd.OpenReport reportName , acViewPreview, , "[no] = " & Me.[no]
        DoCmd.PrintOut
        DoCmd.Close
    End Sub