Suggestions on how to alter 'openreport' VBA?

gojets1721

Registered User.
Local time
Today, 08:29
Joined
Jun 11, 2019
Messages
430
I have the below 'openreport' vba which works great to open up a customer's history based on their customer ID. It is a command on a form which opens a customer's full purchase history.

However, i was curious if it was possible to modify so that if the customer ID field in the form reads 'n/a' then the button does nothing and no report is opened.

Suggestions?

Code:
Private Sub btnOpenCustomerHistory_Click()
On Error GoTo btnOpenCustomerHistory_Click_Err

    strReport = "rptPurchaseHistory"
    lngView = acViewReport
    strWhere = "[CustomerID] ='" & Nz(Me.[CustomerID], 0) & "'"
    
    DoCmd.OpenReport strReport, lngView, , strWhere
    
btnOpenCustomerHistory_Click_Exit:
    Exit Sub

btnOpenCustomerHistory_Click_Err:

    MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Error"
    
    Resume btnOpenCustomerHistory_Click_Exit
    
End Sub
 
I have the below 'openreport' vba which works great to open up a customer's history based on their customer ID. It is a command on a form which opens a customer's full purchase history.

However, i was curious if it was possible to modify so that if the customer ID field in the form reads 'n/a' then the button does nothing and no report is opened.

Suggestions?

Code:
Private Sub btnOpenCustomerHistory_Click()
On Error GoTo btnOpenCustomerHistory_Click_Err

    strReport = "rptPurchaseHistory"
    lngView = acViewReport
    strWhere = "[CustomerID] ='" & Nz(Me.[CustomerID], 0) & "'"
   
    DoCmd.OpenReport strReport, lngView, , strWhere
   
btnOpenCustomerHistory_Click_Exit:
    Exit Sub

btnOpenCustomerHistory_Click_Err:

    MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Error"
   
    Resume btnOpenCustomerHistory_Click_Exit
   
End Sub
How do you determine whether the CustomerID control (on the form it's a control) reads "n/a" or something else? What would "something else" be, by the way. When does it display "n/a" and when does it display something else?

Thanks for the details.
 
Before your line that starts with "strWhere = ..."

Code:
If Me.CustomerID = "n/a" Then
'   option here for a MsgBox to tell user you are not going to open the report
    GoTo btnOpenCustomerHistory_Click_Exit
End If

EDIT:
GPGeorge raises a valid question, so consider it before implementing what I just showed you.
 
How do you determine whether the CustomerID control (on the form it's a control) reads "n/a" or something else? What would "something else" be, by the way. When does it display "n/a" and when does it display something else?

Thanks for the details.
So the form is linked to a table full of purchases. And the form looks at each purchase individually. If the purchase was made by a registered customer, the CustomerID will show their ID. If it was made by an unregistered user or guest, it just displays N/A.

The command opens a report to show past purchases by that customerID
 
Before your line that starts with "strWhere = ..."

Code:
If Me.CustomerID = "n/a" Then
'   option here for a MsgBox to tell user you are not going to open the report
    GoTo btnOpenCustomerHistory_Click_Exit
End If

EDIT:
GPGeorge raises a valid question, so consider it before implementing what I just showed you.
This worked! Thank you!
 

Users who are viewing this thread

Back
Top Bottom