Hide Control

Gismo

Registered User.
Local time
Today, 02:09
Joined
Jun 12, 2017
Messages
1,298
Hi,

How do I hide a control in a sub form if a value in another sub form does not meet the criteria?

when a customer is selected and the country does not match a local state, the control in a other sub form should be hidden, if local, certain fields must be completed
 
in the ON CURRENT event of the form, make control visible:

me.control.visible = me.state = me.country

you can use the FULL path of the controls if you are using items in the master form and the sub form

forms!fMasterForm.control.visible = forms!fMasterForm.subform.form.state = forms!fMasterForm.country
 
I tried using below code but displays can not find reference 'CSFN' which is my primary form

Private Sub Form_Current()
If [Forms]![CSFN]![country] = "ZA" Then
Me.DCAC.Visible = True
Else
Me.DCAC.Visible = False
End If
End Sub
 
Hi all,

Still can not get this one going.
The value in the first sub form will determine weather a control in the second sub form will be visible or not. somehow my code above gives me errors. I don't have any link master or child fields as the second subform query validates from a value in another form where the registration was selected from a dropdown box. the control to be hidden is a option box. Any suggestions?
 
It isn't clear, at least to me, whether you are referring to a control called Country or a field called Country. Also whether it is in the main form or another subform on the same form.

You could assign a string variable such as strCountry to the selected country code and then use
Code:
If strCountry="ZA" Then
 
ok I lied :) it now hides my control weather it is "ZA" or not. Think I am issing something here. Control is set as visible
 
Have you tried the obvious - set it as hidden by default.
 
I have indeed, when set as hidden, it still displays weather "ZA" or not
 
Private Sub Form_Current()
If strCountry = "ZA" Then
Me.AHGApproval.Visible = False
Else
Me.AHGApproval.Visible = True
End If
End Sub
 
Different control listed to an earlier post....
Also previously you said the other control was always hidden, now another control is always visible!

There is no obvious reason why its failing.
Either there is something else on your form/subform which is controlling this behaviour ... or you have omitted something relevant from your explanation.

Check whether the control or controls are being set visible by other code elsewhere on your form.
Also check the Form_Current code is actually running by adding a breakpoint on both lines with '.visible = '
 
yes I have more than one control on the sub form, I tried using a different control to test the water :)

Should I be referencing to the first subform instead of just using strCountry ? how would the DB know where to look for the ZA ?
 
Did you check both of the points I raised in my last reply? If not do so.

As I said back in post #5,
It isn't clear, at least to me, whether you are referring to a control called Country or a field called Country. Also whether it is in the main form or another subform on the same form.

Suggest you look at how to reference controls in forms & subforms here:http://allenbrowne.com/casu-04.html

If the code is run from a subform which contains the control you want to hide & the related control is in the parent form use something like

Code:
If Parent.Country = "ZA" Then

If the related Country control is in another subform on the same form, use

Code:
If Parent.OtherSubform.Form.Country = "ZA" Then
replacing the OtherSubform part with the name of the other subform control on the main form

If Country is a FIELD name rather than a CONTROL name, none of this will work
 
You can catch the events of the subform and handle them in your parent (main) form using withevents

Code:
'IN THE PARENT FORM

' declare subforms
Private WithEvents subfrm1 As Form
Private subfrm2 As Form

'Withevents allows you to catch events raised by the subform
'The subform must have the 'hasmodule' property set to 'yes'

Private Sub Form_Load()
    Set subfrm1 = CSFN.Form '<-- form that uses oncurrent event
    Set subfrm2 = DCAC.Form '<-- form that you're hiding controls on
	
	'ensure required events are raised
    subfrm1.oncurrent   = "[Event Procedure]"
    subfrm1.AfterUpdate = "[Event Procedure]"
End Sub

Private Sub subfrm1_Current()
   'ensure a different control has focus
    subfrm2("command1").SetFocus
   'set visibility
    subfrm2("dcac").Visible = Nz(subfrm1("country"), "") <> "za"
End Sub

Private Sub subfrm1_AfterUpdate()
    'if value is updated call oncurrent code again
    subfrm1_Current
End Sub
 
Country is a field name in the first subform - CustomerSubForm-New
AHGApproval is a yes/no option box in the second Subform - SparesSubForm
 
Thank you Static, but your code is high grade to me :)
 
I'll leave static to explain his own code....

You still haven't responded to these 2 points which I asked you to check in post #11:
Check whether the control or controls are being set visible by other code elsewhere on your form.
Also check the Form_Current code is actually running by adding a breakpoint on both lines with '.visible = '

Until you do that, all I can do is make hopefully inspired guesses ....

From your last answer:
Country is a field name in the first subform - CustomerSubForm-New
AHGApproval is a yes/no option box in the second Subform - SparesSubForm

Do you have a bound control called Country on the form.
If not add it now - the code is looked for a control not a field

When you tried the variable strCountry, you need to define it as a public or global variable so it can be used in more than one place?

Or if using the subform names approach, try

Code:
If Parent.CustomerSubForm-New.Form.Country = "ZA" Then
 
A subform is not a form. It is a control that contains a form.

Most controls have events that you can use to run code when something specific happens.
Subform controls have those events, but you aren't interested in subform events, you are interested in form events.

Whenever you want direct access to some object that isn't part of a form, you create a variable.

By declaring a variable for a subform's form 'withevents', that variable would appear in the 'Object' selector dropdown in the current form's VBA for you to write event code for.

And hence you have direct access to parent and child(/sub) form events in one place.
 

Users who are viewing this thread

Back
Top Bottom