我有一个访问表单,其中包含从表中填充的表单名称的下拉列表。当有人从下拉列表中选择特定项目时,我希望能够显示其中一个相应的子表单。例如,我在下拉列表中有表格1,表格2,表格3。我选择表格2,然后出现子表格2。我尝试访问VBA中的子表单visible属性,但有多个错误,它从来没有完全工作。有人可以帮忙吗?
这是填充下拉列表的代码:
Private Sub Form_Load()
Dim dba As Database
Dim rst As Recordset
Dim SQL As String
Set dba = CurrentDb
Set rst = dba.OpenRecordset("tbl_Forms", dbOpenDynaset, dbSeeChanges)
SQL = "SELECT ListName FROM tbl_Forms"
Set rst = dba.OpenRecordset(SQL, dbOpenDynaset, dbSeeChanges)
Set rst = Nothing
Set dba = Nothing
End Sub
对于我尝试的子窗体可见性:
forms!mainform.Form1.visbile = false
forms!mainform.Form2.visbile = false
forms!mainform.Form3.visbile = false
Select case dropdown
case 1
forms!mainform.Form1.visbile = true
forms!mainform.Form2.visbile = false
forms!mainform.Form3.visbile = false
case 2
forms!mainform.Form1.visbile = false
forms!mainform.Form2.visbile = true
forms!mainform.Form3.visbile = false
case 3
forms!mainform.Form1.visbile = false
forms!mainform.Form2.visbile = false
forms!mainform.Form3.visbile = true
end select
答案 0 :(得分:2)
这很粗糙,但它应该给你一个开始。您可以使用所需子表单的字符串名称设置子表单容器的源对象。完成后,您可以获取MS Access定义的基本Form类的引用,并使用其中定义的属性和方法。
如果您愿意,也可以获取对特定表单的引用,但这会变得更加复杂。
从这里开始,您需要稍微玩一下才能让它按照您想要的方式运行。
Option Compare Database
Option Explicit
Private currentSubformReference As Form
Private Sub cboSubForms_AfterUpdate()
LoadSubform (cboSubForms)
currentSubformReference.DividingLines = False
currentSubformReference.ControlBox = False
' . . . Etc
End Sub
Private Sub LoadSubform(ByVal SubformName As String)
'Set the SourceObject property of the subform/subreport
' container object on your form:
Me.sfmContainer.SourceObject = SubformName
' Grab a reference to the Form that is used as the
' SourceObject (this will only get you access to the
' properties exposed by the MS Access Form Class):
Set currentSubformReference = Me.sfmContainer.Form
End Sub
答案 1 :(得分:1)
在Dropdown中将三个值写为Form1,Form2,Form3(或您的表单名称),然后编写代码:
forms!mainform.Form1.visbile = false
forms!mainform.Form2.visbile = false
forms!mainform.Form3.visbile = false
With Me.
Select case Me.DropDownName.Value
case "Form1"
With Me.MainFormName.Form
.Form1.visbile = true
.Form2.visbile = false
.Form3.visbile = false
End With
case "Form2"
With Me.MainFormName.Form
.Form1.visbile = false
.Form2.visbile = true
.Form3.visbile = false
End With
case "Form3"
With Me.MainFormName.Form
.Form1.visbile = false
.Form2.visbile = false
.Form3.visbile = true
End With
End Select
End With