Sql 访问中的BOF和EOF属性可能存在时间问题?

Sql 访问中的BOF和EOF属性可能存在时间问题?,sql,ms-access,vba,Sql,Ms Access,Vba,我创建了一个测试,使用BOF和EOF属性检查记录集是否为空(请参见下面的代码)。我知道这段代码的工作原理与我以前在其他实例中使用过的代码相同。但是,由于某种原因(在测试时),当我删除子窗体中的最后一条记录时,属性都为FALSE。据我所知,这意味着还有记录。但是,一旦代码运行,子窗体中就没有记录,并且子窗体所基于的查询为空。我的想法是,这是一个时间问题,但我非常感谢您的帮助,启发我,以及如何改进代码,使其真正按照预期工作 让我觉得这是一个时间问题的是,当我重新加载包含子表单的表单时,两个属性状态现

我创建了一个测试,使用BOF和EOF属性检查记录集是否为空(请参见下面的代码)。我知道这段代码的工作原理与我以前在其他实例中使用过的代码相同。但是,由于某种原因(在测试时),当我删除子窗体中的最后一条记录时,属性都为FALSE。据我所知,这意味着还有记录。但是,一旦代码运行,子窗体中就没有记录,并且子窗体所基于的查询为空。我的想法是,这是一个时间问题,但我非常感谢您的帮助,启发我,以及如何改进代码,使其真正按照预期工作

让我觉得这是一个时间问题的是,当我重新加载包含子表单的表单时,两个属性状态现在都为TRUE…奇怪。请参阅下面的代码:

Private Sub supp_del_Click()
    Dim Msg As String
    Dim result As Integer
    Dim Title As String
    Dim dbrec As Recordset
    Dim checkDel As Variant
    Dim idCheck As Integer

    'Run the Error handler when an error occurs.'
On Error GoTo Errhandler

    Set dbrec = Me.supp_subform.Form.Recordset

    'delete record'
    'check existing selected record'
    If Not (dbrec.EOF And dbrec.BOF) Then
        'set msgbox text'
        Msg =   "Are you sure you want to delete this supplier?" & vbCrLf & vbCrLf & _
                "ID: " & dbrec.Fields("supp_ID") & vbCrLf & _
                "Name: " & dbrec.Fields("supp_name") & vbCrLf & _
                "Map: " & dbrec.Fields("supp_map") & vbCrLf & _
                "Tax code: " & dbrec.Fields("tax_code") & vbCrLf & _
                "Department: " & dbrec.Fields("Department")
        Title = "Point of no return"

        result = MsgBox(Msg, vbYesNo, Title)

        'confirm delete'
        If result = vbYes Then
            idCheck = dbrec.Fields("supp_id")
            'delete now'
            CurrentDb.Execute "DELETE FROM suppliers " & _
                                " WHERE supp_ID=" & idCheck

            If DLookup("[supp_ID]", "[suppliers]", "supp_id=" & idCheck) Then
                'set msgbox text'
                Msg = "Cannot delete as the supplier has an invoice allocated to them."
                Title = "Cannot delete"
                result = MsgBox(Msg, vbOK, Title)
            Else
                'refresh data in list'
                dbrec.Requery
                'enable/disable buttons depending on if form list is empty'
                If Not (dbrec.EOF And dbrec.BOF) Then
                    Me.supp_subform.Enabled = True
                    Me.Supp_edit.Enabled = True
                    Me.supp_del.Enabled = True
                Else
                    Me.supp_subform.Enabled = False
                    Me.Supp_edit.Enabled = False
                    Me.supp_del.Enabled = False
                End If
            End If
        End If
    End If
    Exit Sub
Errhandler:
    Select Case Err
        Case 3021 ' error '3021 'no current record - it think's there aren't any records'
            'select all records in suppliers'
            sqlstr = "SELECT supp_ID, supp_name, tax_code, supp_map, Department FROM suppliers;"
            Set dbrec = CurrentDb.OpenRecordset(sqlstr)
            'select first record and allocate to form fields'
            dbrec.MoveFirst
            'set msgbox text'
            Msg =   "Are you sure you want to delete this supplier?" & vbCrLf & vbCrLf & _
                    "ID: " & dbrec.Fields(0) & vbCrLf & _
                    "Name: " & dbrec.Fields(1) & vbCrLf & _
                    "Map: " & dbrec.Fields(3) & vbCrLf & _
                    "Tax code: " & dbrec.Fields(2) & vbCrLf & _
                    "Department: " & dbrec.Fields(4)
            Title = "Point of no return"

            result = MsgBox(Msg, vbYesNo, Title)
            'confirm delete'
            If result = vbYes Then
                idCheck = dbrec.Fields(0)
                'delete now'
                CurrentDb.Execute "DELETE FROM suppliers " & _
                                    " WHERE supp_ID=" & idCheck
                If DLookup("[supp_ID]", "[suppliers]", "supp_id=" & idCheck) Then
                    'set msgbox text'
                    Msg = "Cannot delete as the supplier has an invoice allocated to them."
                    Title = "Cannot delete"
                    result = MsgBox(Msg, vbOK, Title)
                Else
                    'refresh data in list'
                    Set dbrec = Me.supp_subform.Form.Recordset
                    dbrec.Requery
                    If Not (dbrec.EOF And dbrec.BOF) Then
                        Me.supp_subform.Enabled = True
                        Me.Supp_edit.Enabled = True
                        Me.supp_del.Enabled = True
                    Else
                        Me.supp_subform.Enabled = False
                        Me.Supp_edit.Enabled = False
                        Me.supp_del.Enabled = False
                    End If
                End If
            End If
        Case Else 'all other errors'
            Msg = "Error # " & Str(Err.Number) & " was generated by " _
                    & Err.Source & Chr(13) & Chr(13) & Err.Description
            MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
    End Select
End Sub

在问题中张贴的代码中,删除起始行

CurrentDb.Execute ...
并替换为:

dbrec.Delete ' Delete the current record from the recordset
dbrec.MoveFirst ' Move to the top of the recordset for testing
If Not dbrec.EOF Then ... ' Recordset contains records 
删除该行:

dbrec.Requery
并替换为:

dbrec.Delete ' Delete the current record from the recordset
dbrec.MoveFirst ' Move to the top of the recordset for testing
If Not dbrec.EOF Then ... ' Recordset contains records 

您正在重新查询表单,但应该重新查询记录集。将行
Me.supp\u subform.Form.Requery
更改为
dbrec.Requery
@user3728595我按照您所说的进行了更改,并更新了上面的代码。奇怪的是,现在BOF和EOF属性不断地说是真的,即使有记录。。。我有一个添加子以及类似(相同)的编码,这似乎是工作。我检查了记录集的“absolutePosition”,如果有一条记录,它应该表示0代表1条记录,并且看起来很好(-1代表0条记录)。我想我可以用它来代替我的测试,但仍然不确定为什么它不能正常工作。我再次检查了,你对BOF/EOF的看法是正确的,尽管事实上“如果使用重新查询方法后记录集对象的BOF和EOF属性设置均为True,则查询不会返回任何记录,且记录集不包含任何数据。“设计行为到此为止。您可以使用以下代码代替上面的
CurrentDb.Execute
方法:
dbrec.Delete
|
dbrec.MoveFirst
|
如果dbrec.EOF那么…
@user3728595我不确定我是否完全遵循了…我如何合并dbrec.Delete代码等而不是CurrentDb.Execute?”?