【发布时间】:2020-05-04 18:06:05
【问题描述】:
[在 MS Access 2019 Professional Plus 2019 中工作。但这并不重要。]
我的设置可以缩小到这个层次结构,它本身就可以很好地工作:
'------> Hierarchy of objects :
"myMainForm" contains :
"select_client" (ComboBox based on "client" table)
"select_status" (ComboBox based on "status" table)
' and some multi-select checkboxes that forbid use of Master/Child feature
"mySubForm"
"Source Object" : "myQuery" ' "mySubForm" not saved with an explicit name, local to "myMainForm"
"mQuery" filters the table "course" :
"client_ID" criteria : [Forms]![myMainForm]![select_client]
"status_ID" criteria : [Forms]![myMainForm]![select_status]
'------> "myMainForm" VBA to requery on change (could be performed with Macros) :
Private Sub select_client_Change()
Me!mySubForm.Requery
End Sub
Private Sub select_status_Change()
Me!mySubForm.Requery
End Sub
然后我尝试将“myMainForm”嵌入到更高级别的表单中(比如说“myNavForm”):
"myNavForm" contains :
"myMainForm" contains :
' same as above from there
"mySubForm"
当我尝试运行"myNavForm" 时,系统提示我输入[Forms]![myMainForm]![select_client] 和[Forms]![myMainForm]![select_status],而"myMainForm" 完全停止工作。
我首先认为问题与触发 "myQuery" 时尚未加载 ComboBox 值有关,因此我将以下 VBA 添加到 "myMainForm" :
Private Sub Form_Load()
Me!mySubForm.SourceObject = "Query.myQuery"
Me!mySubForm.Requery
End Sub
Private Sub Form_Unload(Cancel As Integer)
Me!mySubForm.SourceObject = ""
End Sub
但问题仍然存在。我想它来自我这边的范围/路径错误,但我无法弄清楚。如上所示,我在"myQuery" 中尝试了绝对路径。我还尝试使用带有.Parent 的相对路径,但无法使"myMainForm" 使用它(甚至没有嵌入"myNavForm")。
因此,我被困住了,被搞砸了,绝望了,我想要我的 Node/MongoDB 环境,但我必须在 Access 中执行此操作(不要问!)。
知道如何使它与嵌入一起工作吗?尽可能地,我宁愿坚持这种设置并避免输入更复杂的 VBA(语法让我头疼),但如果需要,我愿意接受 ;-)
感谢任何帮助我摆脱噩梦的帮助!
[编辑:我的用例中的解决方案基于接受的答案(供未来的读者使用!)]
感谢@Olivier,这是完美运行的解决方案:
'------> Hierarchy of objects :
"myMainForm" contains :
"select_client" '(ComboBox based on "client" table)
"select_status" '(ComboBox based on "status" table)
"mySubForm"
"Source Object" : "myQuery" 'remove all criteria from the query !
' ------> Handle the rest in VBA :
'Lets write this only once...
Private Sub updateResult()
mySubForm.Form.Filter = "client_ID=" & select_client & " AND status_ID=" & select_status
mySubForm.Form.FilterOn = True
End Sub
'Apply filter with default values upon loading
Private Sub mySubForm_Current()
updateResult
End Sub
'Update filter whenever a control is updated
Private Sub select_client_AfterUpdate()
updateResult
End Sub
Private Sub select_status_AfterUpdate()
updateResult
End Sub
总而言之,我终于有了一个很好的框架,可以在任何控件更新后即时更新任何多条件查询,并立即在表单上查看结果,就像任何现代 GUI 一样!再次感谢奥利维尔!
【问题讨论】:
标签: vba ms-access ms-access-2016