Search code examples
formsms-accessms-officems-access-2010office-2010

MS Access 2010 - How to display form fields programmatically based on previous input?


I have a form where the first field is a type that the user will choose from a combo box. I'd like the subsequent fields to display based off of what the type is. Pseudo:

  if typefield == bank
      display fields 1-4
  else
      display fields 5-8

How would I go about doing this in Access 2010? I'm not sure modal would apply since I would like this all one form. If this is not possible, any suggestions on a solution that gets close?


Solution

  • If you have all eight fields on the form and you are just wanting to make them visible based on a choice from the "typefield" then something like this would work.

    If me.typefield.value = bank Then
                Me.field5.Visible  = False 
                Me.field6.Visible  = False 
                Me.field7.Visible  = False 
                Me.field8.Visible  = False   
                Me.field1.Visible  = True    
                Me.field2.Visible  = True
                Me.field3.Visible  = True 
                Me.field4.Visible  = True
    Else
                Me.field1.Visible  = False 
                Me.field2.Visible  = False 
                Me.field3.Visible  = False 
                Me.field4.Visible  = False   
                Me.field5.Visible  = True   
                Me.field6.Visible  = True 
                Me.field7.Visible  = True
                Me.field8.Visible  = True
    End if
    

    You could of course just have 4 text boxes that you could change the control source and labels based on their choice....

    IF me.typefield.value = bank then
               Me.textbox1.ControlSource = "field1"
               Me.label1.Caption = "field1 or whatever"
              ' repeat for the other 3
    Else
               Me.textbox5.ControlSource = "field5"
               Me.label5.Caption = "field5 or whatever"
               ' repeat for the other 3
    End if