1

我正在使用 Microsoft Access 2007。运行下面的子程序后,我收到以下错误消息:“关闭对象时不允许操作” 尽管有此错误消息,记录仍插入到相应的表中。有人可以解释可能导致此错误的原因吗?

Private Sub Save_Click()

Dim rs As ADODB.Recordset
Dim SQL As String

On Error GoTo HandleError

Set rs = New ADODB.Recordset

SQL = "INSERT INTO [Hiring Entity]  ([Hiring Entity]) VALUES (""RICH PC2"");"

rs.Open SQL, CurrentProject.Connection, _
      adOpenDynamic, adLockOptimistic

With rs
    .AddNew
End With

   Set rs = Nothing
       rs.Close

ExitHere:

    Exit Sub

HandleError:
    MsgBox Err.Description
    Resume ExitHere
End Sub
4

2 回答 2

1

您正试图访问一个不存在的对象。

改变这个:

Set rs = Nothing
rs.Close

对此:

rs.Close
Set rs = Nothing

你试图在它消失后关闭它。

于 2013-04-16T16:23:55.817 回答
1

@Pow-lan explained the error occurred because you set rs to Nothing before attempting rs.Close. The .Close method is not available for a recordset object variable after you set it to Nothing.

However, I'm left wondering why you use a recordset at all. Your SQL string contains an INSERT statement. So if the point is to add a row, just execute the INSERT statement. No recordset required.

CurrentProject.Connection.Execute SQL

Actually, you don't even need ADO (CurrentProject.Connection.Execute) to execute that query. DAO should be fine.

CurrentDb.Execute SQL, dbFailOnError
于 2013-04-16T17:16:52.163 回答