MS Access Forum / Forms / May 2008
Hiding fields on a form
|
|
Thread rating:  |
KevinPreston - 06 May 2008 17:37 GMT Hi, i hope someone can help me, i would like to hide various fields under different conditions. i have combed other posts about the subject and they have help me greatly, i am about 75% there. But now i am stuck. I have 5 fields, one called tachotype, this tells me what type of tacho is in a vehicle, digital, analogue or none. One called tachocal, this tells me when the tacho was calibrated. One called CalDue, this tells me when the next calibration is due. One called tachotest, this tells me when the tacho had its 2yr test. One called 2yrDue, this tells me when the next 2yr test is due. When tachotype = "none" hide all four fields, this i can do. When tachotype = "analogue" show all four fields, this i can do. the problem is - When tachotype = "digital" i want to hide the 2yrtest & 2yrDue fields, this i can't do yet. An added complication is that a analogue tacho has a calibration every 6 years and a 2yr test evry 2yrs, a digital tacho is calibrated every 2 years. The CalDue & 2yrDue fields are calculated fields and when tachotype = "digital" i would like it to only calculate 2 years on. This is the code i have at the moment: Private Sub Form_Current() If Me.[tachotype] = "None" Then Me.[TACHOCAL].Visible = False Me.[TACHOTEST].Visible = False Me.[2yrDue].Visible = False Me.[Due_Cal].Visible = False Else
Me.[TACHOTEST].Visible = (Me.[tachotype] = "analogue") Me.[2yrDue].Visible = (Me.[tachotype] = "analogue") Me.[TACHOCAL].Visible = (Me.[tachotype] = "analogue") Me.[Due_Cal].Visible = (Me.[tachotype] = "analogue") End If
End Sub
I apologise for the longwindedness of the post but if i get it right now then i won't have to keep clarifying things. Hopefully someone can help.
BruceM - 06 May 2008 18:08 GMT What happens if tachotype is something other than "analogue" or "none"? There are several ways you could handle that within an If statement, but see below for another option.
I don't know if you are storing the calculation results in a table, but if so you shouldn't. Instead, calculate on the fly as needed. In a query you could have a calculated field by placing something like this at the top of a blank column in query design view: TwoYearDue: DateAdd("y",2,[tachocal])
Select Case may be easier to handle than an If statement, especially since your example does not include the "digital" option:
Select Case Me.Tachotype Case "None" Me.txtTachocal.Visible = False Case "Analogue" Me.txtSomething.Visible = True Me.txtSomethingElse.Visible = False Case "Digital" Me.txtSomething.Visible = False Me.txtSomethingElse.Visible = True End Select
You are actually hiding controls, not fields. It would be best if the controls and the fields have different names. I have used the prefix "txt" to indicate text boxes.
I have to say I am having a little trouble following just what needs to be visible when, but I hope this will give a general idea of how you can proceed.
> Hi, i hope someone can help me, i would like to hide various fields under > different conditions. [quoted text clipped - 42 lines] > i won't have to keep clarifying things. > Hopefully someone can help. Beetle - 06 May 2008 18:23 GMT You might try a Select Case statement instead. Someting like;
Private Sub Form_Current()
Select Case Me![tachotype] Case "None" Me.[TACHOCAL].Visible = False Me.[TACHOTEST].Visible = False Me.[2yrDue].Visible = False Me.[Due_Cal].Visible = False Case "analogue" Me.[TACHOCAL].Visible = True Me.[TACHOTEST].Visible = True Me.[2yrDue].Visible = True Me.[Due_Cal].Visible = True Me.[2yrDue] = DateAdd("yyyy", 2, [Tachotest]) Me.[Due_Cal] = DateAdd("yyyy", 6, [Due_Cal]) Case "digital" Me.[TACHOCAL].Visible = True Me.[TACHOTEST].Visible = False Me.[2yrDue].Visible = False Me.[Due_Cal].Visible = True Me.[Due_Cal] = DateAdd("yyyy", 2, [Due_Cal]) End Select
End Sub
BTW - 2yrDue and Due_Cal sould be unbound controls on your form (or maybe calulated fields in a query, if your form is based on a query) not fields in your table. Also, forms have "controls" (not fields), fields are in tables or queries.
 Signature _________
