Microsoft Acess VBA-BeforeUpdate无法使用“新记录”按钮

时间:2019-11-13 20:56:12

标签: vba access-vba ms-access-forms beforeupdate

我有一个访问表单,一旦被用户填写,则需要保存该表单,但是由于他们有时忘记这样做,我决定放入一个BeforeUpdate触发器,该触发器将允许弹出消息提醒用户保存或取消后再进行任何操作行动。 我在网上找到了此代码,它适用于除“新记录”按钮以外的所有内容。 据我所知,我无法弄清楚为什么使用Me.Dirty应该可以解决问题。 为了方便起见,我在下面输入了代码


````Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctl As Control
  On Error GoTo Err_BeforeUpdate
 If Me.Dirty Then
    ' Prompt to confirm the save operation.
    If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
            "Save Record") = vbNo Then
       Me.Undo
   End If
 End If

 Exit_BeforeUpdate:
 Exit Sub

  Err_BeforeUpdate:
 MsgBox Err.Number & " " & Err.Description
 Resume Exit_BeforeUpdate
 End Sub                  ````



--------------------------------------------------------------

The code being used for new record is 


  Private Sub new_Click()
 njno = NewJobNbr()
 Job.Value = njno
 RnK.Value = ""
 Date_Requested.Value = ""
 Est_Time.Value = ""
 Originator.Value = ""
 Date_Required.Value = ""
 Description.Value = ""
 Reason_for_Request.Value = ""
 Comments.Value = ""
 Priority_Tasks.Value = ""
 TName.Value = ""
 Required.Value = ""
 Costing.Value = ""
 Completed.Value = ""
Date_Completed.Value = ""
End Sub





----------------------------------------------------------

New to VBA here so learning with the mistakes. 

1 个答案:

答案 0 :(得分:0)

我不太了解njno是什么。如果文本框绑定到表单的RecordSource,则。 在表单Current事件上使用:

Private Sub Form_Current ()

If Me.NewRecord Then
Job.Value = njno 
End if

End Sub

然后在按钮Click事件上使用:

Private Sub new_Click()
DoCmd.GoToRecod,,acNewRec
End sub