Problem with hiding sub forms

cdoyle

Registered User.
Local time
Yesterday, 18:15
Joined
Jun 9, 2004
Messages
383
Hi,
I have a mainform with some sub forms, I want to hide the sub forms until a text box on the main form has been filled in.

So I've tried this on my main form

Code:
Private Sub Form_Current()
If Me.NewRecord Then
Me![frm_criteria2].Visible = False
Me![DX_subform].Visible = False
End If
End Sub

and then put this on the afterupdate of the field I wanted to be filled

Code:
Private Sub txt_Start_Date_AfterUpdate()
Me![frm_criteria2].Visible = True
Me![DX_subform].Visible = True
End Sub

But it's not working right, I already had some data in the table, and when it opens the form, the records that had data the sub forms display fine. I then create a new record, and the sub forms disappear just like they are suppose too. but when I put something in the txtbox, they don't reappear. And then if I go back a record (which displayed earlier), they still don't reappear.

So the after update event doesn't seem to be working? Anyone see what I'm doing wrong? Should I put that code in a different event?

Basically what I want it to do is, if txt_Start_Date is empty don't show the subforms, if there is something in that field display them.
 
The code Me.Refresh or Docmd.Requery in the AfterUpdate code may solve the problem.
 
The code Me.Refresh or Docmd.Requery in the AfterUpdate code may solve the problem.

Did you mean something like this,

Code:
Me.Refresh![frm_criteria2].Visible = True
Me.Refresh![DX_subform].Visible = True

I tried this, and it didn't work, but wasn't sure if that was what you meant.
 
I thought maybe this would make it work too.

Private Sub txt_Start_Date_AfterUpdate()
Me![frm_criteria2].Visible = True
Me![DX_subform].Visible = True
Me.Refresh

End Sub

but they still don't reappear.
 
I also tried it with DoCmd.requery, and that didn't work either.
It's really odd, if it's able to hide the forms, it should be able to unhide them. Right?

Private Sub txt_Start_Date_AfterUpdate()
Me![frm_criteria2].Visible = True
Me![DX_subform].Visible = True
DoCmd.Requery

End Sub
 
Yes it should; from a pure logic point of view - take this one step at a time.

In your On Current event put an ELSE in the IF statement with the 'Visible = True' options. Tht will just ensure that all options are covered.

I have just put two command buttons called Hide and Show on a form of mine and made the onclick event run your code.

Therefore it must be the AfterUpdate event that is wrong. Maybe onExit or OnLostFocus may be better. Try the two buttons first and see if the subform Hides and Shows. If that works then it just an event logic problem.
 
Last edited:
oops, I guess I didn't quite have this completed afterall.

I still have the problem if they click on the create new record (which makes the subforms dissappear), and then change their mind and go back to the previous record. The sub forms, don't reappear.

I'm thinking I need to put something on my first field of the main form, something like if txt_Start_Date has data, then it would do this

Me![frm_criteria2].Visible = True
Me![DX_subform].Visible = True

How would I write that?
 
Is there anything I can put on the form's onload, that says if txt_Start_Date is empty, then hide all the subforms. But if the field has data, then make the sub forms visible?
 
Your code belongs in the forms CurrentEvent, just call the current event in the after update event of the textbox
 
Your code belongs in the forms CurrentEvent, just call the current event in the after update event of the textbox

I'm not sure what you mean, which part of the code do I need to put in the form oncurrent? Did you mean the oncurrent of the main form, or each sub form?

And how do I call it from the afterupdate of the textbox?
 
Put all the code in the subforms Current_Event in the AfterUpdate event of the combo just put Call Form_Current
 
I'm sorry, but I guess I'm just not understanding what code I need to put on my subforms?

I currently have this on my main form

Code:
Private Sub Form_Current()
If Me.NewRecord Then
Me![frm_criteria2].Visible = False
Me![DX_subform].Visible = False
Me![frm_number_incidents].Visible = False
End If
End Sub


Private Sub txt_Start_Date_Exit(Cancel As Integer)
Me![frm_criteria2].Visible = True
Me![DX_subform].Visible = True
Me![frm_number_incidents].Visible = True
End Sub

This works OK, unless a person clicks the new record button, and then change their mind and goes back. The previous records remain hidden. They have to close the form, and reopen to see the records again.

What part of the code above should go into each sub forms on current? Right now I have 3 sub forms, so I do need to put the same code into each one?
 
