Example of the Extra Resume

Here's how the extra Resume works. Let's say your code is supposed to open a report, but there's a problem: the report name you specified doesn't exist. Your code might look like this:

Private Sub cmdPreview_Click() On Error GoTo Error_Handler

If Me!lstReport.Column(3) & "" <> "" Then

DoCmd.OpenReport ReportName:=Me!lstReport.Column(3), View:=acViewPreview End If

'Update the Last Run Date of the report DoCmd.SetWarnings False DoCmd.RunSQL "UPDATE tsysReport " _ & "SET tsysReport.DtLastRan = Date() " _ & "WHERE tsysReport.RptKey = " & Me.lstReport DoCmd.SetWarnings True

Exit_Procedure:

On Error Resume Next DoCmd.SetWarnings True Exit Sub

Error_Handler:

MsgBox "An error has occurred in this application. " _ & "Please contact your technical support person and " _ & "tell them this information:" _

& vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " & Err.Description, _

Buttons:=vbCritical, title:="My Application" Resume Exit_Procedure Resume End Sub

When you run your code, an error message appears, as shown in Figure 9-3.

Figure 9-3

Instead of clicking OK like your user would do, press Ctrl-Break on your keyboard. You'll see a Visual Basic dialog box, as shown in Figure 9-4.

This extra Resume technique won't work in an Access runtime application, because in runtime mode no design modes are allowed, including VBA code. It also won't work in an Access MDE, because all VBA source code is removed from an MDE application.

Figure 9-4

Now click the Debug button. Your code will display in the Code window, as shown in Figure 9-5.

The Resume Exit_Procedure statement will be highlighted in yellow. This is the statement that will execute next if you continue normally. But instead of letting it run, now you need to take control. Using your mouse, drag the yellow arrow down one line to the extra Resume line. By doing this, you are indicating that you want the Resume line to run next. Or, you can click or arrow down to the Resume line, and then use Debug..Set Next statement (Ctrl-F9 on your keyboard). As usual in Access, there are several ways to do the same thing. Now, the yellow arrow will be pointed at the Resume statement, as shown in Figure 9-6.

Microsoft Visual Basic - Chamber Application [break] - [Form frmMenu Reports (Code)]

Eile Edit View Insert Debuq Run Tools Add Ins Window Help

-Ifllx

HI M ■ S tf-ÜP. M ► ■ M 9 ff W 9- 13 in is, coi 1

Piujtfui - Cham bei il D

El FormJrr_*| 11 Form Ji El FormJ Hl FormJ f=s1 FormJ

El Formjsi El Formjsi El Formjsi El Formjsi m Formjsi— El Formjsi [^äl Fnrm Jsi — I

Properties JE3

Alphabetic | Categorized ]

Alphabetic | Categorized ]

Figure 9-5

cmdPreuiew

"3

E:<it_Proceaure:

On Errcr Resume Next DoCmd.¡EetWarnings True DoCmd.Hourglass 7alse Exit Sub

Error_Handler:

If Err.Number = 2501 Then

Resume Exit_Procedure Else

MayBux "An c:lul has luu ul l ed i:x Lhia apf. 1 iutiliuxx. " & "Please contact your technical support person and tell them & vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " & Err. Buttons:=vbCritical, title:="My Application" Resume Exit Procedure Resume End If

End Sub this Descr

Private Sut Forn_Close() On Error GcTo Err_Form_Close

If mstrCallmgForm <> "" Then

Forms(mstrCallinaForm).Visible = True End If

tfsiJ_1

Figure 9-5

i Microsoft Visual Basic - Chamber Application [break] - [Forrn frmMenu Reports (Code)]

Ei'e Edit View Insert Debuq Run Tools Add Ins Window Help a at - a & p M

Piujecl - Chambei

El Form Jrr^ El Form Ji El FormJ El FormJ ESI Form Ji El Form Jsil El Form Jsi El Form Jsi El Form Jsi •Fsl Formjsi— El Formjsi EH Fnrm Jsi — I

Properties EJ

Alphabetic Categorized |

cmdPreuiew i I

E:<it_Proceaure:

On Errcr Resume Next DoCmd.£etWarnings True DoCmd.Hourglass 7alse Exit SUd

Error_Handler:

If Err.Number = 2501 Then

Resume Exit_Procedure Else

MayBux "Axx eixux has lululiclI i:x Lhia ttpi-1 iuaUiuxx. " & "Please contact your technical support person and tell them & vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " & Err. Buttons:=vbCritical, title:="Hy Application"

| Resume Exit_?rocedure

Resume End If

End Sub this Descr

Private Sub Forn_Close() On Error GcTo Err_Form_Close

If mstrCallmgForm <> "" Then

Forms(mstrCallinaForm).Visible = True End If

tfsiJ_1

Figure 9-6

Now, you want the Resume statement to run, which will reposition to retry the statement that caused error. Press F8 to run the next line of code (your Resume) and stop. Or, you can choose Debug..Step Into from the menu.

The exact line that caused the error will now be highlighted in yellow, as shown in Figure 9-7. That was easy, wasn't it?

I Microsoft Visual Basic Chamber Application [break] - (l:orm frmMeiiu Reports (Code)]

Fiie Edit View Insert Debug Run Tools Add-lns Window Help

JflJzJ

Project - Chamber 0

FH Form_Prr •*• ] Fil Form_frr FH Form_frr FH Form_frr Fil Form_frr FH Form_fsi FH Form_fsi FH Form_fsi FH Form_fsi FH Form_fsi— FH Form_fsi FH Form_fsi-T-l j" ±r

Properties |j

Alphabetir Categorized

cmdPreuiew

DoCmd.Close

Exit_cmdClose_Click:

HsgBox Err.Number £ "f " £ Err.Description Resume Exit_cmdClose_Click End Sub

F1 r i va t e Sub cmdF1 r evi ew_C 1 i ck () On Error GoTo Error_Handler

If He ! IstReport. Column (3) £ ,r" <> ,r" Then | DoCmd.OpenReport ReportName : =Me ! IstReport. Column (3 ) , View

End If

1 Update the Last Run Date o± the report DoCmd.Hourglass True DoCmd.SetUarnings False

DoCmd.RunSQL "UPDATE tsysReport SET tsysReport.DtLastRan = Dat

"WHERE tsysReport.RptKey = " £ He.IstReport DoCmd.SetUarnings True DoCmd.Hourglass False

Ex it_Frocedure:

On Error Resume Next DoCmd.SetUarnings True las-ii_i

Figure 9-7

Now, admittedly, this is a very simple example. You probably could have figured out which line caused the error just by looking at the error description. However, when your procedures contain pages of code, often with coding loops, complex logic, and similar statements, this extra Resume technique comes in very handy. It can save you many hours of time while you are debugging your VBA code.

The extra Resume doesn't cause any harm in your code, so you can leave it in every procedure even when you deliver your application. Also, if a technically savvy client encounters an unexpected error, you can walk the client through this process to help determine what caused the problem in the client's environment. As you know, what works on your PC doesn't always work when your user is running it.

0 0

Post a comment