15

プログラムの最初の部分で使用しています

On Error GoTo start

私の第二部で私が再び使用していると仮定します

On Error Resume Next

この2番目のエラートラップは、最初のエラートラップがまだアクティブであるため、アクティブになりません。使用後に最初のエラーハンドラーを非アクティブ化する方法はありますか?

Set objexcel = CreateObject("excel.Application")
                     objexcel.Visible = True
                     On Error GoTo Openwb
                     wbExists = False
                     Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
                     Set objSht = wbexcel.Worksheets("Sheet1")
                     objSht.Activate
                     wbExists = True
Openwb:
                     
                     On Error GoTo 0
                     If Not wbExists Then
                     objexcel.Workbooks.Add
                     Set wbexcel = objexcel.ActiveWorkbook
                     Set objSht = wbexcel.Worksheets("Sheet1")

                     End If
                     
                     On Error GoTo 0
                                         
Set db = DBEngine.opendatabase("C:\book.mdb")
Set rs = db.OpenRecordset("records")

Set rs2 = CreateObject("ADODB.Recordset")
rs2.ActiveConnection = CurrentProject.Connection


For Each tdf In CurrentDb.TableDefs
   
   If Left(tdf.Name, 4) <> "MSys" Then
        rs.MoveFirst
        strsql = "SELECT * From [" & tdf.Name & "] WHERE s=15 "

        Do While Not rs.EOF
            On Error Resume Next
            
            rs2.Open strsql 

最後のステートメントの実行時に、エラーを無視して次のテーブルに移動したいのですが、エラー処理が機能していないようです。

4

5 に答える 5

17

On error goto 0エラー処理のためにビジュアルベーシックに手を差し伸べる(一般的なメッセージボックスで)

On error goto labelコードを次のラベルにリダイレクトします。

On error resume nextエラーを無視して続行します

Resume nextエラーが発生した後、コードを次の行にリダイレクトします

のような命令の組み合わせを意味します。

    On Error goto 0
    ...
    On Error goto 0

意味がありません

また、「エラー時」の命令をリダイレクトする場合は、次のようにする必要があります。

    Do While Not rs.EOF
        
        On Error Resume Next
        rs2.Open strsql
        On error Goto 0

        rs2.moveNext

    Loop

エラーを (処理などのために) ラベルにリダイレクトしてから、エラーが発生したコードに戻りたい場合は、次のように記述する必要があります。

    On error goto label
    ...
    ...
    On error goto 0
    exit sub (or function)

    label:
    ....
    resume next
    end function

ただし、エラー管理をより厳密にすることをお勧めします。最初に次のようなことができるはずです。

    Set objexcel = CreateObject("excel.Application")
    objexcel.Visible = True

    On Error GoTo error_Treatment
    wbExists = False
    Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
    Set objSht = wbexcel.Worksheets("Sheet1")
    objSht.Activate
    wbExists = True
    On error GoTo 0

    Set db = DBEngine.opendatabase("C:\book.mdb")
    Set rs = db.OpenRecordset("records")

    Set rs2 = CreateObject("ADODB.Recordset")
    rs2.ActiveConnection = CurrentProject.Connection

    For Each tdf In CurrentDb.TableDefs
        ....
        'there are a number of potential errors here in your code'
        'you should make sure that rs2 is closed before reopening it with a new instruction'
        'etc.'
    Next tdf

    Exit sub

    error_treatment:
    SELECT Case err.number
       Case **** '(the err.number raised when the file is not found)'
           objexcel.Workbooks.Add
           Set wbexcel = objexcel.ActiveWorkbook
           Set objSht = wbexcel.Worksheets("Sheet1")
           Resume next 'go back to the code'
       Case **** '(the recordset cannot be opened)'
           ....
           ....
           Resume next 'go back to the code'
       Case **** '(whatever other error to treat)'
           ....
           ....
           Resume next 'go back to the code'
       Case Else
           debug.print err.number, err.description '(check if .description is a property of the error object)'
           'your error will be displayed in the immediate windows of VBA.' 
           'You can understand it and correct your code until it runs'
       End select
    End sub

次のステップは、コード内のエラーを予測して、err オブジェクトが発生しないようにすることです。たとえば、次のような汎用関数を作成できます。

    Public function fileExists (myFileName) as Boolean

次に、xls ファイルの存在をテストすることにより、コードでこの関数を利用できます。

    if fileExists("C:\REPORT3.xls") Then
        Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
    Else
       objexcel.Workbooks.Add
       Set wbexcel = objexcel.ActiveWorkbook
    Endif        
    Set objSht = wbexcel.Worksheets("Sheet1")
    objSht.Activate

wbExist 変数はもう必要ありません...

同様に、レコードセットにレコードがない場合も想定する必要があります。テストする前に rs.MoveFirst を書き留めると、エラーが発生する可能性があります。あなたはそれから書くべきです

    If rs.EOF and rs.BOF then
    Else
        rs.moveFirst
        Do while not rs.EOF
             rs.moveNext
        Loop
    End If
于 2008-12-01T16:43:23.117 に答える
5

エラーをクリアする必要があります。このコードを入れてみてください:

If Err.Number > 0 Then
    Err.Clear
End If

Err.Number を使用して、特定のエラー ケースを処理することもできます。

于 2008-12-01T14:41:13.690 に答える
3

It is nearly always better to avoid errors, rather than handling them. For example:

Set objexcel = CreateObject("excel.Application")
objexcel.Visible = True

'On Error GoTo Openwb '
'wbExists = False '

If Dir("C:\REPORT3.xls") = "" Then
    objexcel.Workbooks.Add
    Set wbexcel = objexcel.ActiveWorkbook
    Set objSht = wbexcel.Worksheets("Sheet1")
Else
    Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
    Set objSht = wbexcel.Worksheets("Sheet1")
End If

objSht.Activate
'wbExists = True '
于 2008-12-01T14:57:38.900 に答える
1

試す

On Error Goto 0

詳細については、こちらをご覧ください:http: //msdn.microsoft.com/en-us/library/bb258159.aspx

于 2008-12-01T14:11:03.893 に答える