stop at the error

peterbowles

Registered User.
Local time
Today, 16:27
Joined
Oct 11, 2002
Messages
163
I use the following code in the on_Click event of a button

Private Sub cmdsave_Click()

If IsNull(Me.BillRefNumber) = True Then
MsgBox "Please Enter a customer number"
Me.BillRefNumber.SetFocus
Else

If IsNull(Me.AccomID) = True Then
MsgBox "Please select customer accommodation type"
Me.AccomID.SetFocus
Me.AccomID.Dropdown
Else



On Error GoTo errno- On this part I want to stop going through the rest of the code until there is no error.



MsgBox "Bill Ref No (" & Me.BillRefNumber & ") has been added/amended"
Me.Label55.Visible = True
Me.AllowEdits = False
Me.AllowAdditions = False


Me.Requery
Me.Refresh
DoCmd.ShowAllRecords
Me.Label55.Visible = True

End If
End If

Exit Sub

errno:
MsgBox "This record could not be added as there are duplicate Bill Ref No."


End Sub
 
Terminate At Error ..

Hi Peter,


You Wrote:>

On Error GoTo errno- On this part I want to stop going through the rest of the code until there is no error.

Try following:

On Error Resume Next

do something....

On Error Goto 0 'This should be the last line (Before End Sub)

Regards,

Ashfaque Hussain
 
In your error handling simply add the line:

Exit Sub

after your msgbox.

On a different tact, the line:

If IsNull(Me.BillRefNumber) = True Then

you can simply use

If IsNull(Me.BillRefNumber) Then

HTH

Dave
 
Private Sub cmdsave_Click()

If IsNull(Me.BillRefNumber) Then
MsgBox "Please Enter a customer number"
Me.BillRefNumber.SetFocus
Exit Sub

ElseIf IsNull(Me.AccomID) Then
MsgBox "Please select customer accommodation type"
Me.AccomID.SetFocus
Me.AccomID.Dropdown
Exit Sub

Else

MsgBox "Bill Ref No (" & Me.BillRefNumber & ") has been added/amended"
Me.Label55.Visible = True
Me.AllowEdits = False
Me.AllowAdditions = False

Me.Requery
Me.Refresh
DoCmd.ShowAllRecords
Me.Label55.Visible = True

End If

Exit Sub
 
it still goes to the next line when there is an error

on error goto errno
MsgBox "Bill Ref No (" & Me.BillRefNumber & ") has been added/amended"

errno:
msgbox "duplicate values"
 
Is the code you placed here as you have written it.

If so, place the error handler at the top of the sub

Private Sub cmdsave_Click()
On Error GoTo ErrNo

If IsNull(Me.BillRefNumber) Then
MsgBox "Please Enter a customer number"
Me.BillRefNumber.SetFocus
Exit Sub

ElseIf IsNull(Me.AccomID) Then
MsgBox "Please select customer accommodation type"
Me.AccomID.SetFocus
Me.AccomID.Dropdown
Exit Sub

Else

MsgBox "Bill Ref No (" & Me.BillRefNumber & ") has been added/amended"
Me.Label55.Visible = True
Me.AllowEdits = False
Me.AllowAdditions = False

Me.Requery
Me.Refresh
DoCmd.ShowAllRecords
Me.Label55.Visible = True

End If

ErrNo:
MsgBox "This record could not be added as there are duplicate Bill Ref No."
Exit Sub

End Sub

PS.. I am puzzled with
Me.Requery
Me.Refresh
DoCmd.ShowAllRecords

Try:
Docmd.RunCommand accmdSaveRecord

Dave
 
An alternative.
Code:
Private Sub cmdsave_Click()

  If IsNull(Me.BillRefNumber) = True Then
    MsgBox "Please Enter a customer number"
    Me.BillRefNumber.SetFocus
    [b]Exit Sub
  End If[/b]

  If IsNull(Me.AccomID) = True Then
    MsgBox "Please select customer accommodation type"
    Me.AccomID.SetFocus
    Me.AccomID.Dropdown
    [b]Exit Sub
  End If[/b]

  
  ' Any code that you want to run when both
  ' BillRefNumber and AccomID are not null.
  ...............
  ...............
  ...............

Exit Sub
 

Users who are viewing this thread

Back
Top Bottom