Error 2001

L

Lavatress

I have a button on my form called Add Record and behind the button I have
some VBA. I also have code in my Form_BeforeUpdate which does not allow users
to proceed to the next record until certain fields have been satisfied.
However, I am beta testing the form with data, and it is giving me an Error
2001. It says I have cancel the previous operation. What am I doing wrong?
Also, in my Form_Before Update why is it not honoring my last condition(it is
hightlighted with ****). When I click yes I would like to add a vehicle
License # it proceeds to the next record w/o allowing me to add the vehicle
license #. Can you help me?

'Add a New Record
Private Sub AddRecord_Click()
If Me.Dirty Then
Me.Dirty = False
End If
If Not Me.NewRecord Then
RunCommand acCmdRecordsGoToNew
End If
End Sub
--------------------------------------------------------------------------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ingBigNum As Long
Dim mydate As Date
Dim edit As Integer

mydate = DateAdd("d", 0, Date)
response = MsgBox("Is the File ID correct " & Me.File_ID & " ?", vbYesNo,
"Verification Requested!")
If response = vbNo Then
DoCmd.CancelEvent
DoCmd.GoToControl "File ID"
Answer = MsgBox("Please correct the File ID!", vbOKOnly, "Recheck")


ElseIf Me.Filing_Date <= Me.Issue_Date Then
DoCmd.CancelEvent
DoCmd.GoToControl "Issue Date"
retvalue = MsgBox("Issue Date needs to be less than the Filing Date.",
vbCritical, "Required")


ElseIf IsNull(Me.Division) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Division"
retvalue = MsgBox("A Division must be entered.", vbCritical, "Required")

ElseIf IsNull(Me.File_ID) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "File ID"
retvalue = MsgBox("A File ID must be entered.", vbInformation, "Information")


ElseIf Not (Left(Me.File_ID, 2) = "BR" Or Left(Me.File_ID, 2) = "PK") Then
DoCmd.CancelEvent
DoCmd.GoToControl "File ID"
retvalue = MsgBox("The File ID must start with BR/PK.", vbCritical,
"Required")


ElseIf IsNull(Me.Filing_Date) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Filing Date"
retvalue = MsgBox("A Filing Date must be entered(Ex. 02/06/06).",
vbCritical, "Required")


ElseIf IsNull(Me.Issue_Date) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Issue Date"
retvalue = MsgBox("An Issue Date needs to be entered(Ex. 02/06/2006).",
vbInformation, "Information")


ElseIf (Me.Charge1 = "11:131A(1)" Or Me.Charge1 = "11:131A(2)" Or Me.Charge1
= "11:131A(3)" Or Me.Charge1 = "11:131A(4)" Or Me.Charge1 = "11:131A(5)" Or
Me.Charge1 = "11:131B" Or Me.Charge1 = "11:131C(1)" Or Me.Charge1 =
"11:131C(2)" Or Me.Charge1 = "11:131C(3)") And IsNull(Me.Speed1) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Speed1"
returnvalue = MsgBox("A speed must be entered.", vbCritical, "Required")


ElseIf (Me.Charge2 = "11:131A(1)" Or Me.Charge2 = "11:131A(2)" Or Me.Charge2
= "11:131A(3)" Or Me.Charge2 = "11:131A(4)" Or Me.Charge2 = "11:131A(5)" Or
Me.Charge2 = "11:131B" Or Me.Charge2 = "11:131C(1)" Or Me.Charge2 =
"11:131C(2)" Or Me.Charge2 = "11:131C(3)") And IsNull(Me.Speed2) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Speed2"
returnvalue = MsgBox("A speed must be entered.", vbCritical, "Required")


ElseIf (Me.Charge3 = "11:131A(1)" Or Me.Charge3 = "11:131A(2)" Or Me.Charge3
= "11:131A(3)" Or Me.Charge3 = "11:131A(4)" Or Me.Charge3 = "11:131A(5)" Or
Me.Charge3 = "11:131B" Or Me.Charge3 = "11:131C(1)" Or Me.Charge3 =
"11:131C(2)" Or Me.Charge3 = "11:131C(3)") And IsNull(Me.Speed3) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Speed3"
returnvalue = MsgBox("A speed must be entered.", vbCritical, "Required")

