We help IT Professionals succeed at work.

restore dependent control enabled state on undo

Adrian Simmons
Last Modified: 2017-04-01
I have a sub form that has "Data Entry" set to yes and is purely used for adding log records that are displayed using another sub form which are both used in the same parent.

On the data entry sub form I have a combobox and a textbox, depending on what is chosen in the combobox the textbox is enabled or disabled. This works perfectly by looking up the .Column(3) value in the combobox AfterUpdate event. When the form is opened I call the AfterUpdate event to initially set the textbox and this works too because the combobox inherits and displays a default value from the underlying field of the query used for the combobox rowsource.

Now if a user presses escape and undoes the form I need to enable/disable the textbox given the default value inherited, here comes the twist though, because the default value is inherited the combobox .DefaultValue property is empty!

What can I use in order to set the textbox correctly when the form is undone?
Watch Question

Top Expert 2016

1. set the keypreview property of the subform to YES
2. place this code in the KeyDown event of the form, the codes is just the basic you need which you can modify accordingly

change combo0 with the actual name of the combo box

 private sub form_keydown(KeyCode As Integer, Shift As Integer)
 If KeyCode = 27 Then
     Me.Combo0 = Me.Combo0.ItemData(0)    'set value of the combo box- .ItemData(0) is selecting the first value, 
                                                                              'change 0 to 1 or 2 depending on which row select the enabled property of the controls
End If
end sub

Open in new window


So I don't need to use the Form_Undo event?

The code you posted will grab the value after the undo has happened and thus it will be the default value in the combobox?

I thought ItemData() got a particular row from the list in the combobox? I won't know what that will be if the default value for the field is changed, it needs to be worked out.

I'll give it a try to experiment and see what it does and report back.

Thanks! :)


Me.Combo0 = Me.Combo0.ItemData(0) achieves the same result in the subforms Form_Undo event without having to use KeyPreview = Yes and the Form_KeyDown event.

Yes I was right ItemData(#) gets what ever row you supply a number for, although I know what that is now imagine my client asks me to change the default in a 100 long list and I just go into the table and modify the field to use the ID they want to default, the combobox will show the correct item when the form is loaded but the logic in the form will no longer be correct!

There is no way a month or two down the line that I will remember that I need to make a change to the front end whereas I will just go into the back end and make the field change. I'd prefer to get something in place that won't require a new front end pushing out.

Am I after the impossible? I think not because surely I could actually read the table definition during the Form_Undo event to find out the ID and then cycle through every list item looking for it. I just thought there would be an easier way!
Top Expert 2016
Unlock this solution and get a sample of our free trial.
(No credit card required)


Doh! Lol, wow I must be tired, yes a nice simple solution to the problem.

I think that tempvars is part of macros, I've not come across it in vba but yes I'll just make a form level variable and store it on form load.



Thanks very much Rey, I'm putting my dunce cap over my eyes and getting some sleep!
Top Expert 2016

you are welcome!!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.