简体   繁体   English

将焦点移动到 MS Access 中的子窗体时的主窗体记录验证

[英]Main form record validation when moving focus to subform in MS Access

Using Access 2016, I have a main form frmInvoice which has a subform frmInvoiceDetail .使用 Access 2016,我有一个主表单frmInvoice ,它有一个子表单frmInvoiceDetail My main form is bound to a table with a Required field PaymentMethod .我的主表单绑定到一个带有必填字段PaymentMethod的表。 However, the field is usually not populated until after the items on the invoice have been entered into the continuous subform.但是,通常直到发票上的项目输入到连续子表单后才会填充该字段。 I want the required field validation to kick in when the user moves away from that invoice record or closes the form, but not when switching focus between the main form and subform.当用户离开该发票记录或关闭表单时,我希望必填字段验证启动,但在主窗体和子窗体之间切换焦点时不启动。

I have tried removing the Required status on the field and running VBA code that checks the field is populated when the user closes the form or moves to another invoice using on-form controls, but there seems to be no way of running this code when the user moves to another invoice record using the navigation buttons.我已尝试删除字段上的必需状态并运行 VBA 代码来检查该字段是否在用户关闭表单或使用表单控件移动到另一个发票时填充,但似乎无法运行此代码时用户使用导航按钮移动到另一个发票记录。

Any ideas?有任何想法吗? Thanks for reading...谢谢阅读...

Forms and objects in Access have an order of operations. Access 中的窗体和对象具有操作顺序。 Order of Events MS Article 事件顺序 MS 文章

Unfortunately it appears that you would need to invoke the RecordExit event but it is not available.不幸的是,您似乎需要调用 RecordExit 事件,但它不可用。 More detail here: RecordExit Didn't make the cut更多细节在这里: RecordExit 没有成功

Example code using ADODB.Recordset as Form.Recordset and ADODB.Recordsets WillMove event to track recordset moves.使用ADODB.Recordset 作为 Form.RecordsetADODB.Recordsets WillMove 事件来跟踪记录集移动的示例代码。

For some reason (I'm novice in ADODB-Forms), the rst recordset and the forms recordset are out of sync in WillMove event.出于某种原因(我是 ADODB 表单的新手),第rst记录集和表单记录集在WillMove事件中不同步。 You have to use rst recordset check values.您必须使用rst记录集检查值。 The form controls will show the values of the record you want to move to.表单控件将显示您要移动到的记录的值。 Then you have to set the forms recordset back to rst or form will move.然后您必须将表单记录集设置回 rst 或表单将移动。

Use this code in the Form:在表单中使用此代码:

Option Explicit
Private WithEvents rst As ADODB.Recordset

Private Sub Form_Close()
Set rst = Nothing
End Sub

Private Sub Form_Open()
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con = CurrentProject.Connection
con.CursorLocation = adUseClient
con.Open

Set rst = New ADODB.Recordset
With rst
    .ActiveConnection = con 
    .LockType = adLockBatchOptimistic
    .CursorType = adOpenDynamic
    .Open "Select * from Table"
End With
Set Me.Recordset = rst
End Sub

Private Sub rst_WillMove(ByVal adReason As ADODB.EventReasonEnum, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
If Len(rst.Fields("PaymentMethod") & vbNullString) = 0 Then
    adStatus = adStatusCancel
    Set Me.Recordset = rst
End If

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

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