Suggestions on how to alter 'openreport' VBA? (1 Viewer)

gojets1721

Registered User.
Local time
Today, 02: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
 

GPGeorge

Grover Park George
Local time
Today, 02:29
Joined
Nov 25, 2004
Messages
1,867
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.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:29
Joined
Feb 28, 2001
Messages
27,186
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.
 

gojets1721

Registered User.
Local time
Today, 02:29
Joined
Jun 11, 2019
Messages
430
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
 

gojets1721

Registered User.
Local time
Today, 02:29
Joined
Jun 11, 2019
Messages
430
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

Top Bottom