Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enabling controls based on two other controls

Status
Not open for further replies.

vangx222

Technical User
Jun 6, 2007
36
0
0
US
Hi. I hope this is an easy one, I just can't figure out how to do it.

I have four controls that are used for entering text. TWO of the controls [controlA and controlB] are kept visible and TWO of the others are not. What I want to happen is when I type in data for EITHER controlA OR controlB, the other two controls [controlC and controlD] would then appear. I understand that if I only had one control visible (let's say I didn't have controlB) the code would be like this

Code:
Private Sub controlA_Change()
If Me.controlA.Value = "" Then
    Me.controlC.Enabled = False
    Me.controlD.Enabled = False
   Else
    Me.controlC.Enabled = True
    Me.controlD.Enabled = True
End If
End Sub

in my case.. I need it to work if either controlA OR controlB has data in it. It's not going to ALWAYS be JUST controlA or JUST controlB.. it's either one, or the other, or both.

Thanks in advance.
 
If Me.controlA.Value = "" Or Me.controlB.Value = "" Then

John
 
and this would go in the On Change event of controlA?

or controlB? or it doens't matter?
 
Ok, here's what I tried..
Code:
Private Sub controlA_Change()
[b]If Me.controlA.Value = "" Or Me.controlB.Value = "" Then[/b] 
    Me.controlC.Enabled = False
    Me.controlD.Enabled = False
   Else
    Me.controlC.Enabled = True
    Me.controlD.Enabled = True
End If
End Sub

on the On Change event of only controlA first... the action I wanted only worked when I entered in a value for controlA and did nothing (did not enable controlC and D) when I entered in a value only for controlB.

then,
I ALSO put the same code into the On Change event of controlB. Now there's is currently the same code in both controlA and B.

This is how it works. It works perfectly fine for controlA.
.
.
for controlB, when I enter in a value, controlC and D does enable as well, HOWEVER... when I close the form and reopen the form, controlC and D is disabled again (even when I have data in them).
 
Your code has " ", it should be
 
I forgot to add. If you don't want the same code in both controls, you can create a common procedure. In the code window, click on insert/new procedure. Then put in your code. You will be asked for a name of the new procedure, give it a name. Then in your change events of both controls just put in the procedure name you have just done. This means if either control changes it will go to the common event procedure you just created.
 
Your code has " ", it should be ""
It looks like it's " " but it's really ""

Well the code that jrbarnett offered didn't work. So you're saying, put
Code:
If Me.controlA.Value = "" Or Me.controlB.Value = "" Then 
    Me.controlC.Enabled = False
    Me.controlD.Enabled = False
   Else
    Me.controlC.Enabled = True
    Me.controlD.Enabled = True
