закрыть рекордсет

wua
Дата: 19.01.2012 16:33:07
1-й вариант:
Private Sub Form_Load()
On Error GoTo ErrorHandler

   'some code
    rs.close
 Set rs = Nothing

ErrorHandlerExit:
   Exit Sub
ErrorHandler:
   MsgBox Err.Number & " Form_Load: " & Err.Description, vbInformation, "Error"
   Resume ErrorHandlerExit
End Sub


2-й вариант:
Private Sub Form_Load()
On Error GoTo ErrorHandler

   'some code

ErrorHandlerExit:
    rs.close
 Set rs = Nothing
   Exit Sub
ErrorHandler:
   MsgBox Err.Number & " Form_Load: " & Err.Description, vbInformation, "Error"
   Resume ErrorHandlerExit
End Sub


и в обработчике у применением 1-го или 2-го варианта

Private Sub Form_Load()
On Error GoTo ErrorHandler

   'some code

ErrorHandlerExit:
   Exit Sub
ErrorHandler:
   MsgBox Err.Number & " Form_Load: " & Err.Description, vbInformation, "Error"
    rs.close
 Set rs = Nothing
   Resume ErrorHandlerExit
End Sub


Как правильно?
alvk
Дата: 19.01.2012 16:46:57
wua,

1
Guest33
Дата: 19.01.2012 16:51:45
Private Sub Form_Load()
On Error GoTo ErrorHandler

   'some code

ErrorHandlerExit:
   On Error Resume Next
   rs.close
   Set rs = Nothing
   Exit Sub
ErrorHandler:
   MsgBox Err.Number & " Form_Load: " & Err.Description, vbInformation, "Error"
   Resume ErrorHandlerExit
End Sub
George-III
Дата: 19.01.2012 16:54:42
Private Sub Form_Load()
On Error GoTo ErrorHandler
  dim flg As Boolean
  set rs=db.openrecordset(...) 
  flg=true
  'some code
   rs.close
   Set rs = Nothing
   flg=false
ErrorHandlerExit:
   if flg then
     rs.close
     Set rs = Nothing  
   end if
   Exit Sub
ErrorHandler:
   MsgBox Err.Number & " Form_Load: " & Err.Description, vbInformation, "Error"
   Resume ErrorHandlerExit
End Sub



ИМХО так
Анатолий ( Киев )
Дата: 19.01.2012 16:56:34
Во 2-м варианте при возникновении ошибки на rs.close вы попадете в бесконечный цикл
В 3-м варианте, если нет ошибки, закончите процедуру без rs.close
wua
Дата: 19.01.2012 16:59:30
Прежде делал по первому варианту. Вопрос возник после

автор
9. Failing to close a recordset

It is poor programming to open anything without explicitly closing it. This problem is particularly acute in Access 97. Short of pressing Ctrl+Alt+Del, you may find that Access will not quit if recordsets or other objects are not closed and dereferenced.

Solution:

Always close recordsets and set objects to Nothing in the error recovery of your procedure. Use this construct:

    Sub MyProc
    On Error Goto Err_MyProc
        Dim db as Database
        Dim rst As Recordset

        Set db = CurrentDb()
        Set rst = db.OpenRecordset("MyTable")
        'Useful code here.
        rst.Close            'Close what you opened.

    Exit_MyProc:
        Set rst = Nothing    'Deassign all objects.
        Set db = Nothing
        Exit Sub

    Err_MyProc:
        'Error handler here.
        Resume Exit_MyProc
    End Sub
alvk
Дата: 20.01.2012 02:45:16
wua,

то, что вы привели, не первый вариант.

George-III , красиво.
MrMuscul
Дата: 20.01.2012 14:23:51
Private Sub Form_Load()
On Error GoTo ErrorHandler
Dim db as Database
Dim rst As Recordset

Set db = CurrentDb()
Set rst = db.OpenRecordset("MyTable")

ExitHere:
    If Not (rst Is Nothing) Then rst.Close:  Set rst = Nothing
    If Not (db Is Nothing) Then db.Close:  Set db = Nothing
    Exit Sub
ErrorHandler:
   'Какой то код
    Resume ExitHere
End Sub
Guest33
Дата: 20.01.2012 14:31:57
MrMuscul
...

Вредный совет, если ему последовать для АДО-рекодсета, то бесконечный цикл:
Private Sub test_close_rst()
    On Error GoTo ErrorHandler
    Dim rst As ADODB.Recordset
    
    Set rst = New ADODB.Recordset
    
ExitHere:
        If Not (rst Is Nothing) Then rst.Close:  Set rst = Nothing
        Exit Sub
ErrorHandler:
       'Какой то код
        Resume ExitHere
End Sub
MrMuscul
Дата: 20.01.2012 16:13:45
Guest33,

Ну если так то там будет совсем по другому. Там уже коннектион будет