Error Trapping Problem

olorin

Registered User.
Local time
Today, 08:06
Joined
Jun 9, 2006
Messages
81
I am by no means an expert on VBA, and have only got as far as I have with VBA by viewing the excellent tips and examples in this forum.
That said, I am stuck on Error Trapping.

I have a form with it's datasource tblInvoices.
It automatically fills in a couple of required fields when opened with data from a related from which is open in the background.

In the tblInvoices, the field "InvoiceNumber" is indexed with NoDuplicates allowed, (it is NOT the primary key).

I have tried to include an error trap on the form, for when someone enters an Invoice number that has already been used.

This is what I have;

Code:
Private Sub Notes_Exit(Cancel As Integer)
On Error GoTo Err_SameNumber
     Dim iResponse As Integer
     iResponse = MsgBox("Do you wish to Print an Invoice", vbYesNo, "SELECT AN OPTION")
     If iResponse = vbYes Then
        DoCmd.RunCommand acCmdSaveRecord
        Forms!frmSelectInvoice!Invoiced.Value = True
        Forms!frmSelectInvoice.Requery
        DoCmd.Save acForm, "frmInvoice"
        DoCmd.OpenReport "rptInvoice", acNormal, "", "[pkInvoiceID]=[Forms]![frmInvoice]![pkInvoiceID]"
        Me.btnClose.SetFocus
        Exit Sub
 End If
     If iResponse = vbNo Then
        MsgBox "You can Print it out later from the Invoices Main Menu", , "You Selected NO"
     End If
        DoCmd.RunCommand acCmdSaveRecord
        Forms!frmSelectInvoice!Invoiced.Value = True
        Forms!frmSelectInvoice.Requery
        Me.btnClose.SetFocus
Exit_SameNumber:
    DoCmd.RunCommand acCmdUndo
        DoCmd.GoToRecord , , acNewRec
        Me.fkBookingID.Value = Forms!frmSelectInvoice!pkBookingID.Value
        Me.fkCustomerID.Value = Forms!frmSelectInvoice!fkCustomerID.Value
    Me.InvoiceNumber.SetFocus
Exit Sub
Err_SameNumber:
        MsgBox "Invoice Number has already been used", vbInformation, "Please use a New Invoice Number"
        Resume Exit_SameNumber
End Sub

When the user enters an Invoice Number that has already been used, it tells them after they exit the "Notes" field.
It also clears the record and sets the focus back to the Invoice Number field.
After entering in new information it keep coming back to the "Err_SameNumber" routine and will not let me out.
I have to use the Windows Task Manager to exit the program.
If anyone can throw any light on this and point out my error I would be grateful
Thank you
 
I recommend you building a statement that checks if the Same Number has been entered instead of using the error handler like you are.
 
Thanx so much for the rapid response,
I have just been "tinkering" with the code and I managed to get this to work.
I think it may be stable enough to keep it.

Code:
Private Sub Notes_Exit(Cancel As Integer)
On Error GoTo Err_SameNumber
 Dim iResponse As Integer
 iResponse = MsgBox("Do you wish to Print an Invoice", vbYesNo, "SELECT AN OPTION")
 If iResponse = vbYes Then
    DoCmd.RunCommand acCmdSaveRecord
    Forms!frmSelectInvoice!Invoiced.Value = True
    Forms!frmSelectInvoice.Requery
    DoCmd.Save acForm, "frmInvoice"
    DoCmd.OpenReport "rptInvoice", acNormal, "", "[pkInvoiceID]=[Forms]![frmInvoice]![pkInvoiceID]"
    Me.btnClose.SetFocus
    Exit Sub
 End If
 If iResponse = vbNo Then
    MsgBox "You can Print it out later from the Invoices Main Menu", , "You Selected NO"
    DoCmd.RunCommand acCmdSaveRecord
    Me.btnClose.SetFocus
    Exit Sub
End If
Exit_SameNumber:
    Me.InvoiceNumber.SetFocus
Exit Sub
Err_SameNumber:
    MsgBox "Invoice Number has already been used", vbInformation, "Please use a New Invoice Number"
    Resume Exit_SameNumber
End Sub
 

Users who are viewing this thread

Back
Top Bottom