经常需要从主表单中打开辅助表单以输入一些信息。 以辅助形式输入的数据预期会出现在主要形式中,并且可以选择以某种方式进行处理,辅助形式会给主要形式一些反馈等。
此方法显示了几种方法。
- 当期望辅助表单为指向主表单的记录提供增强的接口时,第一种方法适用。 例如,较大的文本框可输入长文本,日历控件等。
该方法的主要思想是将辅助形式及其控件绑定到与主要形式相同的数据源。
主要表格代码
子表单(frmChild1)代码'opens child form and binds its control to the same datasource 'as the main one 'data in both forms synchronized because they both bound 'to the same data source Private Sub btnOpenChildForm1_Click() DoCmd.OpenForm "frmChild1" With Forms!frmChild1 'set ControlSource of child form control to 'the same as im main one .txb.ControlSource = Me.txb.ControlSource 'set child form Recordset to the same as the main one Set .Recordset = Me.Recordset 'make child form modal .Modal = True End With End Sub
注意:子表单可以选择绑定到主表单记录集的副本。 这提供了在子窗体中有效使用Me.Undo方法的机会,但需要其他逻辑来手动刷新主窗体。Private varOldValue As Variant 'holds control original value Private Sub btnCancel_Click() Me.txb = varOldValue 'restore original value CloseMe End Sub Private Sub btnSubmit_Click() CloseMe End Sub Private Sub CloseMe() DoCmd.Close acForm, Me.Name End Sub Private Sub txb_BeforeUpdate(Cancel As Integer) 'store control original value before first update occur If IsEmpty(varOldValue) Then varOldValue = Me.txb.OldValue End Sub
- 第二种方法为主要形式的数据验证提供了更多机会。 该方法的主要思想是通过属性将对主窗体控件的引用传递给子窗体,以使其有机会写回主窗体。
主要表格代码
子表单(frmChild2)代码'opens child form and passes reference to main form control 'via child form property 'data in main form will be updated by child form Private Sub btnOpenChildForm2_Click() DoCmd.OpenForm "frmChild2" With Forms!frmChild2 'pass reference to main form control 'to child form via its property Set .ConnectedTextBox = Me.txb 'make child form modal .Modal = True End With End Sub'form global variable to store reference to main form control Private objConnectedTextBox As Access.TextBox 'code to get property value Public Property Get ConnectedTextBox() As Access.TextBox Set ConnectedTextBox = objConnectedTextBox End Property 'code to set property value Public Property Set ConnectedTextBox(ByRef objNewValue As Access.TextBox) Set objConnectedTextBox = objNewValue 'set child form control value to that of main form Me.txb.Value = objNewValue.Value End Property Private Sub btnCancel_Click() CloseMe End Sub Private Sub btnSubmit_Click() With Me 'write child form control value back to main form one .ConnectedTextBox.Value = .txb.Value End With CloseMe End Sub Private Sub CloseMe() DoCmd.Close acForm, Me.Name End Sub - 第三种方法的主要思想是让子窗体通过事件在主窗体中调用代码并将值传递给它。 其余部分使用主表单代码,这为在子表单中处理子表单中输入的数据提供了高度的灵活性。
主要表格代码
子表单(frmChild3)代码'object of Form_frmChild3 type is declared with events Dim WithEvents frmChild3 As Access9db.Form_frmChild3 'opens child form and waits for its events 'data in main form will be updated according to what 'child form will return via events Private Sub btnOpenChildForm3_Click() DoCmd.OpenForm "frmChild3" Set frmChild3 = Forms!frmChild3 With frmChild3 'pass main form control value to child form control .txb = Me.txb 'make child form modal .Modal = True End With End Sub Private Sub frmChild3_InputCancelled() MsgBox "Input cancelled" 'destroy reference to frmChild3 no longer valid Set frmChild3 = Nothing End Sub Private Sub frmChild3_ValueSubmitted(varValue As Variant) 'set main form control value to what child form returned Me.txb.Value = varValue MsgBox "Input submitted" 'destroy reference to frmChild3 no longer valid Set frmChild3 = Nothing End Sub
注意:子表单控件和/或表单对象可以使用主表单模块中的事件声明,以处理对应的事件。 但是,这略微超出了该单一方法的大小。 :D'events declaration Event ValueSubmitted(varValue As Variant) Event InputCancelled() Private Sub btnCancel_Click() RaiseEvent InputCancelled CloseMe End Sub Private Sub btnSubmit_Click() RaiseEvent ValueSubmitted(Me.txb.Value) CloseMe End Sub Private Sub CloseMe() DoCmd.Close acForm, Me.Name End Sub
这些示例已在随附的数据库中实现。
上述方法或其组合的特定实现方式取决于允许揭示每个方法的优点并容忍每个方法的缺点的特定情况。
From: https://bytes.com/topic/access/insights/842978-forms-interaction
表单交互技巧
2140

被折叠的 条评论
为什么被折叠?



