Changing Enable Property

  • Thread starter Thread starter Don Rountree
  • Start date Start date
D

Don Rountree

Help. I have 8 text boxes and 1 command button on a
form. I want the command button to disable if any of the
8 text boxes have data entered into them. I know how to
do this for one text box, but how do I write a global
code to make this happen for all of them. Any help would
be appreciated.

Don Rountree
 
Don,
You can put some kind of identifier (see below) in the Tag property of the
text boxes. Then do something like this:

*** air code ***
For each ctl in Me.Controls
if ctl.Tag="Include This One When Checking" then
ctl.enabled = (Nz(ctl.value) = "")
end if
Next
*** end code***

"Me" refers to the form you are looking at and Me.Controls is a collection
of all the controls on the form.

Hope that helps,
Mattias Jonsson
 
Mattias,

Thank you for your response. I think I understand the
code below, but how does it disable a command button when
I enter text in any one of eight text boxes? Where do I
refer to the command button? Sorry to be so dense.

Don Rountree
 
Don, OK that's a slightly different issue. I misread your question. In that
case, you can do something like this:

*** warning: more air code ***
Private Sub MyControlA_AfterUpdate()
EnableDisableButton
End Sub

Private Sub MyControlB_AfterUpdate()
EnableDisableButton
End Sub

'And so on...

Private Sub EnableDisableButton()
dim ysnEnable as boolean
ysnEnable=True
For each ctl in Me.Controls
if ctl.Tag="Include This One When Checking" then
ysnEnable = (Nz(ctl.value) = "")
exit for
end if
Next
cmdMyButton.Enabled = ysnEnable
End Sub
*** end code ***

This will not work if you type something a text box that is included when
checking and then click on the button. A workaround for that is to use the
Change event of the textbox to call EnableDisableButton but then it is
important to check the .Text property of the textbox that has focus instead
of the .Value property, or you will be checking its old value. Hope that
made sense.

Good luck,
Mattias
 
Sorry that loop was not great but I hope you get the idea. I should just
stay off the news groups while my work is too crazy rather than giving
people bad air code. / Mattias
 
Back
Top