简体   繁体   English

表单按钮以使用表验证保存记录冲突

[英]Form button to Save Record Clashing with Table Validation

I have a form which is intended for Data Entry, and a button which (should) save the record to the data table (ie goes to the next blank record) with a message box appearing saying "your record has been saved successfully". 我有一个用于数据输入的表格,以及一个(应该)将记录保存到数据表(即转到下一个空白记录)的按钮,带有一个消息框,显示“您的记录已成功保存”。

However, I have mandatory fields on the form, where the validation is set in the data table to 'Is Not Null' so I am able to define the error messages which appear. 但是,我在表单上有必填字段,其中数据表中的验证设置为“不是空”,因此我可以定义出现的错误消息。 This then causes, on the 'Save Record' button click, 1st: a messagebox appearing telling me it has saved successfully, followed by the Validation error message set in the data table, followed by "you can't go to the specified record", followed by the Macro Single Step window prompting me to 'Stop All Macros' How can I get the macro to stop running if a validation rule (set in the data table) fails? 然后,在“保存记录”按钮上单击,导致第一:出现一个消息框,告诉我它已成功保存,然后在数据表中设置了“验证错误”消息,然后是“您不能转到指定记录” ,然后是“宏单步”窗口,提示我“停止所有宏”,如果验证规则(在数据表中设置)失败,如何停止运行宏? - I'd assume this would then go on the first event of the macro builder? -我想这会在宏生成器的第一个事件上继续吗?

Thanks for any help provided! 感谢您提供的任何帮助!

You could do something like this, use this code as event procedure for your button. 您可以执行以下操作,将此代码用作按钮的事件过程。

Private Sub cmdSaveRecord_Click()

    ' Try to save the record, skip error
    On Error Resume Next
    DoCmd.RunCommand acCmdSaveRecord
    ' If not successful, display error and exit
    If Err.Number <> 0 Then
        ' Err.Description contains the field validation rule message
        MsgBox Err.Description, vbExclamation, "Error on saving"
        Exit Sub
    End If

    On Error GoTo 0

    MsgBox "Your record has been saved successfully", vbInformation
    ' new record
    DoCmd.GoToRecord acActiveDataObject, , acNewRec
    ' goto first control on new record, instead of cmdSaveRecord
    Me.Text1.SetFocus

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM