Check field for error and stop procedure (1 Viewer)

DL101

New member
Local time
Today, 12:41
Joined
Jun 20, 2017
Messages
9
I have a search form that users can open an existing repair order or create a new repair order by selecting from the search results that show in a subform.

I want to do some error handling and the one procedure I'm having issues with is when a user types in a search and gets NO results but then clicks the Create R.O. button. The button will try to pass the [CustID] to the next form it opens, but the next form errors out because there is no value passed.

I want a Msg Box to display "No Customer Selected" and the button procedure to exit, but I'm not having any luck. Here is the code I've tried for the for the button.

Code:
Private Sub CreateRO_Click()

 If IsError(CustID) Then
    MsgBox "No Customer Selected"
            Exit Sub
    End If

DoCmd.OpenForm "NewRepairOrder", , , , acFormAdd
Forms!NewRepairOrder.CustID = Me.[CustomerSearchSubForm].[Form]![CustID]
DoCmd.Close acForm, "CustomerSearch"

I've tried it with: If IsNull(CustID), and If IsError(CustID). Every time the procedure runs and opens the "NewRepairOrder", but then gives me a Runtime error because it's missing the [CustID].

Any help would be greatly appreciated.

Thanks,
Dustin
 

isladogs

MVP / VIP
Local time
Today, 20:41
Joined
Jan 14, 2017
Messages
18,300
Why don't you just disable the button if there are no search results
 

DL101

New member
Local time
Today, 12:41
Joined
Jun 20, 2017
Messages
9
Thanks for the suggestion Ridder. What would you suggest to disable to the button. I tried variations of the following, including IsNull and IsError, in the On Current event in both the form and subform and didn't have any luck with disabling the button.


Code:
If IsNull(CustomerSearchSubForm!CustID) Then
CustomerSearch!CreateRO.Enabled = False
Else
CustomerSearch!CreateRO.Enabled = True
End If
 

isladogs

MVP / VIP
Local time
Today, 20:41
Joined
Jan 14, 2017
Messages
18,300
I would try using the Nz function

Also, if you are referring to a control on a form the correct notation is:
Code:
Forms!CustomerSearch.CreateRO.Enabled = False

However, as you are running this code from the form itself, use Me. instead
Code:
Me.CreateRO.Enabled = False

So combining, we get
Code:
If Nz(Me.CustID,"") ="" Then
      Me.CreateRO.Enabled = False
Else
      Me.CreateRO.Enabled = True
End If

If that still gets you nowhere, you need to find out what value for CustID is being stored when the button is clicked

Try one or more of these before the If ... End If code above to print the output (if any) to the Immediate window:
Debug.Print Me.CustID
Debug.Print Len(Me.CustID)

or if you prefer use MsgBox Me.CustID or MsgBox Len(Me.CustID)
 

DL101

New member
Local time
Today, 12:41
Joined
Jun 20, 2017
Messages
9
Thanks for all the help Colin, it was greatly appreciated!!!

I continued to try disabling the button in the form and subform with every different check I could come up with; IsNull, IsError, Nz, etc, and wasn't able to get any of those suggestions to work. So, ultimately I just decided to check the subform source query that's showing the customer search results for 0. This is the code I came up with and it works. I like that it gives the use some indication of what they did wrong when they pushed the button.

Code:
Private Sub CreateRO_Click()
 If DCount("*", "QueryCustomerSearch") = 0 Then
    MsgBox "No Customer Selected"
            Exit Sub
    End If

DoCmd.OpenForm "NewRepairOrder", , , , acFormAdd
Forms!NewRepairOrder.CustID = Me.[CustomerSearchSubForm].[Form]![CustID]
DoCmd.Close acForm, "CustomerSearch"

End Sub

Thanks again for pointing me in the right direction and correcting my poor control reference. I can't thank everyone on the forums here enough. The wealth of information I find and the assistance that is offered is AMAZING!!

Thanks again,
Dustin
 

Users who are viewing this thread

Top Bottom