You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Access 2013编辑表单报错3021:首次编辑后后续无当前记录

Troubleshooting Error 3021 ("No Current Record") in Your Access Form Edit Flow

Let's break down this error 3021 ("No current record") issue you're facing—your scenario (works first time, fails on subsequent edits) gives us clear clues about what's going wrong with that Tableinform.Form.Requery call.

1. The Core Problem: Requery Loses the Current Record

When you run Requery, it reloads the form's recordset from scratch. Unlike Refresh, it doesn't keep your position in the record list. Here's why this breaks after the first edit:

  • On first use, the form loads with a valid current record. After editing and running Requery, if the edited record is no longer in the recordset (e.g., filtered out, deleted by another process, or the query behind the form excludes it), the form can't find a "current record" to land on, triggering error 3021.
  • Even if the record exists, Requery resets the record pointer to the start of the set—but if the form's settings (like DataEntry = True) force it to only show new records, you'll still get this error.

2. Fix: Save the Current Record's ID and Reposition After Requery

The easiest fix is to track the edited record's primary key before requerying, then jump back to it afterward. Here's how to adjust your button code:

Private Sub YourUpdateButton_Click()
    Dim currentRecordID As Variant
    Dim targetForm As Form
    
    Set targetForm = Tableinform.Form
    
    ' Step 1: Save the primary key of the current record (skip if it's a new record)
    If Not targetForm.NewRecord Then
        currentRecordID = targetForm!YourPrimaryKeyField.Value ' Replace with your actual PK field name
    End If
    
    ' Step 2: Ensure the current edit is saved first
    If targetForm.Dirty Then
        targetForm.Dirty = False ' Forces save of unsaved changes
    End If
    
    ' Step 3: Run the requery
    targetForm.Requery
    
    ' Step 4: Reposition to the edited record if it exists
    If Not IsEmpty(currentRecordID) Then
        With targetForm.RecordsetClone
            .FindFirst "YourPrimaryKeyField = " & currentRecordID ' Match your PK field name
            If Not .NoMatch Then
                targetForm.Bookmark = .Bookmark ' Jump back to the record
            End If
        End With
    End If
End Sub

3. Other Quick Checks

  • Check the form's DataEntry property: If this is set to True, the form will only display new records after a requery. Go to the form's properties (Design View > Property Sheet > Data tab) and set DataEntry to False if you need to view existing records.
  • Verify the form's record source: If the form is bound to a query with dynamic filters (e.g., filters based on a hidden control that changes after edits), the requery might exclude the edited record. Double-check the query's criteria to ensure it doesn't accidentally filter out valid records.
  • Handle empty recordsets: If the requery could result in an empty recordset, add a check before trying to access any controls:
    If targetForm.Recordset.EOF And targetForm.Recordset.BOF Then
        ' Handle empty state (e.g., show a message, switch to new record)
        targetForm.SetFocus
        DoCmd.GoToRecord , , acNewRec
    End If
    

4. On the Code Highlighting Issue

That's likely just an editor setting quirk:

  • In the VBA Editor, go to Tools > Options > Editor Format to confirm syntax highlighting is enabled for VBA code.
  • If you're pasting code into another tool (like a documentation app), make sure the tool supports VBA syntax highlighting—you might need to specify the language when wrapping code blocks.

内容的提问来源于stack exchange,提问作者Plzhalp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:44:19