"添加记录"给出"不能去指定的记录"首次使用Access后

时间:2015-01-15 18:42:05

标签: ms-access

我在表单中有一个命令按钮,允许用户添加新记录。该命令仅在窗体打开后第一次单击时才有效。我可以在没有任何错误的情况下浏览记录,并且在打开表单后第一次添加记录时不会出错。尝试添加第二条记录后,"无法转到指定的记录"显示错误消息。

我可以在查询中进行编辑而不会出现问题,我可以毫无问题地编辑内容。如果我关闭表单并重新打开它,我可以添加一个没有问题的新记录。有人有想法吗?

使用VBA,它是标准的新增代码,没有别的。发布此表单的所有代码如下。列出的一些命令按钮是宏,不知道是否有所作为。

Private Sub add_Click()
  DoCmd.GoToRecord , , acNewRec
End Sub

Private Sub edit_info_Click()
    Date_of_Echo.Locked = False
    ID.Locked = False
    AoV.Locked = False
    AI.Locked = False
    MR.Locked = False
    TR.Locked = False
    TR_velocity.Locked = False
    PA_pressures.Locked = False
    LVeDD.Locked = False
    LVeSD.Locked = False
    RV_function.Locked = False
    comments.Locked = False
    Me.next.Visible = False
    Me.previous.Visible = False
    Me.first.Visible = False
    Me.last.Visible = False
    Me.add.Visible = False
    Me.save.Visible = True
    Me.save.SetFocus
    Me.edit_info.Visible = False

End Sub

Private Sub Form_Current()
   If Me.NewRecord Then
    Me.recordcounter.Caption = "New Record"
    Me.next.Visible = False
    Me.previous.Visible = False
    Me.first.Visible = False
    Me.last.Visible = False
    Me.add.Visible = False
    Me.edit_info.Visible = False
    Me.save.Visible = True
    Date_of_Echo.Locked = False
    ID.Locked = False
    AoV.Locked = False
    AI.Locked = False
    MR.Locked = False
    TR.Locked = False
    TR_velocity.Locked = False
    PA_pressures.Locked = False
    LVeDD.Locked = False
    LVeSD.Locked = False
    RV_function.Locked = False
    comments.Locked = False
   Else
    Me.recordcounter.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.RecordCount
    Me.next.Visible = True
    Me.previous.Visible = True
    Me.first.Visible = True
    Me.last.Visible = True
    Me.add.Visible = True
    Me.edit_info.Visible = True
    Me.save.Visible = False
    End If
 End Sub
Private Sub save_Click()
DoCmd.save
    Me.previous.Visible = True
    Me.first.Visible = True
    Me.last.Visible = True
    Me.next.Visible = True
    Me.add.Visible = True
    Me.edit_info.Visible = True
    Me.recordcounter.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.RecordCount
    Me.add.SetFocus
    Me.save.Visible = False
    Date_of_Echo.Locked = True
    ID.Locked = True
    AoV.Locked = True
    AI.Locked = True
    MR.Locked = True
    TR.Locked = True
    TR_velocity.Locked = True
    PA_pressures.Locked = True
    LVeDD.Locked = True
    LVeSD.Locked = True
    RV_function.Locked = True
    comments.Locked = True

End Sub
Private Sub next_Click()
    On Error GoTo Err_cmdLastRecord_Click

    Me.AllowAdditions = False
    DoCmd.GoToRecord , , acNext

Exit_cmdLastRecord_Click:
    Exit Sub

Err_cmdLastRecord_Click:
    MsgBox " There are no more records ", vbExclamation, ""
    Resume Exit_cmdLastRecord_Click

End Sub

3 个答案:

答案 0 :(得分:2)

我的第一个想法是,在您尝试转到新记录之前,您的记录并未保存,因此请尝试此操作,看看它是否解决了问题。

Private Sub add_Click()

    If Me.Dirty Then
        Me.Dirty = False
    End If

  DoCmd.GoToRecord , , acNewRec

End Sub

答案 1 :(得分:1)

如果有人仍然遇到这个问题,我无法解释它为什么会发生,但这里有一个解决方法:

重置表单上的 AllowAdditionsProperty

Me.[form_name].Form.AllowAdditions = False
Me.[form_name].Form.AllowAdditions = True

这是一个非常奇怪的问题,因为我的代码已经运行了好几个星期。我正在使用这样的东西:

Me.frm.SetFocus
   DoCmd.GoToRecord , , acNewRec
     !Field1 = value1
     !Field2 = value2

它运行良好,然后出乎意料地开始出现“无法转到指定记录”错误,即使我没有对代码进行任何更改。表单设置很好,我可以整天在基础表中添加记录。我什至在 Immediate 窗口中检查了表单的 AllowAdditions 属性,结果显示它被设置为 True。但我尝试了这项工作,并且奏效了。

现在我在 Docmd.GoToNewRoecordacNewRecord 的每个实例之后添加这两行,并且它正在工作。

答案 2 :(得分:0)

我提到的另一个具有相同问题的问题是,您应该检查Form数据源查询。是否所有字段都可用,联接是否正确? 就我而言,我将索引连接从文本字段更改为数字,并且还需要在“表单数据”源查询中进行更新。