Sean Bailey
> Hi, i hope someone can help me, i would like to hide various fields under > different conditions. [quoted text clipped - 35 lines] > i won't have to keep clarifying things. > Hopefully someone can help. KevinPreston - 07 May 2008 10:01 GMT Thanks for your reply Beetle, i have tried your suggestion and it has worked perfectly apart from the code below, it is telling me that i can't assign a value to this control.
> Me.[2yrDue] = DateAdd("yyyy", 2, [Tachotest]) > Me.[Due_Cal] = DateAdd("yyyy", 6, [Due_Cal]) > Case "digital" > > Me.[Due_Cal] = DateAdd("yyyy", 2, [Due_Cal]) > End Select At the moment i calculate the due date with the same expression (if that's the right word) as yours but as the control source for [Due_Cal], all i need it to do is to change from 6 to 2 if tachotype = "Digital". Will i need to use a query and if so any suggestions?
Regards Kevin
BruceM - 07 May 2008 12:21 GMT I would think you need to use DateAdd on LastCal rather than Due_Cal. If Due_Cal is unbound I can't see how it would work to add time to whatever value it contains.
You could use a Control Source expression: =IIf([Tachotype] = "Digital",DateAdd("yyyy", 6, [Tachotest]),IIf([Tachotype] = "Analog",DateAdd("yyyy", 6, [Tachotest]),""))
It helps in replying if you include the code about which you are commenting. I'm getting a bit lost trying to flip back and forth between messages, so the field names may not be exact, but that's the general idea. You could use the same expression in a calculated query field. At the top of an empty column in query design view: NextTest: IIf([Tachotype] = "Digital", etc.
I think it's simpler to use code, but that's up to you.
> Thanks for your reply Beetle, i have tried your suggestion and it has > worked [quoted text clipped - 17 lines] > Regards > Kevin KevinPreston - 07 May 2008 17:38 GMT Bruce Hi sorry about not including the code. Due_Cal is an unbound control that gets its value from using DateAdd on TachoCal.
Thank you very much for your assistance, it has been of great help and my form is now doing what i wanted it to.
>I would think you need to use DateAdd on LastCal rather than Due_Cal. If >Due_Cal is unbound I can't see how it would work to add time to whatever [quoted text clipped - 18 lines] >> Regards >> Kevin Beetle - 07 May 2008 14:51 GMT Sorry, that was a typo error on my part. I used the the wrong control name in the DateAdd. It probably should be like this (if I have your control names right);
Me.[2yrDue] = DateAdd("yyyy", 2, [Tachotest]) Me.[Due_Cal] = DateAdd("yyyy", 6, [Tachocal]) Case "digital" Me.[Due_Cal] = DateAdd("yyyy", 2, [Tachocal]) End Select
If you decide to do it this way, then you need to remove anything that is in the control source of Due_Cal. Or you could use the method that Bruce suggested, using the IIF statemant as the control source of Due_Cal.
 Signature _________
Sean Bailey
> Thanks for your reply Beetle, i have tried your suggestion and it has worked > perfectly apart from the code below, it is telling me that i can't assign a [quoted text clipped - 14 lines] > Regards > Kevin KevinPreston - 07 May 2008 17:39 GMT Beetle Thank you very much for your assistance, it has been very helpful and my form now does what i wanted it to.
>Sorry, that was a typo error on my part. I used the the wrong control name >in the DateAdd. It probably should be like this (if I have your control [quoted text clipped - 15 lines] >> Regards >> Kevin Beetle - 07 May 2008 20:08 GMT Glad I could help :-)
 Signature _________
Sean Bailey
> Beetle > Thank you very much for your assistance, it has been very helpful and my form [quoted text clipped - 19 lines] > >> Regards > >> Kevin KevinPreston - 07 May 2008 09:19 GMT Hi Bruce thanks for reply, i will try your suggestions as soon as i can.
The 2yrDue & Due_Cal are both unbound controls onthe form and just show the dates due to remind me.
I have a table that stores vehicle details, i have decided to use one table for are types of vehicle, cars right up to 44ton trucks and trailers, hgv's have to have Tachographs fitted, cars, small vans and trailers don't, if i look at the details for a particular car i don't want tachograph details to show, new hgv's have to have digital tachos fitted and don't need the 2yr test showing as they are only calibrated every 2 years.
At the moment, if there is nothing in the tachotype field then i get an invalid use of null, however i am going through the table and entering the field, normally this will be done when a new vehicle is entered.
The code i have at the moment doesn't show anything about the digital side because i wasn't sure how to do it, hence the post.
I am fairly new to access and am learning as i go along, i have been developing this database for about 2 years and keep thinking of things for it to do for me.
Regards Kevin
|
|
|