Catching two errors..

Klion

Registered User.
Local time
Yesterday, 22:22
Joined
Jul 12, 2004
Messages
16
Basically what will happen is the first error will occur, Case -2147217887, and the code will catch it fine. Then user will click ok and code will continue on and the same error is likely to occur a second time. However the second time instead of it going to error handling the debugger pops up. I tried clearing Err but that didnt seem to do anything :(

So basically what do I have to do to make this able to catch the second error after it's dealt with the first?


Code:
Sub Import(strDocName As String)
Dim appWord As Word.Application
Dim doc As Word.Document
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim qst As New ADODB.Recordset
Dim flg As Boolean

Dim blnQuitWord As Boolean

On Error GoTo ErrorHandling

Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)


cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\Documents and Settings\befusc\Desktop\Database\" & _
    "GradSurvey.mdb;"


rst.Open "tblStudent", cnn, _
    adOpenKeyset, adLockOptimistic
With rst
    .AddNew
'
    !StudentID = doc.FormFields("fldStudentNum").Result
    !FirstName = doc.FormFields("fldFirstName").Result
    !LastName = doc.FormFields("fldLastName").Result
    !Address = doc.FormFields("fldMailingAddy").Result & " " & doc.FormFields("fldPostalCode").Result
    !Telephone = doc.FormFields("fldAreaCode").Result & " " & doc.FormFields("fldTeleNum").Result
    !Email = doc.FormFields("fldEmail").Result
    !Degree = doc.FormFields("fldDegree").Result
    'This is done to get the Major. I had to use multiple fields to compensate for being
    'unable to use macros in MS word so I use this to narrow it down to one.
    If Not doc.FormFields("fldArts").Result = "       " Then
        !Major = doc.FormFields("fldArts").Result
    ElseIf Not doc.FormFields("fldSciences").Result = "       " Then
        !Major = doc.FormFields("fldSciences").Result
    ElseIf Not doc.FormFields("fldCommerce").Result = "       " Then
        !Major = doc.FormFields("fldCommerce").Result
    Else
        MsgBox "An Error Occured while Importing the Major. This probably means a Major was not selected."
    End If

    'Checks to see if they filled in more than one Major field
    If Not (doc.FormFields("fldArts").Result = "       " _
    And doc.FormFields("fldSciences").Result _
   = "       ") And Not (doc.FormFields("fldSciences").Result = "       " _
    And doc.FormFields("fldCommerce").Result = "       ") And Not _
   (doc.FormFields("fldCommerce").Result = "       " And _
    doc.FormFields("fldArts").Result = "       ") Then
    MsgBox "More than one Major field was filled out. Aborting Import"
    GoTo Cleanup
    End If
    .Update
    .Close
End With

ContImp:
qst.Open "tblSurveyData", cnn, _
    adOpenKeyset, adLockOptimistic
    flg = False
With qst
    .AddNew
    !StudentID = doc.FormFields("fldStudentNum").Result
    !Date = doc.FormFields("fldDate").Result
    !SecureEmployment = doc.FormFields("chkEmploy").Result
    !HowLongAfter = doc.FormFields("drpHowLong").Result
    !Employer = doc.FormFields("drpEmployer").Result
    !Location = doc.FormFields("drpLocation").Result
    !RatePay = doc.FormFields("drpPayRate").Result
    !PositionStatus = doc.FormFields("drpPosStatus").Result
    !ContinuingEd = doc.FormFields("chkContEd").Result
    !ContDegree = doc.FormFields("drpDegreeProg").Result
    !GradYear = doc.FormFields("fldGradYear").Result
    !CoopAssist = doc.FormFields("fldAssistChk").Result
    !SelfConfidence = doc.FormFields("chkConfidence").Result
    !ResumePresentation = doc.FormFields("chkResumePres").Result
    !HiredBy = doc.FormFields("chkFormerCoop").Result
    !ImprovedOral = doc.FormFields("chkImpOral").Result
    !TransferableSkills = doc.FormFields("chkRelevantSkills").Result
    !Other = doc.FormFields("chkOther").Result
    !Contacts = doc.FormFields("chkContact").Result
    !InterviewSkills = doc.FormFields("chkInterviewSkill").Result
    !PreferenceCoop = doc.FormFields("chkGivenPref").Result
    !ImprovedWritten = doc.FormFields("chkImpWritten").Result
    !ExplainOther = doc.FormFields("fldOtherExpl").Result
    !RelevanceofEmploy = doc.FormFields("drpRelev").Result
    !AlumniChapter = doc.FormFields("drpAlumni").Result
    !HireCoop = doc.FormFields("drpHireCoop").Result
    !Present = doc.FormFields("drpSpeak").Result
    !GeneralComments = doc.FormFields("fldGeneral").Result
    .Update
    .Close
End With
doc.Close

cnn.Close
MsgBox "Contract Imported!"

Cleanup:
If blnQuitWord Then appWord.Quit
Set qst = Nothing
Set rst = Nothing
Set cnn = Nothing
Set doc = Nothing
Set appWord = Nothing
Exit Sub

ErrorHandling:
Select Case Err
Case -2147022986, 429
    Set appWord = CreateObject("Word.Application")
    blnQuitWord = True
    Resume Next
Case 5121, 5174
    MsgBox "You must select a valid Word document. " _
        & "No data imported.", vbOKOnly, _
        "Document Not Found"
        GoTo Cleanup
'Case 5941
'    MsgBox "The document you selected does not " _
'        & "contain the required form fields. " _
'        & "No data imported.", vbOKOnly, _
'        "Fields Not Found"
Case -2147217887

    If flg = False Then
    MsgBox "Student information corresponding to that student ID already exists.", vbOKOnly
    GoTo ContImp
    Else
        MsgBox "Grad Survey Results have already been entered under that Student number.", vbOKOnly
    End If
    
Case Else
    MsgBox Err & ": " & Err.Description
End Select
GoTo Cleanup
End Sub
 
Some of your error-trap cases don't end in Resume. Which means you are still in your error handler when you exit the case statement, I think.

If that is the case, the debugger kicks in 'cause Access doesn't want a trap to occur in trap code, which in some cases it would think is still running.
 
Thanks a lot, that got it.
 

Users who are viewing this thread

Back
Top Bottom