ElseIf (Me.Charge4 = "11:131A(1)" Or Me.Charge4 = "11:131A(2)" Or Me.Charge4
= "11:131A(3)" Or Me.Charge4 = "11:131A(4)" Or Me.Charge4 = "11:131A(5)" Or
Me.Charge4 = "11:131B" Or Me.Charge4 = "11:131C(1)" Or Me.Charge4 =
"11:131C(2)" Or Me.Charge4 = "11:131C(3)") And IsNull(Me.Speed4) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Speed4"
returnvalue = MsgBox("A speed must be entered", vbCritical, "Required")

ElseIf (Me.Charge5 = "11:131A(1)" Or Me.Charge5 = "11:131A(2)" Or Me.Charge5
= "11:131A(3)" Or Me.Charge5 = "11:131A(4)" Or Me.Charge5 = "11:131A(5)" Or
Me.Charge5 = "11:131B" Or Me.Charge5 = "11:131C(1)" Or Me.Charge5 =
"11:131C(2)" Or Me.Charge5 = "11:131C(3)") And IsNull(Me.Speed5) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Speed5"
returnvalue = MsgBox("A speed must be entered.", vbCritical, "Required")



ElseIf ((Me.Division = "TR") And IsNull(Me.Last_Name = True) And
IsNull(Me.First_Name = True)) Then
DoCmd.CancelEvent
returnvalue = MsgBox(" A First and Last Name must be entered for the
defendant.", vbCritical, "Required")

ElseIf (Me.Division = "TR") And IsNull(Me.Zip) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Zip"
retvalue = MsgBox("A Zip Code must be entered.", vbCritical, "Required")


ElseIf (IsNull(Me.DL_Number) = False And IsNull(Me.DL_State) = True) Then
DoCmd.CancelEvent
DoCmd.GoToControl "DL State"
retvalue = MsgBox("The Driver's License State must be entered.", vbCritical,
"Required")

ElseIf IsNull(Me.Airport) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Airport"
retvalue = MsgBox("You must enter Y/N for Airport.", vbCritical, "Required")

ElseIf Me.Court_Date < mydate Then
DoCmd.CancelEvent
DoCmd.GoToControl "Court Date"
returnvalue = MsgBox("The Court Date > = Current Date.", vbCritical,
"Required")

ElseIf Me.New_Court_Date < mydate Then
DoCmd.CancelEvent
DoCmd.GoToControl "New Court Date"
returnvalue = MsgBox("The New Court Date > = Current Date.", vbCritical,
"Required")

ElseIf IsNull(Me.Court_Date) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Court Date"
returnvalue = MsgBox("You must entered a Court Date .", vbCritical,
"Required")

***ElseIf (IsNull(Me.Vehicle_Lic_No = True) And IsNull(Me.VIN = True)) Then
***value = MsgBox("Would you like to add a Vin # or Vehicle Lic #? ",
***vbYesNo, "Warning")
***If value = vbNo Then
***MsgBox "Your request has been denied. No action was taken", , "Request
Canceled"
End If


End Sub
 
D

Dirk Goldgar

Lavatress said:
I have a button on my form called Add Record and behind the button I
have some VBA. I also have code in my Form_BeforeUpdate which does
not allow users to proceed to the next record until certain fields
have been satisfied. However, I am beta testing the form with data,
and it is giving me an Error 2001. It says I have cancel the
previous operation. What am I doing wrong? Also, in my Form_Before
Update why is it not honoring my last condition(it is hightlighted
with ****). When I click yes I would like to add a vehicle License #
it proceeds to the next record w/o allowing me to add the vehicle
license #. Can you help me?

'Add a New Record
Private Sub AddRecord_Click()
If Me.Dirty Then
Me.Dirty = False
End If
If Not Me.NewRecord Then
RunCommand acCmdRecordsGoToNew
End If
End Sub
---------------------------------------------------------------------- ----------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ingBigNum As Long
Dim mydate As Date
Dim edit As Integer

mydate = DateAdd("d", 0, Date)
response = MsgBox("Is the File ID correct " & Me.File_ID & " ?",
vbYesNo, "Verification Requested!")
If response = vbNo Then
DoCmd.CancelEvent
DoCmd.GoToControl "File ID"
Answer = MsgBox("Please correct the File ID!", vbOKOnly, "Recheck")


ElseIf Me.Filing_Date <= Me.Issue_Date Then
DoCmd.CancelEvent
DoCmd.GoToControl "Issue Date"
retvalue = MsgBox("Issue Date needs to be less than the Filing Date.",
vbCritical, "Required")


ElseIf IsNull(Me.Division) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Division"
retvalue = MsgBox("A Division must be entered.", vbCritical,
"Required")

ElseIf IsNull(Me.File_ID) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "File ID"
retvalue = MsgBox("A File ID must be entered.", vbInformation,
"Information")


ElseIf Not (Left(Me.File_ID, 2) = "BR" Or Left(Me.File_ID, 2) = "PK")
Then DoCmd.CancelEvent
DoCmd.GoToControl "File ID"
retvalue = MsgBox("The File ID must start with BR/PK.", vbCritical,
"Required")


ElseIf IsNull(Me.Filing_Date) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Filing Date"
retvalue = MsgBox("A Filing Date must be entered(Ex. 02/06/06).",
vbCritical, "Required")


ElseIf IsNull(Me.Issue_Date) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Issue Date"
retvalue = MsgBox("An Issue Date needs to be entered(Ex.
02/06/2006).", vbInformation, "Information")


ElseIf (Me.Charge1 = "11:131A(1)" Or Me.Charge1 = "11:131A(2)" Or
Me.Charge1 = "11:131A(3)" Or Me.Charge1 = "11:131A(4)" Or Me.Charge1
= "11:131A(5)" Or Me.Charge1 = "11:131B" Or Me.Charge1 = "11:131C(1)"
Or Me.Charge1 = "11:131C(2)" Or Me.Charge1 = "11:131C(3)") And
IsNull(Me.Speed1) = True Then DoCmd.CancelEvent
DoCmd.GoToControl "Speed1"
returnvalue = MsgBox("A speed must be entered.", vbCritical,
"Required")


ElseIf (Me.Charge2 = "11:131A(1)" Or Me.Charge2 = "11:131A(2)" Or
Me.Charge2 = "11:131A(3)" Or Me.Charge2 = "11:131A(4)" Or Me.Charge2
= "11:131A(5)" Or Me.Charge2 = "11:131B" Or Me.Charge2 = "11:131C(1)"
Or Me.Charge2 = "11:131C(2)" Or Me.Charge2 = "11:131C(3)") And
IsNull(Me.Speed2) = True Then DoCmd.CancelEvent
DoCmd.GoToControl "Speed2"
returnvalue = MsgBox("A speed must be entered.", vbCritical,
"Required")


ElseIf (Me.Charge3 = "11:131A(1)" Or Me.Charge3 = "11:131A(2)" Or
Me.Charge3 = "11:131A(3)" Or Me.Charge3 = "11:131A(4)" Or Me.Charge3
= "11:131A(5)" Or Me.Charge3 = "11:131B" Or Me.Charge3 = "11:131C(1)"
Or Me.Charge3 = "11:131C(2)" Or Me.Charge3 = "11:131C(3)") And
IsNull(Me.Speed3) = True Then DoCmd.CancelEvent
DoCmd.GoToControl "Speed3"
returnvalue = MsgBox("A speed must be entered.", vbCritical,
"Required")

ElseIf (Me.Charge4 = "11:131A(1)" Or Me.Charge4 = "11:131A(2)" Or
Me.Charge4 = "11:131A(3)" Or Me.Charge4 = "11:131A(4)" Or Me.Charge4
= "11:131A(5)" Or Me.Charge4 = "11:131B" Or Me.Charge4 = "11:131C(1)"
Or Me.Charge4 = "11:131C(2)" Or Me.Charge4 = "11:131C(3)") And
IsNull(Me.Speed4) = True Then DoCmd.CancelEvent
DoCmd.GoToControl "Speed4"
returnvalue = MsgBox("A speed must be entered", vbCritical,
"Required")

ElseIf (Me.Charge5 = "11:131A(1)" Or Me.Charge5 = "11:131A(2)" Or
Me.Charge5 = "11:131A(3)" Or Me.Charge5 = "11:131A(4)" Or Me.Charge5
= "11:131A(5)" Or Me.Charge5 = "11:131B" Or Me.Charge5 = "11:131C(1)"
Or Me.Charge5 = "11:131C(2)" Or Me.Charge5 = "11:131C(3)") And
IsNull(Me.Speed5) = True Then DoCmd.CancelEvent
DoCmd.GoToControl "Speed5"
returnvalue = MsgBox("A speed must be entered.", vbCritical,
"Required")



ElseIf ((Me.Division = "TR") And IsNull(Me.Last_Name = True) And
IsNull(Me.First_Name = True)) Then
DoCmd.CancelEvent
returnvalue = MsgBox(" A First and Last Name must be entered for the
defendant.", vbCritical, "Required")

ElseIf (Me.Division = "TR") And IsNull(Me.Zip) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Zip"
retvalue = MsgBox("A Zip Code must be entered.", vbCritical,
"Required")


ElseIf (IsNull(Me.DL_Number) = False And IsNull(Me.DL_State) = True)
Then DoCmd.CancelEvent
DoCmd.GoToControl "DL State"
retvalue = MsgBox("The Driver's License State must be entered.",
vbCritical, "Required")

ElseIf IsNull(Me.Airport) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Airport"
retvalue = MsgBox("You must enter Y/N for Airport.", vbCritical,
"Required")

ElseIf Me.Court_Date < mydate Then
DoCmd.CancelEvent
DoCmd.GoToControl "Court Date"
returnvalue = MsgBox("The Court Date > = Current Date.", vbCritical,
"Required")

ElseIf Me.New_Court_Date < mydate Then
DoCmd.CancelEvent
DoCmd.GoToControl "New Court Date"
returnvalue = MsgBox("The New Court Date > = Current Date.",
vbCritical, "Required")

ElseIf IsNull(Me.Court_Date) = True Then
DoCmd.CancelEvent
DoCmd.GoToControl "Court Date"
returnvalue = MsgBox("You must entered a Court Date .", vbCritical,
"Required")

***ElseIf (IsNull(Me.Vehicle_Lic_No = True) And IsNull(Me.VIN =
True)) Then
***value = MsgBox("Would you like to add a Vin # or Vehicle Lic #? ",
***vbYesNo, "Warning")
***If value = vbNo Then
***MsgBox "Your request has been denied. No action was taken", ,
"Request Canceled"
End If


End Sub

The last condition isn't working right because you have the parentheses
in the wrong place. Try this:

ElseIf (IsNull(Me.Vehicle_Lic_No) = True) _
And (IsNull(Me.VIN) = True) _
Then

Are you getting the 2001 error whether the update is canceled or not?
If you're only getting it when your BeforeUpdate code cancels the
update, it makes perfect sense: you try to force a save with "Me.Dirty
= False", but the operation was canceled (by the BeforeUpdate code) so
you get an error on the statement.

To cope with that, you should trap the error:

'----- start of code -----
Private Sub AddRecord_Click()

On Error GoTo Err_Handler

If Me.Dirty Then
Me.Dirty = False
End If

If Not Me.NewRecord Then
RunCommand acCmdRecordsGoToNew
End If

Exit_Point:
Exit Sub

Err_Handler:
If Err.Number <> 2001 Then
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
End If
Resume Exit_Point

End Sub
'----- end of code -----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VBA error using Access 5
Error 2001 0
Error 2001 0
Error 2001 0
Error 2001 message 1
Long If Statement 3
type mismatch error in macro in excel 0
Password Protect form 1

Top