PowerPoint 演示文稿

例 11.14 错误处理的基本方法
Sub OnErrorStatementDemo()
Dim Response ,Msg, Style, Title As String
Dim db As Database, rs As Recordset
On Error GoTo ErrorHandler
Set db = OpenDatabase("A:\Student.mdb") ' 打开数据库
Set rs = db.OpenRecordset("基本情况") ' 设置记录集
… …
Exit Sub ' 正常退出本程序
ErrorHandler: ' 错误处理块入口
Style = vbRetryCancel + vbCritical + vbDefaultButton2
Select Case Err.Number
Case 53 ' 出错号53为文件不存在
Msg = "文件不存在!"
Response = MsgBox(Msg, Style, "出错提示")
If Response = vbCancel Then Exit Sub
Case 71 ' 71号驱动器未准备好
Msg = " A盘未准备好"
Response = MsgBox(Msg, Style, "出错提示")
If Response = vbCancel Then Exit Sub
Case Else ' 当发生其他不可预知的错误，退出本程序
Exit Sub
End Select
Resume ' 再次执行原出错语句
End Sub