Solved Hide subform Does Not Work (1 Viewer)

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
Hello all;

I have gone thru hours a few articles in AccessWorld and a few youtube videos, plus countless google pages trying to find out why a SIMPLE task as hiding a subform just simply does not work for me.
THIS IS THE FIRST TIME I AM TRYING THIS SO PLEASE BE PATIENT.

my idea is that everytime the user wnats to create a new record, the subform goes to HIDE mode and once the user fills the LAST field (there's only 4, or is at the LAST field) the hiden SUBFORM now is visible and ready to add notes for that record.

I just spent almost 3 hours and I can't figure it out, please help me understand what am I doing wrong and how can I correct my error.

I have tried the following code already:


Code:
    ' NONE OF THIS WORKS WHY???
    Forms!PatientAdmitanceNF!PatientNotesF.Visible = True
    'Me.PatientAdmitanceNF.Visible = True

I was going to attach the database here with only the form in question, but I can't...
I imagine I am too new with not enough posts, but here is a link to the zip file is about 9.1Kb only [ the forum keeps telling the file is too big ]

ANY help will be appreciated.

LinkToTheFile


Thank you in advance.

Maurice.
 

tvanstiphout

Active member
Local time
Today, 12:02
Joined
Jan 22, 2016
Messages
225
More than likely you're using the wrong names, e.g. the SourceObject property rather than the Name property.

For an alternate way, see the Northwind 2 Dev edition template database, frmOrderDetails.sfrmOrderLineItems_Enter:
Code:
Private Sub sfrmOrderLineItems_Enter()
          'It doesn't make sense to allow user to (begin to) enter a Child row while the Parent row is not saved, due to Referential integrity. This also prevents a possible bug with subforms and recordsetclones that MSFT has not yet fixed (as of Oct-2023).
10        If Me.NewRecord Then
20            MsgBox GetString(sOrderBeforeOrderLineItems), vbExclamation
30            Me.CustomerID.SetFocus
40        End If
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:02
Joined
May 21, 2018
Messages
8,546
The name of the Subform control and the object inside the subform control (source object) do not always have the same name.
Yours is "Patient Notes"
Since your name has a space in it, in vba you have to put [Patient Notes] or Patient_Notes
Me.Patient_Notes.Form.Visible = True
Do yourself a favor and no spaces in any access cotrols or properties, ever.

Look at the controls Name property and then look at the Source Object property.

Normally it will default the control name to the name of the source object, but depends how you built the form.
Subform.png
 

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
The name of the Subform control and the object inside the subform control (source object) do not always have the same name.
Yours is "Patient Notes"
Since your name has a space in it, in vba you have to put [Patient Notes] or Patient_Notes
Me.Patient_Notes.Form.Visible = True
Do yourself a favor and no spaces in any access cotrols or properties, ever.

Look at the controls Name property and then look at the Source Object property.

Normally it will default the control name to the name of the source object, but depends how you built the form.
View attachment 113735
The Name was a stupid finger error, no other one has a space, but thanks for pointing out that.
 

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
The name of the Subform control and the object inside the subform control (source object) do not always have the same name.
Yours is "Patient Notes"
Since your name has a space in it, in vba you have to put [Patient Notes] or Patient_Notes
Me.Patient_Notes.Form.Visible = True
Do yourself a favor and no spaces in any access cotrols or properties, ever.

Look at the controls Name property and then look at the Source Object property.

Normally it will default the control name to the name of the source object, but depends how you built the form.
View attachment 113735
WORKED PERFECTLY.. Thanks I just learn something NEW thanks to you.
 

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
More than likely you're using the wrong names, e.g. the SourceObject property rather than the Name property.

For an alternate way, see the Northwind 2 Dev edition template database, frmOrderDetails.sfrmOrderLineItems_Enter:
Code:
Private Sub sfrmOrderLineItems_Enter()
          'It doesn't make sense to allow user to (begin to) enter a Child row while the Parent row is not saved, due to Referential integrity. This also prevents a possible bug with subforms and recordsetclones that MSFT has not yet fixed (as of Oct-2023).
10        If Me.NewRecord Then
20            MsgBox GetString(sOrderBeforeOrderLineItems), vbExclamation
30            Me.CustomerID.SetFocus
40        End If
End Sub
Yes I did, problem solved, and I learned a new thing.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:02
Joined
Feb 19, 2002
Messages
43,314
I have gone thru hours a few articles in AccessWorld and a few youtube videos, plus countless google pages trying to find out why a SIMPLE task as hiding a subform just simply does not work for me.
Why are you not simply using a bound subform? If you want to validate the data and ensure all fields are filled in, use the subform's BeforeUpdate event. Just ask if you don't understand how to ensure all fields are filled and have valid values.
 

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
Why are you not simply using a bound subform? If you want to validate the data and ensure all fields are filled in, use the subform's BeforeUpdate event. Just ask if you don't understand how to ensure all fields are filled and have valid values.
Thank you Pat, I had a fun time changing everything and figuring out how to validate the data, but a few googles and youtube help me out, I know is not perfect or the most awesome coding but it works.
thanks for the advice.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:02
Joined
Feb 19, 2002
Messages
43,314
You're welcome. Simple is usually better. Let Access do as much of the work as possible. That is the only rational way to use a RAD (Rapid Application Development) tool like Access.
 

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
You're welcome. Simple is usually better. Let Access do as much of the work as possible. That is the only rational way to use a RAD (Rapid Application Development) tool like Access.
Pat SORRY.. I hope you don't mind me asking..

I need to check that the user does not leave the fields empty so I am using
" If Null(FieldName) then MsgBox " FieldName X Cannot be empty ", etc.
Is there a better technique to check the fields so I don't have to write so many if then ????

thanks for your help..

Maurice.
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:02
Joined
Sep 21, 2011
Messages
14,333
Pat SORRY.. I hope you don't mind me asking..

I need to check that the user does not leave the fields empty so I am using
" If Null(FieldName) then MsgBox " FieldName X Cannot be empty ", etc.
Is there a better technique to check the fields so I don't have to write so many if then ????

thanks for your help..

Maurice.
That is not even valid syntax?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:02
Joined
May 21, 2018
Messages
8,546
I use a version of this to validate multiple controls. Works very well. Easy to use.
 

Attachments

  • ValidateData V1.accdb
    540 KB · Views: 17

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
I use a version of this to validate multiple controls. Works very well. Easy to use.
DARN GENIOUS.. Thanks to you and @arnelgp he has helped me before, simply GENIOUS...
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:02
Joined
Feb 19, 2002
Messages
43,314
You were not specific on what code you chose from that link. There's a lot of confusion in that thread. I think #5 might be similar to the solution I use. The important thing to remember is that you call the validation from your form's BeforeUpdate event.

Using the separate procedure in the stand alone module allows the code to be used regardless of which form calls it by simply passing in a reference from the calling form. I have also posted similar code but don't have a link handy.

Although I have and do occasionally use that looping code, I am more likely to just include all the validation for each control in one place.

If you use that code, my suggestion is to run it as the first thing in the BeforeUpdate event (after your authorization check if you do one) and if any required fields are missing, exit at that point. Just set Cancel = True and exit without doing the rest of the validation. That way, the detail validation only happens if required controls are not null so you won't have to repeat yourself when you get to the sanity checks like ensuring that dates make sense. 1/1/204 is a valid date as far as Access is concerned but is not likely to be valid in context unless you are doing some app that is tracking historical data.
 

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
You were not specific on what code you chose from that link. There's a lot of confusion in that thread. I think #5 might be similar to the solution I use. The important thing to remember is that you call the validation from your form's BeforeUpdate event.

Using the separate procedure in the stand alone module allows the code to be used regardless of which form calls it by simply passing in a reference from the calling form. I have also posted similar code but don't have a link handy.

Although I have and do occasionally use that looping code, I am more likely to just include all the validation for each control in one place.

If you use that code, my suggestion is to run it as the first thing in the BeforeUpdate event (after your authorization check if you do one) and if any required fields are missing, exit at that point. Just set Cancel = True and exit without doing the rest of the validation. That way, the detail validation only happens if required controls are not null so you won't have to repeat yourself when you get to the sanity checks like ensuring that dates make sense. 1/1/204 is a valid date as far as Access is concerned but is not likely to be valid in context unless you are doing some app that is tracking historical data.
Hello Pat.
Thank you so much for your suggestions, I do understand a lot of what you said, but I am "a learning curve" in this world, my training was a few decades ago with 'Foxbase" [ oh yea ], and now due to my Job being "at risk" I decided to learn Access, to keep my actual job, I started with a few videos, then I got some books, then somehow I found these forums and people that has been gracious to me like, you and @arnelgp, and MANY other countless ones, who had helped me greatly in the past.

In Logic, I believe I am not so bad, and have been able to learn a few tricks here and there.

My INSANITY comes trying to understand and FORGET the old ways [foxbase], and maybe you can understand why I am "coding" [programming], the way I do, I still see for me the need to "VALIDATE" when I click the "Save" button, with your perspective [the correct one should I say], I should do the Validation on the BeforeUpdate event, that terminology is quite NEW for me, and my head struggles to understand the idea, then I got lost when you said " (after your authorization check if you do one) ", since I am assuming knowing my OLD BACKGROUND in foxbase, that I do that with the code you share with me, I am going to try to use it in the BeforeUpdate event as you suggested, [made a backup twice just in case], if it works, I would much appreciate if I can post my code [or the link] here and would much appreciate if you can literally {DESTROY IT], for my own good, and help me understand why XY or Z is a mistake, in any place in the code, and maybe provide me some pointers on how could I improve it, it will be a teaching experience for me, and hopefully a great teaching experience for the rest of the members as well.

If you believe that creating a NEW post is better I will do as well, but of course YOU ARE UNDER NO OBLIGATION TO EVEN SEE MY POST OR HELP ME, my objective is only to be able to grab as much as your help as I can in order to improve my basic knowledge.

I have also found a website of an author named "Richard Rost" and I will subscribe under my "Po$$ibilitie$".

I thank you so much for your kind advise.
Sincerely..
Maurice.
 

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
You were not specific on what code you chose from that link. There's a lot of confusion in that thread. I think #5 might be similar to the solution I use. The important thing to remember is that you call the validation from your form's BeforeUpdate event.

Using the separate procedure in the stand alone module allows the code to be used regardless of which form calls it by simply passing in a reference from the calling form. I have also posted similar code but don't have a link handy.

Although I have and do occasionally use that looping code, I am more likely to just include all the validation for each control in one place.

If you use that code, my suggestion is to run it as the first thing in the BeforeUpdate event (after your authorization check if you do one) and if any required fields are missing, exit at that point. Just set Cancel = True and exit without doing the rest of the validation. That way, the detail validation only happens if required controls are not null so you won't have to repeat yourself when you get to the sanity checks like ensuring that dates make sense. 1/1/204 is a valid date as far as Access is concerned but is not likely to be valid in context unless you are doing some app that is tracking historical data.
Hey Pat, Here is the new Code [ See Link ] :

So I made the changes as suggested, thou for now I understand the code is a bit messy but I will clean it as soon as you review it if you do have time..

SO JUST IN CASE..
The idea is:
- if the End User [EU] decides to create a new Patient, then 2 things should happen:
-- 1) The Subform should be either invisible or locked [invisible is my preferred]
-- 2) All the main fields MUST be filled no mater what, and unless they are filled the SUB will never appear.
- Once the EU clicks the SAVE button and all is OK, the Notes Subform shows up, but the EU should be able to either enter notes or not.
- The EU then clicks the exit button, and the form, exits to the menu.

