Error 2001 message

G

Guest

I have a form that has a button on it called Add New Record. It allows the
users to add a new record. However, before they can add a new record I have
some filters in the form before the update takes place. These certain fields
have to be satified before adding a new record. When one of my fields were
not satisfied, it gave me an error 2001 (you canceled the previous
operation). What am I doing wrong? Also, in my Private Sub_Form
BeforeUpdate, it is not honoring my last filter(I added *'s in front of it).
Before, I added the New Record button,when I didn't enter an issue date, it
still was proceeding to the next screen. Is something wrong with my
conditional statements? Thanks in advance for you comments.

'Add New Record
Private Sub Add_New_Record_Click()
If Me.Dirty Then
Me.Dirty = False
End If
If Not Me.NewRecord Then
RunCommand acCmdRecordsGoToNew
End If
End Su
-----------------------------------------------------------------------------------------------
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")
End If
End Sub
 
A

Allen Browne

Instead of:
DoCmd.CancelEvent
try:
Cancel = True
That might give you more sensible error messages.

You will need to add error handling to Add_New_Record_Click. Access will
inform you that the attempt to set the form's Dirty property to False did
not succeed when Form_BeforeUpdate is cancelled. But I would have expected
error 2101 here. If error handling is new, see:
Error Handling in VBA
at:
http://allenbrowne.com/ser-23a.html

The failure of the last condtion might be to do with the fact that no
further ElseIf is evaluated once one is true.

BTW, you might prefer to test several things at once, and notify the user of
all the problems instead of one at a time. This kind of thing might give you
some ideas:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Dim strField As String

'**************************
'Handle required fields here
'**************************
If Me.Filing_Date <= Me.Issue_Date Then
Cancel = True
strMsg = strMsg & "Issue Date must be before Filing Date." & vbCrLf
strField = "Issue Date"
End If

If IsNull(Me.Division) Then
Cancel = True
strMsg = strMsg & "Division required." & vbCrLf
strField = "Division"
End If

With Me.[File ID]
If Not (.Value Like "BR*" OR .Value Like "PK*") Then
Cancel = True
strMsg = strMsg & "File aID must start with BR or PK." & vbCrLf
strField = .Name
End If
End With

'etc for other controls.

If Cancel Then
MsgBox strMsg, vbExclamation, "Invalid"
Else
'**************************
'Enter your warnings/questions here.
'**************************
strMsg = strMsg & "Is the File ID correct?" & vbCrLf
'Other questions as needed.
strMsg = strMsg & vbCrLf & "Save the record?"
If MsgBox(strmsg, vbYesNo+vbQuestion+vbDefaultButton2, "Confirm") <>
vbYes Then
Cancel = True
'Me.Undo
End If
End If
End Sub
 

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

Error 2001 1
Error 2001 1
VBA error using Access 5
Validation Rule and Now() function 6
Password Protect form 1
Option Explicit 2
Compile Error Help 6
OpenReport Where Condition with date range 6

Top