Access中的BOF和EOF属性可能出现时间问题?

时间:2015-02-24 11:38:17

标签: sql vba ms-access access-vba

我创建了一个测试来检查记录集是否为空,使用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

1 个答案:

答案 0 :(得分:0)

在问题中发布的代码中,删除以

开头的行
CurrentDb.Execute ...

并替换为:

dbrec.Delete ' Delete the current record from the recordset

删除该行:

dbrec.Requery

并替换为:

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