How to enable objects in subform based on value in main form?

J

Jon

Greeting,

I have a form that contains text box called “Code†and a sub form called
details contains 5 text boxes, Text1, text2, text3… What I want to do is if
Code text box contains 1 then, text1 is enabled and others are disabled. If
Code textbox contains 2 then, text1 & text2 are enabled and etc….. how to do
that?
 
W

Wayne-I-M

Hi Jon

Open the sub form in design view and then right click the text box (text1)
to open the properties box
Set the Enabled row to No
Do the same for all the other boxes
Save and close

Open the main form in design view - and then right click the Code box to
open the properties box
Add this to the AfterUpdate (just to test the code) - you can change the
event later.

Private Sub Code_AfterUpdate()
If Me.Code = 1 Then
Forms![MainForm]![SubForm].Form![Text1].Enabled = True
Forms![MainForm]![SubForm].Form![Text2].Enabled = False
Forms![MainForm]![SubForm].Form![Text3].Enabled = False
Forms![MainForm]![SubForm].Form![Text4].Enabled = False
Forms![MainForm]![SubForm].Form![Text5].Enabled = False
ElseIf Me.Code = 2 Then
Forms![MainForm]![SubForm].Form![Text2].Enabled = True
Forms![MainForm]![SubForm].Form![Text1].Enabled = False
Forms![MainForm]![SubForm].Form![Text3].Enabled = False
Forms![MainForm]![SubForm].Form![Text4].Enabled = False
Forms![MainForm]![SubForm].Form![Text5].Enabled = False
ElseIf Me.Code = 3 Then
Forms![MainForm]![SubForm].Form![Text3].Enabled = True
Forms![MainForm]![SubForm].Form![Text1].Enabled = False
Forms![MainForm]![SubForm].Form![Text2].Enabled = False
Forms![MainForm]![SubForm].Form![Text4].Enabled = False
Forms![MainForm]![SubForm].Form![Text5].Enabled = False
ElseIf Me.Code = 4 Then
Forms![MainForm]![SubForm].Form![Text4].Enabled = True
Forms![MainForm]![SubForm].Form![Text1].Enabled = False
Forms![MainForm]![SubForm].Form![Text2].Enabled = False
Forms![MainForm]![SubForm].Form![Text3].Enabled = False
Forms![MainForm]![SubForm].Form![Text5].Enabled = False
ElseIf Me.Code = 5 Then
Forms![MainForm]![SubForm].Form![Text5].Enabled = True
Forms![MainForm]![SubForm].Form![Text1].Enabled = False
Forms![MainForm]![SubForm].Form![Text2].Enabled = False
Forms![MainForm]![SubForm].Form![Text3].Enabled = False
Forms![MainForm]![SubForm].Form![Text5].Enabled = False
End If
End Sub

You do need to add all the lines so that if you change the number in the
Code:
 then you will (I assume) want the other text box to become disabled.

I am not sure if the word "Code" is restricted and it is not really a good
idea to call your text boxes 1 2 3 etc.  Give them a name that means something

Good luck
 
W

Wayne-I-M

add this
Forms![MainForm]![SubForm].Form![Text1].SetFocus
to the 3rd line in each section or you will get an erorr as you can't move
the focus from a disabled control.

ooops - sorry I forgot that bit

so it will look like


Forms![MainForm]![SubForm].Form![Text1].Enabled = True

Forms![MainForm]![SubForm].Form![Text1].SetFocus

Forms![MainForm]![SubForm].Form![Text2].Enabled = False
Forms![MainForm]![SubForm].Form![Text3].Enabled = False
Forms![MainForm]![SubForm].Form![Text4].Enabled = False
Forms![MainForm]![SubForm].Form![Text5].Enabled = False


etc
etc
etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top