Enabling controls

vX987

Registered User.
Local time
Yesterday, 23:11
Joined
Jun 29, 2007
Messages
51
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.
 
Would something like this work?
Code:
Private Sub controlA_Change()
       Change_Check
End Sub

Private Sub controlB_Change()
       Change_Check
End Sub

Sub Change_Check
       If Me!controlA.Value <> "" Then
            Me!controlC.Enabled = True
            Me!controlD.Enabled = True
       ElseIf Me!ControlB.Value <> "" Then
            Me!controlC.Enabled = True
            Me!controlD.Enabled = True
       Else
            Me.controlC.Enabled = False
            Me.controlD.Enabled = False
       End If
End Sub
 
I'm not sure that the Value property of the control has changed yet during the Change event. You may need to reference the Text property of the active control.
Code:
Private Sub controlA_Change()
  Change_Check Len(me.controlA.Text) > 0 OR Len(me.controlB) > 0
End Sub

Private Sub controlB_Change()
  Change_Check Len(me.controlA) > 0 OR Len(me.controlB.Text) > 0
End Sub

Sub Change_Check(Enable as boolean)
  Me.controlC.Enabled = Enable
  Me.controlD.Enabled = Enable
End Sub
 
I'm not sure that the Value property of the control has changed yet during the Change event. You may need to reference the Text property of the active control.

Fair point. Wouldn't you still need to include the possibility that both fields are blank and, if so, disable the controls?
 
Thank you soo much for your assistance. However, here's what happened... Alc, your method didn't work. It says...
Code:
Sub Change_Check
       If Me!controlA.Value <> "" Then
            Me!controlC.Enabled = True
            Me!controlD.Enabled = True
       [b]ElseIf Me!ControlB.Value <> "" Then[/b]
            Me!controlC.Enabled = True
            Me!controlD.Enabled = True
       Else
            Me.controlC.Enabled = False
            Me.controlD.Enabled = False
       End If
End Sub
the line I bolded must be the first line.

lagbolt, your method brings me this error message:

You can't reference a property or method for a control unless the control has the focus.
 
the line I bolded must be the first line.
Sorry about that. Not sure what you mean by the above, though.
What happens when you run it? Does it error, or just not work? If it errors, what's the message?

Does this work
Code:
Sub Change_Check
       'Assume that neither A or B contain a value and disable both C & D
       '-----------------------------------------------------------------
       Me.controlC.Enabled = False
       Me.controlD.Enabled = False

       'If A has a value, enable both C & D
       '-----------------------------------
       If Me!controlA.Value <> "" Then
            Me!controlC.Enabled = True
            Me!controlD.Enabled = True
       End if

       'If B has a value, enable both C & D
       '-----------------------------------
       If Me!ControlB.Value <> "" Then
            Me!controlC.Enabled = True
            Me!controlD.Enabled = True
       End If
End Sub
 
Alc: If both fields are blank the following evaluates to false, which disables the control
Code:
Len(me.controlA) > 0 OR Len(me.controlB.Text) > 0
987: Are you sure you only reference the Text property of controlA in the change event of controlA, and you only reference the Text property of controlB in the change event of controlB? You can only reference the Text property of a control if it has the focus. This will be the case during the change event of the control.
Code:
Private Sub controlA_Change()
  Change_Check Len(me.controlA.[COLOR="Red"]Text[/COLOR]) > 0 OR Len(me.controlB) > 0
End Sub

Private Sub controlB_Change()
  Change_Check Len(me.controlA) > 0 OR Len(me.controlB.[COLOR="Red"]Text[/COLOR]) > 0
End Sub
 
Thanks for the explanation. A bit beyond what I've used before, so I'll have to remember that one.
 
ok, lagbolt I rechecked it and I did missspelled the control so I fixed it and re did the code, but now I get this:

"Wrong number of arguments or invalid property assignment."

Alc, there's no error message, it just doesn't work. BUT oddly.. it's works in a weird way.
.
.
When I enter in a value for controlA or controlB, the other two control C and D does NOT enable... however, IF I enter in a value for JUST controlB and then DELETE the value, somehow control C and D appears (but it's not the same case for controlA).
 
And the routine you call looks like this?
Code:
Sub Change_Check(Enable as boolean)
  Me.controlC.Enabled = Enable
  Me.controlD.Enabled = Enable
End Sub
 
Yes, where does the Sub change_check control come from? Do I need to create this as a new procedure? Or can I just copy it and paste in directly onto the code window?
 
This is like an Abbott and Costello routine; "Who's on first?" I think this covers all the possibilities.

General logic:
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
Linq
 
I believe this is the leanest suite of functions that'll solve the problem.
Code:
Private Sub ControlA_Change()
  EnableCD Me.ControlA.Text <> "" Or Nz(Me.ControlB, "") <> ""
End Sub

Private Sub ControlB_Change()
  EnableCD Nz(Me.ControlA, "") <> "" Or Me.ControlB.Text <> ""
End Sub

Private Sub Form_Current()
  EnableCD Nz(Me.ControlA, "") <> "" Or Nz(Me.ControlB, "") <> ""
End Sub

Private Sub EnableCD(Enable As Boolean)
   Me.ControlC.Enabled = Enable
   Me.ControlD.Enabled = Enable
End Sub
The Len() function fails if a value is null.
 
lagbolt, it's either I am not inserting your codes right or maybe I just don't know how to do it, but I've tried erasing all the codes and rewriting them over and over and it still doesn't work.

linq, your code works PERFECT =) exactly what I wanted. thank you!!!
 
Actually, lagbolt, the If Len(ControlA.Text) > 0 Then lines are superfluous; if the Change event has been tripped the Len of .text will always be greater than 0! These two subs could be shortened to

Code:
Actually the Private Sub ControlA_Change()
 ControlC.Visible = True
 ControlD.Visible = True
End Sub

Private Sub ControlB_Change()
 ControlC.Visible = True
 ControlD.Visible = True
End Sub
And vX987 is right; your code doesn't work. First, the OP wanted the controls C and D to be visible/invisble depending on A and B, not Enabled or Disabled. And to make the control enabled I believe

Me.ControlC.Enabled = Enable

needs to be

Me.ControlC.Enabled = True

Also, your Form_Current sub causes C & D to be visible on new record, when nothing has been entered.
 
Last edited:
Linq: I'm not using "IF" and the Text property of the control will have a zero length if the user deletes all the characters.
 

Users who are viewing this thread

Back
Top Bottom