hide fields based on yes/no fields

strawberry

Registered User.
Local time
Today, 08:55
Joined
Apr 1, 2008
Messages
43
I have a form and would like to hide some of the fields based on other fields being answered.

For example some of fields on form :

1) is pump to be repaired by woking - yes/no box

2) is pump to be repaired by distributor - yes/no box

3) date sent to manufacturer

4) date repair report received from manufacturer

5) target date for woking to repair

6) date repair report completed by woking

so if answer 1 is yes then i want to hide fields 2, 3 and 4 and if answer 2 is yes then hide fields 5 and 6.

or the other option would be that if answer 1 is yes then when the user hits return it takes them to the next relevant question to answer ie. 5

hopefully this explains it clearly enough

i look forwards to receiving your help
Many thanks
Jane
 
You would use the OnClick Event of the first checkbox

Code:
'/By default make all questions invisible

Me.Q1.Visible = False
Me.Q2.Visible = False
etc

If Me.Check1 = True then
   '/Turn on the appropriate controls
   me.Q3.visible = True
   etc
Else
   '/Turn on the appropriate controls
   me.Q1.visible = True
   etc
End If
 
thanks for your reply.

i have put the following code in the on click event for the first yes/no question and it looks as follows but its not working


Private Sub RepairByWoking_Click()

'/By default make all questions invisible

Me.RepairByManufacturer.Visible = False
Me.DateSentToManu.Visible = False
Me.TargetDateForWokingToInspect.Visible = False

If Me.RepairByWoking = True Then

'/Turn on the appropriate controls

Me.TargetDateForWokingToInspect.Visible = True

Else
'/Turn on the appropriate controls

Me.DateSentToManu.Visible = True
Me.RepairByManufacturer.Visible = True

End If
End Sub


Please can you advise what I have done wrong

Many thanks
Jane
 
Whe you say it is not working, what is not working?
 
i put in the code and when i entered a record to test it, I ticked the yes box for the first question and i assumed some of the questions would then appear. All the questions appear as soon as you open the form. sorry for not being clearer
 
Set the default visible property to False or all the controls when the form opens.
 
Hi David, thanks again for your prompt reply. How do i do this, sorry not very good on access!
 
Go to each of the controls and bring up their properties

Visible False
 
I have done this for the following fields (i assume its only these ones that I hide)

RepairByManufacturer
DateSentToManu
TargetDateForWokingToInspect

when i opened the form they were not visible but when i ticked the yes/no box, still none of them appeared.

I dont know if this has anything to do with it but when i went into the properties, and changed the visible to false, i only had a yes/no option so i choose no
 
Put a breakpoint on your on click event and step though the code.
 
I knew you would ask that.

Go to the VBA section of the checkbox to the OnClick Event()

Next click on the extreme left gray vertical strip see image

attachment.php


Then when you click on the checkbox press the F8 function key to step through the code to see whats is or is not happening.
 

Attachments

  • breakpoint.JPG
    breakpoint.JPG
    16.3 KB · Views: 737
Nearly working but this is what it is doing now.

firstly here is the code:

Private Sub RepairByWoking_Click()
'/By default make all questions invisible

Me.DateSentToManu.Visible = False
Me.TargetDateForWokingToInspect.Visible = False
Me.DateRepairReportRecdByManu.Visible = False
Me.DateRepairReportCompleted.Visible = False
If Me.RepairByWoking = True Then
'/Turn on the appropriate controls
Me.TargetDateForWokingToInspect.Visible = True
Me.DateRepairReportCompleted.Visible = True

Else
'/Turn on the appropriate controls
Me.DateSentToManu.Visible = True
Me.DateRepairReportRecdByManu.Visible = True

End If
End Sub


When u open the form firstly all 4 fields are hidden and check the yes box for 'repair by Woking' the correct two fields then appear.

if u open the form and check the yes box for 'repair by manu' the relevent two hidden fields do not appear but then i played around with it and found that if i click the 'repair by woking' box and then unclick it, the relevant fields then appear (for the 'repair by manu') and stay there even when i have clicked on 'repair by manu' so nearly i think

what i need to do now please

hope this makes sense
 
Hi David, did u get a chance to look at my last message, nearly working, just needs a tweak somewhere but i have no idea what i need to do so if you help me this would be much appreciated

Jane
 
what you need then is to set the

"repair by woking" button to turn off the "repair by manf" as well, and vice versa

HOWEVER Instead of this, you could probably consider it this way

a) just have 3 fields (or more depending what info you need to store)
1 - yesno field - "repair_by_manufacturer"
2 - date field - date_sent
3. - text field - report_details

now all you show on your form is these 3 fields, and the user can just click the yesno field as required.

so, taking this further - instead of a yesno field (and if you contemplated more depots) - you could have a combo box, (or an option group) offering a choice of

"repair by woking"
"repair by manufacturer"
"repair by maidstone" (say)

which gives you flexibility to add more depots in the future without needing to change your design. This is the usual way of doing stuff like this.
 
Thank u for your reply. Have done what you suggested and now have a combo box to choose either 'woking' or 'manufacturer'

here is the code i have put in the on click property and is working perfectly on my form.


Private Sub RepairBy_Click()
'/By default make all questions invisible

Me.DateSentToManu.Visible = False
Me.TargetDateForWokingToInspect.Visible = False
Me.DateRepairReportRecdByManu.Visible = False
Me.DateRepairReportCompleted.Visible = False

If Me.RepairBy = "Woking" Then
'/Turn on the appropriate controls
Me.TargetDateForWokingToInspect.Visible = True
Me.DateRepairReportCompleted.Visible = True


Else
'/Turn on the appropriate controls
Me.DateSentToManu.Visible = True
Me.DateRepairReportRecdByManu.Visible = True

End If
End Sub



I now also have a form which i created so that someone can update an existing record. I have put the above code in again in the hope that when i opened an existing record, it would only show the 2 boxes relevant to the answer in 'repair by'. it didnt work so i thought i would put the code in 'on got focus' property as opposed to 'on click'.

It worked a little better but not correctly. It showed the 2 correct questions for 'Woking' option and hid the other 2 but when i opened a record which had 'manufacturer' option already chosen, it showed the 2 questions for the 'woking' option and hid the other 2 for the 'manufacturer' option when i opened the record,

hope this makes sense and that you can help

many thanks
 

Users who are viewing this thread

Back
Top Bottom