that is the Basic Idea, I am eventually going to return the user to the same form, then add a Return to Menu button, giving the option to the EU to either keep editing / adding records or not.

ANY Ideas to make it better will be greatly appreciated.

Maurice.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:02
Joined
Feb 19, 2002
Messages
43,314
ANY Ideas to make it better will be greatly appreciated.
Without seeing it in action, it is hard to say. As long as the validation runs from the form's BeforeUpdate event, the record will only get saved if all fields have values. Keep in mind that if you have AllowZeroLengthString set to Yes - WHICH, sadly, IS THE DEFAULT - a zls will be valid so the field will effectively be empty. I always change ALS to No. Then, if the field is required, I mark it as required so the db engine enforces the rule rather than my code. My code is only to give the user a better error message than he will get from the db engine.

In the form's AfterUpdate event, you can show the subform. Don't forget, you also need to show/hide the subform in the form's Current event. If Me.NewRecord = True, hide, otherwise, show.
 

mloucel

Member
Local time
Today, 12:02
Joined
Aug 5, 2020
Messages
157
Without seeing it in action, it is hard to say. As long as the validation runs from the form's BeforeUpdate event, the record will only get saved if all fields have values. Keep in mind that if you have AllowZeroLengthString set to Yes - WHICH, sadly, IS THE DEFAULT - a zls will be valid so the field will effectively be empty. I always change ALS to No. Then, if the field is required, I mark it as required so the db engine enforces the rule rather than my code. My code is only to give the user a better error message than he will get from the db engine.

In the form's AfterUpdate event, you can show the subform. Don't forget, you also need to show/hide the subform in the form's Current event. If Me.NewRecord = True, hide, otherwise, show.
Hello Pat, sorry for the delay, the link to the file is there in my message to you, if want to see it in action.. my apologies for any confusion.
Right here: Hey Pat, Here is the new Code [ See Link ] :
 

Users who are viewing this thread

Top Bottom