delete tabs from a form (1 Viewer)

Kingz

Member
Local time
Today, 07:35
Joined
Mar 19, 2024
Messages
34
Hi,

I am having problems deleting the tabs of my Tabcontrol in an access form.
I have a button on one form, which is supposed to force the deletion of the tabs in another form. My way of doing it seems to cause a problem as I get an error that the form needs to be in design mode to do this(error 2147).

In the prior form, I call the function..

DeleteAlltabs Form_xyz

The function looks like this:

Private sub delete all tabs(frm as object)
Dim tabctrl as Tabcontrol

Set tabctrl = frm.Controls("MainTab")

If not tabctrl is nothing then
For I = tabctrl.Pages.count-1 to 0 step -1
tabctrl.Pages.Remove (i)
Next i
End if
End sub

Any help is greatly appreciated.
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:35
Joined
Sep 21, 2011
Messages
14,299
Yes, you will likely need to open the form in design mode to do that.
However your process sounds flawed. Deleting/adding objects is not the norm.
 

ebs17

Well-known member
Local time
Today, 07:35
Joined
Feb 7, 2020
Messages
1,946
that the form needs to be in design mode to do this
Yes it is.
Design changes at runtime are often bad style.
You should also immediately consider that in a compiled version (ACCDE) the design view is blocked for distribution to third parties and therefore this code cannot be used at all.

Perhaps it is enough to set the register control's pages to invisible at runtime and remove data content that could generate overhead.
 

Kingz

Member
Local time
Today, 07:35
Joined
Mar 19, 2024
Messages
34
First of all thanks for the prompt answer.

I've seemingly got a conceptual problem. The thing is, depending on the fields of a table, the tabs are to be created. I have one form, which allows the user to choose one of three forms before updating the dynamic form. It sounds like I should update the forms straight after the login without the forms being active. How would I need to change my code?
 

ebs17

Well-known member
Local time
Today, 07:35
Joined
Feb 7, 2020
Messages
1,946
I've seemingly got a conceptual problem. The thing is, depending on the fields of a table, the tabs are to be created.
You recognized that well.
All the necessary pages of the tab should already exist. Then you no longer have a design problem.

As I said, pages that are not required can be made invisible and it is best not to load any data there.
You can only see one page at a time. Therefore, only this page's data would be refreshed upon activation, provided no interdependencies have been built in between these pages and their controls and other objects.
Such interdependencies would be another design problem.
 

GPGeorge

Grover Park George
Local time
Yesterday, 22:35
Joined
Nov 25, 2004
Messages
1,867
It's probably not a great idea to constantly modify forms at all.

It sounds like you are trying to compensate for an inadequate table design, though.

Let's backtrack. Show us the tables. We can return to form design later, but first, we need to be sure your table design is appropriate for a relational database application.
 

Kingz

Member
Local time
Today, 07:35
Joined
Mar 19, 2024
Messages
34
Ok, I'll explain the actual problem and hope for an adequate concept to solve the problem.

I am a headmaster and a teacher of a class of 12 people. The names, age and sex are recorded in a table "pupils". So as I log in to the application, I would like the tabs of a Tabcontrol on my form to be dynamically updated with regards to the table pupils. Depending on sex, I actually have one more combo box on a dialogue.

I actually have a function, which creates the controls on my form, when it's in the design mode. I just go to the function in the module and it works.. With me deleting the tab beforehand manually from the form.

However, I want to merely add another column to the pupil table so that I can distinguish subjects, so that another teacher with a different subject could select his/her pupils, and these pupils would be tabs on his form.

You know what I mean?

To be honest, it doesn't have to be created from a form. I could create them all at the beginning at some point, but I do need it to be created dynamically and not manually and then I could just call up the forms when needed.