Code:
Private Sub Form_Current()
If Me.txt_Start_Date & "" ="" Then
Me![frm_criteria2].Visible = False
Me![DX_subform].Visible = False
Me![frm_number_incidents].Visible = False
Else
Me![frm_criteria2].Visible = true
Me![DX_subform].Visible = true
Me![frm_number_incidents].Visible = true
End If
End Sub


Private Sub txt_Start_Date_Exit(Cancel As Integer)
if Me.txt_Start_Date.Text & "" <> "" then
Me![frm_criteria2].Visible = True
Me![DX_subform].Visible = True
Me![frm_number_incidents].Visible = True
Else
Me![frm_criteria2].Visible = False
Me![DX_subform].Visible = False
Me![frm_number_incidents].Visible = False
End if
End Sub
 
That worked great!

I was just trying out a if/else type statement when I got the notification that you replied, and I was close to doing what you had. Just missing a few thing :)

Thank You!
 
In my experience you should always explictly state what you want to happen if the condition is true AND if the condition is false anywhere you are using the test. Otherwise you get unexpected results like you've seen. Glad you have it working :)
 
I like this so much, I'm going to use it on another form, and this time instead of a text box, that is the deciding factor it's on a combo box.

On the first part I've changed this

If Me.txt_Start_Date & "" ="" Then

to

If cbo_Entered_By & "" = "" Then << because this is a combo box, does this need to change at all?

Then in the second piece of code

Private Sub cbo_Entered_By_Exit(Cancel As Integer)
If Me.cbo_Entered_By.Text & "" <> "" The part in bold does it need to be changed for a cbo?

Right now, it doesn't give me any errors, but it's not hiding anything.

Here is the complete code
Code:
Private Sub Form_Current()
If cbo_Entered_By & "" = "" Then
Me![TabCtl21].Visible = False
Me![frm_Member_Appeal].Visible = False
Me![frm_Letters].Visible = False
Me![frm_PRC].Visible = False
Me![lbl_name_select].Visible = True
Else
Me![TabCtl21].Visible = True
Me![frm_Member_Appeal].Visible = True
Me![frm_Letters].Visible = True
Me![frm_PRC].Visible = True
Me![lbl_name_select].Visible = False
End If
End Sub

Private Sub cbo_Entered_By_Exit(Cancel As Integer)
If Me.cbo_Entered_By.Text & "" <> "" Then
Me![TabCtl21].Visible = True
Me![frm_Member_Appeal].Visible = True
Me![frm_Letters].Visible = True
Me![lbl_name_select].Visible = False
Else
Me![TabCtl21].Visible = False
Me![frm_Member_Appeal].Visible = False
Me![frm_Letters].Visible = False
Me![lbl_name_select].Visible = True
End If
End Sub
 
For a combo that's storing a numeric key field you may need to change that to .Value instead of .Text, or just drop it altogether like the other test in the current event does.

The reason for using that property is really for when you place the code in a before update event where the value or text that you 'see' is not yet 'saved' to the table. That means that a test of this kind is blind to the value that you have typed in at that point in the process. In the on exit event you shouldn't have to worry about it since I'm fairly sure that the record has already been updated before the exit event triggers.
 
OK, I just dropped it.

So overall this should work?

Code:
Private Sub Form_Current()
If cbo_Entered_By & "" = "" Then
Me![TabCtl21].Visible = False
Me![frm_Member_Appeal].Visible = False
Me![frm_Letters].Visible = False
Me![frm_PRC].Visible = False
Me![lbl_name_select].Visible = True
Else
Me![TabCtl21].Visible = True
Me![frm_Member_Appeal].Visible = True
Me![frm_Letters].Visible = True
Me![frm_PRC].Visible = True
Me![lbl_name_select].Visible = False
End If
End Sub

Private Sub cbo_Entered_By_Exit(Cancel As Integer)
If Me.cbo_Entered_By & "" <> "" Then
Me![TabCtl21].Visible = True
Me![frm_Member_Appeal].Visible = True
Me![frm_Letters].Visible = True
Me![lbl_name_select].Visible = False
Else
Me![TabCtl21].Visible = False
Me![frm_Member_Appeal].Visible = False
Me![frm_Letters].Visible = False
Me![lbl_name_select].Visible = True
End If
End Sub

It's not seeming to do anything.
is there something Here I need to change for a cbo?
If cbo_Entered_By & "" = "" Then
 

Users who are viewing this thread

Back
Top Bottom