Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't trap error 2015 on Add New Record Button

Status
Not open for further replies.

aoloughlen

Technical User
Aug 17, 2012
1
IE
I’ve a form with a two buttons, Close & Add New Record. When the user clicks Close, the Beforeupdate event kicks in and validates the form, if there are empty fields the user gets a alert and has the option to Exit and not save record or Cancel and return to form. This works. The problem is that when the user hits custom button for Add New Record (Command114 button) they get “Runtime error 2105 - Can't go to selected record" and I can’t trap this like I did for Error 2105 on the Close button. I then tried to use an Error Handler but my code is not correct. Here is the code I have. Can anyone steer my in the right direction? I want to replace the error with my own message telling the user to update the required fields.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.Combo52) Then
Me.Combo52.BorderColor = vbRed
Cancel = True
End If


If IsNull(Me.Text62) Then
Me.Text62.BorderColor = vbRed
Cancel = True

Exit Sub
End If

If IsNull(Me.Combo70) Then
Me.Combo70.BorderColor = vbRed
Cancel = True

Exit Sub
End If

If IsNull(Me.Combo74) Then
Me.Combo74.BorderColor = vbRed
Cancel = True

Exit Sub
End If


If IsNull(Me.Combo72) Then
Me.Combo72.BorderColor = vbRed
Cancel = True

Exit Sub

Else
MsgBox ("Record Saved")
End If


End Sub

-------------------------------------------------------------------------------------------------------

Private Sub Form_Error(DataErr As Integer, Response As Integer)


If DataErr = 2169 Then
If MsgBox("Your record has not been saved as all required fields have not been completed. Click OK to exit without saving or CANCEL to return to form", vbOKCancel) = vbOK Then
Cancel = False
Response = acDataErrContinue ' telling Access not to show default message
Else
SendKeys "%N", False
End If


End If

End Sub


-------------------------------------------------------------------------------------------------------

Private Sub Command114_Click()

On Error GoTo MyErr

DoCmd.GoToRecord , , acNewRec

MyExit:
Exit Sub

MyErr:
Select Case Err.Number

Case 2105
If DataErr = 2169 Then
Msgbox "A required field is missing"

Case else
DoCmd.GoToRecord , , acNewRec

End Select

Resume MyExit

End Sub

 

Where do you declare and assign any value to DataErr?

Code:
Select Case Err.Number
  Case 2105
    If [red]DataErr[/red] = 2169 Then
      Msgbox "A required field is missing"
  Case else
    DoCmd.GoToRecord , , acNewRec
End Select

"my code is not correct" - what does it do when you step thru your code?

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top