So the function should remove the tabs and create the new tabs according to a table.
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:35
Joined
Sep 21, 2011
Messages
14,299
And when you get your 13th pupil? :(
 

Kingz

Member
Local time
Today, 07:35
Joined
Mar 19, 2024
Messages
34
The number of pupils are irrelevant, as it goes through a recordset with the pupils and creates "While (rs.Eof = false) "
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:35
Joined
Oct 29, 2018
Messages
21,473
The number of pupils are irrelevant, as it goes through a recordset with the pupils and creates "While (rs.Eof = false) "
I think the point was how "wide" would your form be as you add more pupils, if each one is a tab on it? And even if you use multiple horizontal tabs, that could look crowded on a form, don't you think so?

By the way, were you aware there is a lifetime limit on the number of controls you can add to a form? So, as you delete and add new tabs to that form, you eventually won't be able to add any more, so you won't be able to show all your pupils.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:35
Joined
May 21, 2018
Messages
8,529
I am a headmaster and a teacher of a class of 12 people. The names, age and sex are recorded in a table "pupils". So as I log in to the application, I would like the tabs of a Tabcontrol on my form to be dynamically updated with regards to the table pupils. Depending on sex, I actually have one more combo box on a dialogue.

I actually have a function, which creates the controls on my form, when it's in the design mode. I just go to the function in the module and it works.. With me deleting the tab beforehand manually from the form.

However, I want to merely add another column to the pupil table so that I can distinguish subjects, so that another teacher with a different subject could select his/her pupils, and these pupils would be tabs on his form.

You know what I mean?

To be honest, it doesn't have to be created from a form. I could create them all at the beginning at some point, but I do need it to be created dynamically and not manually and then I could just call up the forms when needed.
Everything about this concept is bad. Creating controls at runtime will eventually crash the form as you max out the number of controls in the life of a form. You can also never make a runtime or accde if needed.

Your tables do not sound normalized.
However, I want to merely add another column to the pupil table so that I can distinguish subjects, so that another teacher with a different subject could select his/her pupils, and these pupils would be tabs on his form.
There are plenty of good school database examples with properly normalized table structure. Just search this forum.

If you have to add more fields to tables because of more data or more controls to forms because of more data then your table design is wrong and form design is wrong. So forget this idea and tell us how you want to interact with your data.

If the absolute amount of tabs is not known then instead of different tabs have a single subform. You can then have a listbox/subform/or combobox on the side of the subform to select the current student. This would allow for unlimited students. Bottom line you can only see the data on each tab one at a time anyways. I demo this as well.

However, If you can limit the amount of students to a absolute max number. Here is how you can do it properly. This is fully normalized and no dynamic controls (even though it appears so). In this example you can select a law firm and each employee gets their own tab. I limited to 20 but I guess you could easily go to 50 or more.

I think the point was how "wide" would your form be as you add more pupils,
This is not an issue because you can make rows of tabs. In this example there is up to 4 rows.
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:35
Joined
Sep 21, 2011
Messages
14,299
There are plenty of good school database examples with properly normalized table structure. Just search this forum.
In fract there is one experienced developer here who has created his own, which various schoold use.
Perhaps overkill for your purposes, but there might be others, less well developed.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:35
Joined
Feb 19, 2002
Messages
43,275
Thinking that tabs should be used to show data values is a common misconception. They are more of a functional grouping. Tabs are static and never created/deleted on the fly. If you want a more graphical view, search for the native VBA treeview. It should be posted here somewhere. Here's one that shows Objectives, templates, Process Documents, Assets, Tools, and Artifacts for a project documentation tool.

If you want to see a list of students and then as you click on each one, look at a split form. Access provides a model and it may work for you.

PB_KPISupportingItems.JPG



This form is a custom "split" form. It is built without using the Access form. It shows an unbound main form with three subforms. The left subform is a list of Endorsements for an Insurance application. When you click on an endorsement, the details show in the top right form. Then the lower right form is a subform of the endorsement and it shows a list of required data fields. This app was used to build all the documents required to quote and issue an insurance policy and it ensured that all forms always had the same data value for a specific field.
DGT_ThreeSubforms.JPG
 

Kingz

Member
Local time
Today, 07:35
Joined
Mar 19, 2024
Messages
34
Admittedly, I might be misusing the functionality of a tab control somewhat, but I feel it shouldn't be such a problem to fill a Tabcontrol dynamically according to the contents of a table or tables. By the way the table I described was just an example so let's not dwell on the content.
I am now using a line:
DoCmd.OpenForm "name", acDesign

...Before the line:

Set tabctrl = frm.Controls("MainTab")

To, then, delete the pages afterwards, but I get an error 2467 " Application or object defined with".. Can anyone help me there? I feel I'm close :)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:35
Joined
May 7, 2009
Messages
19,243
re-think what you are doing.
you can delete tab on Design view.
just Hide it if you want.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:35
Joined
May 21, 2018
Messages
8,529
Admittedly, I might be misusing the functionality of a tab control somewhat, but I feel it shouldn't be such a problem to fill a Tabcontrol dynamically according to the contents of a table or tables. By the way the table I described was just an example so let's not dwell on the content.
You can think all you want but you are wrong! Adding controls at runtime is problematic. People on this forum are not going to waste their time helping you do something stupid. You can look at my demo that clearly does what you want and is a proper way to do this.
Or you can continue to waste your time asking how to add controls dynamically at run time.

Depending on how many controls you add and delete it is guaranteed to STOP working when you exceed the limit of controls on the life of a form (this counts the added and deleted). It is not an IF but a when.
 

Minty

AWF VIP
Local time
Today, 06:35
Joined
Jul 26, 2013
Messages
10,371
You can change the title of a Tab page dynamically without affecting the actual design of the form, and hide unused tabs.
That would be perfectly acceptable design, but still limits you to however many tabs you set up in the initial form design.

This is where your design falls over. You have 12 pupils, so as a safety measure you design and save you form with 15 tabs.
Then the unexpected happens and you end up with 17 - now you have to re-build your form.

No design should ever need to accommodate this type of practice.
Listen to what the experts here are telling you.
 

ebs17

Well-known member
Local time
Today, 07:35
Joined
Feb 7, 2020
Messages
1,946
You ignore content information. OK, if you don't want to hear, you have to feel (at some point, faster than you think).

Technically:
Code:
DeleteAlltabs Form_xyz

' ---------------------------------------------------

Private sub delete all tabs(frm as object)
' ...
You should use the same name for the procedure in the definition and when calling it, but in any case also a valid name.. What is your particular consideration for not doing this?
Why do you assume that a written Form_xyz is an Object?
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:35
Joined
Sep 21, 2011
Messages
14,299
Listen to what the experts here are telling you.
I am telling you the same, and I am not even an expert, just a dabbler. :)
How would you feel if I told you your curriculum for your pupils, was all wrong as I would have one curriculum for every pupil, so when the next registers, you need to complete a whole new curriculum paperwork. Admittedly it might be a photocopy, but still. :(
 

Users who are viewing this thread

Top Bottom