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!

Trying to Trap Error

Status
Not open for further replies.

cRODEEkrank

Technical User
Dec 5, 2001
41
0
0
US
Hi,

I'm building a database which tracks the # of minutes a person spent working on a specified task. I created an Access form which will accept their input. The form is unbound, but I created a procedure to bind the controls when the user wants to save their information to the database.

My problem is, I'm trying to trap error #3022 which pops up if the user tries to enter a duplicate record. This error comes up when the "rs.update" command is executed. I've tried to trap this error, but for some reason it just won't trap it, and the standard Access error message displays.

Here's my code:
Private Sub BindControls()
Dim rs As DAO.Recordset
Dim strSQL As String
Dim intTaskID As Integer
Dim strEmpNum As String

On Error GoTo TrapErrors

strSQL = "SELECT TaskID FROM tblTaskLookup WHERE
TaskName='" & gstrTask & "';"
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset,
dbConsistent, dbPessimistic)
rs.AddNew
rs!TaskID = intTaskID
rs!EmployeeID = gstrEmployeeID
rs!Date = CDate(txtDate.Value)
rs!Comments = Nz(txtComments.Value, "")
rs!ProjectName = Nz(txtProjectName.Value, "")
rs!ContactName = Nz(txtContactName.Value, "")
rs!Minutes = CDbl(txtNumMinutes.Value)
rs!NumOfMaterials = CInt(Nz(txtMatGoodNums.Value, 0))
rs!SubTasks = Nz(cboSubtasks.Value, "")
rs!NumSets = CInt(Nz(txtNumSets.Value, 0))
rs.Update
'close, free recordset object:
rs.Close
Set rs = Nothing
Exit Sub

TrapErrors:
Select Case Err.Number
Case 3022
MsgBox "The system has indicated an entry for this date and task has already been entered into the database. Please go to the edit section to make any necessary changes to this task", vbOKOnly + vbExclamation, "Entry Already Exists for this Task"
End Select

End Sub


Help?
 
Try checking the recordsets error object rather than the standard VB error object. It strikes me as an error that is being produced by the recordset. Mark

The key to immortality is to make a big impression in this life!!
 
Thanks Mark. I'm not sure what recordsets error object you're referring to. I tried looking at all the objects and properties of the recordset and recordsets object, but couldn't find anything.
 
HI. See this example from the DAO help files which ship with VB. It's a little Verbose but you might be able to get something from it.

This example forces an error, traps it, and displays the Description, Number, Source, HelpContext, and HelpFile properties of the resulting Error object.

Sub DescriptionX()

Dim dbsTest As Database

On Error GoTo ErrorHandler

' Intentionally trigger an error.
Set dbsTest = OpenDatabase("NoDatabase")

Exit Sub

ErrorHandler:
Dim strError As String
Dim errLoop As Error

' Enumerate Errors collection and display properties of
' each Error object.
For Each errLoop In Errors
With errLoop
strError = _

"Error #" & .Number & vbCr
strError = strError & _
" " & .Description & vbCr
strError = strError & _
" (Source: " & .Source & ")" & vbCr
strError = strError & _
"Press F1 to see topic " & .HelpContext & vbCr
strError = strError & _
" in the file " & .HelpFile & "."
End With

MsgBox strError
Next

Resume Next

End Sub Mark

The key to immortality is to make a big impression in this life!!
 
Mark,

Nope, the error is still not trapping. I changed the "TrapError" code to :

TrapError:
msgbox "Error number: & err.number & " just occurred."


...and unfortunately the error still isn't trapping.
 
Make the first line of TrapError:
msgbox err.number & " " err.description
to see exactly what it is returning. Mark

The key to immortality is to make a big impression in this life!!
 
Well that's the whole problem. The code isn't going to my "TrapError:" code at all. It encounters an error, but instead of going to the "TrapError:" error trapping, it just displays the standard Access error message, which is what I'm trying to avoid.

I wonder if there's something in Access I need to turn on or something to get this to work? I've encountered this problem before, but now I would like to resolve it.
 
I may be missing the point here but looking at your code it doesn't seem correct. You seem to be referring to all those items on the recordset which are not part of the recordset as you only have one field on the recordset. You will never trap that error because it is probably failing on a prior error. Are you sure you are not getting "Item not present.... " ?
 
Oh, sorry...I abbreviated my code (it's quite a lengthy procedure) and just wanted to make sure the piece I had a question on was present. This is why it probably doesn't make much sense.

I actually figured out the problem. In the VB editor in Access, I went to the menu option "Tools -> options", clicked the "general" tab and noticed an "Error Trapping" section. The radio button selected was "Break on All Errors". I changed this option to be "Break in Class Module" and it worked! Since I just created this database a few weeks ago, "Break on All Errors" must be the default selection with a new database.

Thanks everyone for your help!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top