hide controls when a form is opened conditionally (1 Viewer)

jd9913

Registered User.
Local time
Yesterday, 16:18
Joined
Sep 12, 2012
Messages
14
Hi,

I've searched for an answer here and can only find information setting the visible property when selections are made on an open form--I want to set the visible property when the form is opened depending on which avenue the user chooses from the navigation form.

I have a navigation form (Form A) and a second form with a subform (Form B). The user will choose from a combobox, either an existing record or a new record on Form A. On Form A there are two buttons, one that will take the user to the correct record on Form B for editing and one that will open to a new record on Form B. Once Form B is open, all controls will be blank (if a new record is chosen) or with certain controls prepopulated if an existing record is chosen.

What I want to do is to hide controls on the main form (not the subform) of Form B if the user chooses an existing record. Form A's buttons work correctly to open Form B right now. I want to be able to hide prepopulated controls on Form B if the user chooses an existing record from Form A.

Can someone assist me with the code for this? Here's the code I'm using to open Form B to an existing record. I'd like to set the visible property here if at all possible.

NOTE: "Form A" and "Form B" are not the actual names of my forms--it's just easier for illustration purposes here in my question.


Private Sub cmdAddLogEntry_Click()
DoCmd.OpenForm "Form B", acNormal, , "Activity_ID = " & Me.cboINum
DoCmd.Close acForm, "Form A", acSaveYes

End Sub

Thanks for any suggestions.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 09:18
Joined
Jan 20, 2009
Messages
12,852
You can't set properties of form controls before they open so the Visible property must be set after the form opens.

You could pass a flag in the OpenArgs argument of the OpenForm command then test the OpenArgs property of the form for the flag when it opens.

Another possibility would be to test the NewRecord property of the form that opens.
 

jd9913

Registered User.
Local time
Yesterday, 16:18
Joined
Sep 12, 2012
Messages
14
Thank you Galaxiom for your reply. Since I'm a beginner in VBA, I'm not sure how to implement your suggestions.

I've tried another approach based on some code I've found elsewhere on the site. I've set the visible property to false on the appropriate controls when the form opens. When the user wants to modify those controls (when choosing to add new record) then I'm trying to write the code to set that property to true. Essentially using an If/Else statement.

Here's the code I have so far. I'm not sure what I'm doing wrong here, but it's not changing the visible property on these two controls. I've tried placing the code into both the Form_open and the Form_current places and testing it in each. It doesn't work in either, the controls stay invisible. So I need some assistance in modifying it so that it works.

The incidentnumber control's value is passed from the navigation form and is only null when the user chooses to add a new record.

If Me.IncidentNumber = Null Then
Me.OpenDate.Visible = True
Me.OpenTime.Visible = True

Else
Me.OpenDate.Visible = False
Me.OpenTime.Visible = False

End If

Thanks.

Jennifer
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 09:18
Joined
Jan 20, 2009
Messages
12,852
The Equals operator doesn't wotk with Null. Even Null = Null returns False

Try this in the OnCurrent Event:

Code:
With Me
     .OpenDate.Visible = .NewRecord
     .OpenTime.Visible = .NewRecord
End With
 

jd9913

Registered User.
Local time
Yesterday, 16:18
Joined
Sep 12, 2012
Messages
14
That works beautifully. Thank you!
 

Users who are viewing this thread

Top Bottom