Pregunta

I have an access form that contains a dropdown list of form names populated from a table. When someone selects a particular item from the dropdown I want to be able to display 1 of the corresponding subforms. For example, I have Form 1, Form 2, Form 3 in the dropdown. I select Form 2 and the subform two appears. I tried accessing the subforms visible property in VBA but got multiple errors and it never quite worked. Can anyone help?

This is the code to populate the dropdown:

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

And for the subform visibility I tried:

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 
¿Fue útil?

Solución 2

Write three values in Dropdown as Form1,Form2,Form3 (or whatever your form name are) and then code:

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 

Otros consejos

This is crude, but it should give you a start. You can set the source object of a subform container using the string name of the desired subform. Once you have done that, you can than grab a reference to the base Form Class as defined by MS Access, and use the properties and methods defined therein.

You can also grab a reference to your specific form if you like, though this becomes more complicated.

From here, you will need to play with it a little to get it to behave the way you want.

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top