2

我有一个访问表单,其中包含从表中填充的表单名称的下拉列表。当有人从下拉列表中选择特定项目时,我希望能够显示相应的子表单中的 1 个。例如,我在下拉列表中有表格 1、表格 2、表格 3。我选择表格 2,然后出现子表格 2。我尝试访问 VBA 中的子窗体可见属性,但遇到了多个错误,它从来没有完全奏效。任何人都可以帮忙吗?

这是填充下拉列表的代码:

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 
4

2 回答 2

2

这是粗略的,但它应该给你一个开始。您可以使用所需子表单的字符串名称设置子表单容器的源对象。完成此操作后,您可以获取对 MS Access 定义的基本表单类的引用,并使用其中定义的属性和方法。

如果您愿意,您也可以获取对您的特定表单的引用,但这会变得更加复杂。

从这里开始,您将需要对其进行一些操作,以使其按照您想要的方式运行。

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
于 2012-12-24T15:22:15.833 回答
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 
于 2017-05-29T09:53:24.070 回答