End If
in a new created procedure (let's say named NewProcedure)?

and then in controlA On Change event call up that created procedure [NewProcedure]?

How would I do this? What would be the code that goes into these sections be?

Code:
Private Sub controlA_Change()


End Sub

and

Private Sub controlb_Change()

End Sub
 
Your code has " ", it should be ""
It looks like it's " " but it's really ""

Well the code that jrbarnett offered didn't work. So you're saying, put
Code:
If Me.controlA.Value = "" Or Me.controlB.Value = "" Then 
    Me.controlC.Enabled = False
    Me.controlD.Enabled = False
   Else
    Me.controlC.Enabled = True
    Me.controlD.Enabled = True
End If
in a new created procedure (let's say named NewProcedure)?

and then in controlA On Change event call up that created procedure [NewProcedure]?

How would I do this? What would be the code that goes into these sections be?

Code:
Private Sub controlA_Change()


End Sub

and

Private Sub controlb_Change()

End Sub
 
I will go back to the beginning. You say you have four controls, two are visible, and two are not. However your code refers to making the two hidden ones enabled?. Is that what you want?, if not change the enabled to visible.

In design mode of your form, click on view/code.
Then select Insert/New procedure. For the name it asks for use Test.

The code window for Test will then open. In this sub, type in

If Me.controlA.Value = "" Or Me.controlB.Value = "" Then
Me.controlC.Enabled = False
Me.controlD.Enabled = False
Else
Me.controlC.Enabled = True
Me.controlD.Enabled = True
End If

Then go into the change events of controlA and controlB.

Private Sub controlA_Change()
Test
End Sub

Private Sub controlb_Change()
Test
End Sub

Now try it.

The procedure you are creating will only do something if you try to put something in either text box, as the change event only fires if you try to enter data in either text boxes.

 
I will go back to the beginning. You say you have four controls, two are visible, and two are not. However your code refers to making the two hidden ones enabled?. Is that what you want?, if not change the enabled to visible.

oops I meant to say enable and disable... not visible. =)



Thank you so much for your help.

AGAIN, it works perfectly fine when I enter a value into controlA but does not work the way I want it to when you enter a value into controlB.

The problem with controlB is that when I enter a value into it, both controlC and controlD are enabled (like I want) BUT if I click out of the form and come back to it later, the two control C and D are then disabled again (unless I try deleting the value currently in controlB and reentering it) even when there is data in the control [C and D].

BUT, I guess the code you both shared with me can do for now. If you or anyone else knows something that will solve this problem then please share.

Again, THANKS SO MUCH!
 
Go into the OnOpen event of the form. Put in the word Test.

This would then evaluate whether both textboxes have text in when the form opens.

 
ZOR, I tried what you suggested and it still didn't work for me.

For anyone else with the same question as mine, I FOUND A SOLUTION with the help of someone else! Yahooo! =)

for future reference,


If anything is entered into A or B then C & D is visible
Code:
Private Sub ControlA_Change()
If Len(ControlA.Text) > 0 Then
 ControlC.Visible = True
 ControlD.Visible = True
End If
End Sub

Private Sub ControlB_Change()
If Len(ControlB.Text) > 0 Then
 ControlC.Visible = True
 ControlD.Visible = True
End If
End Sub


If anything is entered into A or B and then deleted, C & D is not visible
Code:
Private Sub ControlA_Exit(Cancel As Integer)
If Not IsNull(ControlA) Or Not IsNull(ControlB) Then
 ControlC.Visible = True
 ControlD.Visible = True
Else
 ControlC.Visible = False
 ControlD.Visible = False
End If
End Sub

Private Sub ControlB_Exit(Cancel As Integer)
If Not IsNull(ControlA) Or Not IsNull(ControlB) Then
 ControlC.Visible = True
 ControlD.Visible = True
Else
 ControlC.Visible = False
 ControlD.Visible = False
End If
End Sub


If, when the user moves onto an existing record where A or B has a value then C & D is visible
Code:
Private Sub Form_Current()
If Not IsNull(ControlA) Or Not IsNull(ControlB) Then
 ControlC.Visible = True
 ControlD.Visible = True
Else
 ControlC.Visible = False
 ControlD.Visible = False
End If
End Sub
 
Glad you got there. You should also look at the use of trim, in case a user simply presses his spacebar and puts in a space.
 
I actually thought of that, Zor, but while pressing the spacebar would cause the controls C nad D to become visible, if nothing else is entered in the field, the field Exit event makes them invisble again. Just didn't seem like it was worth adding to the code, especially for a relative newbie!

Linq

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Thanks, and you are correct. Just thought I would mention it as I am working on a database at the moment where nobody did anything, and there are hundreds of records where the data enterer thought nothing of putting in a space before the data. Best regards
 
Don't you just love cleaning out somebody else's litter box?

Have a good weekend!

Linq

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
No 'If' needed...

Me.ControlC.Visible = Len(Trim(Nz(Me.ControlA,""))) + Len(Trim(Nz(Me.ControlB,"")))
Me.ControlD.Visible = Me.ControlC.Visible

Change, AfterUpdate, KeyUp, whatever event you like... Just test them all to see which is best for your situation.


HTH

[pipe]
Daniel Vlas
Systems Consultant

 
Vangx222 did say:
oops I meant to say enable and disable... not visible. =)

However I think he's been drinking too much weedkiller?

Probably does'nt know we are all still talking about him!!

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top