Support in making my visible fields visible (1 Viewer)

Charlie100

New member
Local time
Today, 10:34
Joined
Nov 2, 2021
Messages
12
My access form has a series of fields which only appear on a certain criteria being input, normally yes or no. The code has been keyed in the build event menu and working perfectly for a new form.

When I try and open an existing form with the data contained, the fields which I expected to be visible are hidden, although once you say select yes in the field, the additional fields automatically appear with all the previous data displayed. Any ideas?

These are combi boxes with my inputs as

Private Sub fieldname1_AfterUpdate ()
If fieldname1.value="No" Then
Fieldname2.visible=false
Else
Fieldname2.visible=True
End if
End sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:34
Joined
May 7, 2009
Messages
19,175
add code also to the Form's Current Event:

private sub Form_Current()
Call fieldname1_AfterUpdate
end sub
 

Charlie100

New member
Local time
Today, 10:34
Joined
Nov 2, 2021
Messages
12
add code also to the Form's Current Event:

private sub Form_Current()
Call fieldname1_AfterUpdate
end sub
Excellent. Thank you.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:34
Joined
Feb 19, 2002
Messages
42,981
Although calling the _AfterUpdate event is not wrong, I would not do it. I would create a new sub that handles just the hide/unhide functionality and call it from both of the events. This is a little cleaner because it isolates the code so that it will not be impacted if you add other code to the event procedures.

Programmers have an expectation regarding event procedure code and that expectation is that the ONLY time that code will be executed is when the event happens and calling the code from a different event breaks that contract. Probably nothing will ever go wrong but using defensive programming techniques is always good practice especially when it takes less than a minute to accomplish:)
 
Last edited:

Charlie100

New member
Local time
Today, 10:34
Joined
Nov 2, 2021
Messages
12
Although calling the _AfterUpdate event is not wrong, I would not do it. I would create a new sub that handles just the hide/unhide functionality and call it from both of the events. This is a little cleaner because it isolates the code so that it will not be impacted if you add other code to the event procedures.

Programmers have an expectation regarding event procedure code and that expectation is that the ONLY time that code will be executed is when the event happens and calling the code from a different event breaks that contract. Probably nothing will ever go wrong but using defensive programming techniques is always good practice especially when it takes less than a minute to accomplish:)
 

Charlie100

New member
Local time
Today, 10:34
Joined
Nov 2, 2021
Messages
12
Hi Pat, really appreciate your reply. Are you able to share the code which you're suggesting that I should use?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:34
Joined
Feb 19, 2002
Messages
42,981
I don't have any code that makes your controls visible or invisible. I suggested that YOU copy the code you are using to do that from the event procedure to a sub form's class module Then from the two events you use:

call FormatVisible()

Or whatever you chose to name the procedure.

Code:
Private Sub fieldname1_AfterUpdate ()
    Call FormatVisible
End sub
Sub Public FormatVisible()
If Me.fieldname1="No" Then
    Me.Fieldname2.visible=false
Else
    Me.Fieldname2.visible=True
End if
End Sub
 

Charlie100

New member
Local time
Today, 10:34
Joined
Nov 2, 2021
Messages
12
The code provided works perfectly on a single group of linked fields, however I have approximately five separate fields which have the visible (true/false) field codes. An error is displayed on form load if you try to apply to more than one as separate codes.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:34
Joined
Feb 19, 2002
Messages
42,981
It is hard enough to debug without a database. Without even seeing the code it is just not possible:(
 

Charlie100

New member
Local time
Today, 10:34
Joined
Nov 2, 2021
Messages
12
I managed to sort it.

private sub Form_Current()
Call fieldname1_AfterUpdate
Call fieldname2_AfterUpdate
Call fieldname3_AfterUpdate

Worked perfectly.👍🤞
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:34
Joined
Feb 19, 2002
Messages
42,981
So, you ignored my advice about not calling the event directly? Who were you thanking then?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:34
Joined
May 7, 2009
Messages
19,175
who else.. take it!
 

Users who are viewing this thread

Top Bottom