How to dynamically load, access and unload subforms in microsoft access - vba

I am trying top transition from ASP.NET to programming in access and I am used to thinking in terms of Usercontrols when I think of subforms in Access. What I would like to do is allow user to click a button to load a subform that contains controls user can enter additional data into. I would appreciate any information or resource that would help me with understanding how this is done in MS Access -- how to load, unload and access data in the subforms as well. Thanks in advance

Well, as a general rule such loading of sub forms is done automatic and does not require any coding on your part. So you in general are best not to worry about this issue and save all that coding time for helping the poor and needy in your neighborhood.
I should however point out that if you building web forms in Access, then the sub forms are dynamic loaded ONLY when they are used. So if you place a sub form behind a tab control, then the resulting web form (they are XAML forms when published and form code is converted into JavaScript), then the sub form is dynamic loaded for you into the browser. No doubt this setup does result in web forms loading a lot faster. So for web forms, such loading is dynamic and on demand for Access Web forms.
However, my guessing here is you talking about Access client forms and not Access web forms. Given that case, on the client side, load time is rather fast and is quite rare to worry or need or waste developer time on doing this.
However, there are some cases, such having to load say 5 sub forms, and such time can start to add up to the point where a user might start to notice a delay. In this case, you can dynamic load a sub form, and you do this by setting the source object property of sub-form object.
So keep in mind that a sub form is only a "control" and is not tied to an actual form. IN most Access applications I seen the name of this sub form control is the same name as the sub form, but it certainly does not have to be.
So, to dynamic load a sub form, such as when changing to a different tab on a form, the code looks like this:
Private Sub TabCtl2_Change()
If Me.TabCtl2.Value = 1 Then
If Me.frmListContacts.SourceObject = "" Then
Me.frmListContacts.SourceObject = "frmListContacts"
End If
End Sub
So, if you place a sub form control on a form, but leave the source object setting blank, then no form will be loaded or display for that sub form. And in the above, once I loaded the form, then the source object setting will not be blank, and thus I don't attempt to set/load the sub form more than once.
As noted, in the vast majority of cases when loading a form, you need that sub form to load and display anyway, so in most typical applications such above code is not required.

Related

How to avoid sudden stopped in MS Access?

In my access project, I have Navigation form named DepartmentMgtWindow and one of its subform is named DepartmentPanel. In my DepartmentPanel I also have subform/subreport that target a form named PcnsList, where PcnsList is a continuous form. Consider the image below:
In my event (On Open) for DepartmentMgtWindow, I want to hide PcnsList hence the code:
Private Sub Form_Open(Cancel As Integer)
Form_PcnsList.Visible = False
End Sub
When I run the project, my access suddenly crashes and gets Microsoft Access has stopped working, and if I comment out the code inside the event it works fine.
I also put On Open event in my DepartmentPanel and I am getting the same result.
Any help/suggestions are highly appreciated.
Code fails because form PcnsList is not open as an independent object and therefore is not in active Forms collection.
Subform path referencing must be through container names, not form names.
Looks like PcnsList is on a form that is loaded as Navigation Target. Access assigns container that holds target forms a name of NavigationSubform by default. So if the embedded subform container control is named PcnsList and code is behind DepartmentMgtWindow form, try (yes, that is the word Form, do not replace with a form name):
Me.NavigationSubform.Form.PcnsList.Visible
I always give container control a name different from object it holds, like ctrPCN, then:
Me.NavigationSubform.Form.ctrPCN.Visible
For code behind DepartmentPanel: Me.ctrPCN.Visible = True
However, advise to set PcnsList subform container as not visible in its design and then code makes visible when needed. This would eliminate problem code from main form.

User Control Form (Wizard panel) to have common ShowData() function

Like many, I'm trying to build a wizard like presentation.
I've created a Windows Form as the host for a series of "WizPanel"s that have been designed as User Control Forms. Each of them may/will have unique data connections and layouts. My intended implementation creates a Collection(of UserControl). I'm at the design point that I want to load the collection with a set of panels that best describe the process flow required by the current use case.
The problem I'm running into is how to be able to load a New UCDataReview (user control form), a New UCDataParameters (user control form), then the rest of the panels as required. I don't want the panels to load data at this point as some of the data queries may never be required - based on data supplied in previous panels (possibly, just design at this point). I just want to make sure data only loads when I specify. I like to have a function ShowData() on each of the panels and then execute it from within the main Wizard form (after the next button is clicked - it wouldn't be needed on a back as it should be there already).
So, I'm stuck on the concept of crating a 'base wizard panel' control (WizPanelBase) that has a ShowData() function that is empty in the base control form but can be overridden in the class (WizPanel) that the panel is based upon. I'd then have a Collection(of WizPanelBase) that would then allow me to issue a ShowData() call.
I'm sorry for not using the correct verbiage as I'm only a part timer and just playing to learn. I've done this with an Object Class (ObjBase_Person; ObjBase_Vehicle ...) and then extended them into Person_Student, Person_Instructor, Vehicle_Car, Vehicle_Truck etc). I'm quite sure I can, I just know enough to be dangerous, but I don't know what to even ask for to find tutorials. The similar questions here are generally about using a User Control on a Windows form.

How to change the order of open "tabbed forms" in MS Access via VBA

I am using "tabbed document" forms in MS Access (each main form that is open has a "tab" to allow users to easily move between forms).
Does anyone know of a way to reorder such opened forms via VBA? I have workaround code that closes all the forms and then re-opens them in the order I need, but it is clunky and slow (some of the forms are big so take a while to load, and often users have applied filters/sorts which I then need to individually re-apply, plus resetting the current record etc etc).
As all I need is to change the order of the forms on-screen, so my approach seems like overkill - but I can't seem to find info on how to do this anywhere!
(FYI I am NOT talking about the order of pages within a tab control, to avoid any confusion!)
Please attached screenshot with three tabbed forms open, I'd like to re-arrange them so eg left to right they become: #Home, Booking Detail, Enquiry Detail
Thanks
Ok, I have inadvertently figured this out.
In testing I found that if you hide a tabbed form, when you unhide it, it doesn't show up in the same position, but in fact now shows as the last form (ie the rightmost tab)
Eureka! Now to reorder the forms I simply hide them, and then unhide them in the order required. Combine this with DoCmd.Echo = False and there is, in my testing, virtually zero overhead.
Phew!
If the user is not seeing all the forms at once, you don't need to waste resource/time loading all forms at once. Load only what the user supposed to see. NavigationControl is very good for that.
Method 1
Move from tabbed documents to NavigationControl. Each tab is loaded OnDemand hence your main form start-up would be much more quicker. If the main and tabbed form are related to each other, use tabbed form's OnOpen event to dynamically change the record source.
I.e. Form_Open => me.RecordSource = Select * form T1 where T1.id = ParentForm.Id
Method 2
If you can't move to Navigation control, simulate the same effect as above.
When tab pages are changed/selected => underlying form gets a record source.
I.e.
Private Sub TabCtl2_Change()
If Me.TabCtl2.value = 1 Then
me.subform1.recordsource = source
me.subform1.LinkChildFields = linkingFieldName 'if related
me.subform1.LinkChildFields = LinkMasterFields 'if related
ElseIf Me.TabCtl2.value = 2 Then
'Your other form
end if
End Sub
This can help you to reduce your loading time.
[Cascading result sets]
In case if all of your forms are showing cascading results, you might want to consider remove record-source for all of your subforms and then re-apply at each selection.
The goal is to minimise loading time and show only necessary data the user is supposed to see/want to see. Hope this helps to have some idea.

VB.NET - Navigating without loading another form

I'm currently developing a simple enrollment system for our school project. I have multiple modules that when clicked will open a new form with another set of sub modules.
However, I hate how forms load every time I open a new form. Is there like a way that when I clicked say for example "Enrollment" button, instead of loading a new form, it will change all the controls instead. Tried using User Control but it keeps crashing my program down after I created 2 user controls and load them in one form.
Main Form;
Enrollment Sub Form
Please help me guys.
As LarsTech mentioned I would use user controls instead of forms.
Each user control would be setup the same way as your form is, but can be loaded to a panel on the main form, allowing for a single form to show all content.
Dim myControl = New ControlYouCreated
panel1.Controls.Add(myControl)
myControl.bringtofront
This for example, will bring in the contents of the user control you created, into your form1 panel.

Making multiple forms appear as one in VB.NET

I am writing a Windows Forms application in VB.NET. I have three forms: the main form, which shows a list of accounts, the account form which allows the user to view/edit the information for a specific account, and the policy form which allows the user to view/edit the information on a specific policy for that account. I want the forms to appear as if they are all the same window. Example: when the application starts, the user clicks an account name in the list box on the main form and clicks "edit". What I want to happen is that the window stays in the exact same place and stays the same exact size, only the content of the main form appears to be replaced with the content of the account form. Same thing if the user then chooses to edit a policy from the account form. When the user finishes and clicks "save", the main form comes back up. Through this entire use case, it would appear to the user as if they were viewing the same window the entire time, with the content of that window changing.
How can I do this? I have tried something like:
Dim newForm as New AcctForm
newForm.Location = Me.Location
newForm.Show()
Me.Close()
The problem is that if the user moves the original window, the new window appears where the parent form originally appeared, not where it ended up.
I see this is already in the comments, but what I have done in this case in the past is build each "form" in the application as a custom control. Then I have one actual form, and navigation works by changing which custom control is currently loaded on the parent form. To move from one screen/view to another, you remove the current custom control from the form's controls collection and add the new custom control.
I believe this is superior to manually setting the startup position and size, because you can use the form's .SuspendLayout()/.ResumeLayout() methods to hide the interim state, where there is no control loaded, from the user. This is harder to do when you want one form to be completely replaced by another.
This also makes it easy to set certain form properties in one place and have them be consistent for the application. You can even have an area on the form with controls that will now show in every view.
When using this pattern, I typically have each of my custom controls inherit from a common base. You may not have anything specific you will do with that base at the outset, but it almost always comes in handy later.
Finally, switching to use this scheme is easier than you think. Just go to the code for the each of your current forms, and you will find that each class currently inherits from System.Windows.Forms.Form. Most of the time, all you really need to do is change them to inherit from System.Windows.Forms.Panel and you're most of the way there.
As others have said, it may be better to redesign your application using custom controls or panels etc.
However, to answer your question regarding the seemingly random location of your forms, the first thing to check is that each form has it's StartPosition property set to Manual.
If your main form is resizable, then I would also add code to adjust newForm to the same size too.
I hope that helps with your immediate issues; so that you can move on to redesigning the application!
good morning there is another way . set property for second form to (top most) and use also
from2.show();
that make you switch between forms and keep form2 top other
Thanks
try using ShowDialog()
Dim newForm as New AcctForm
newForm.Location = Me.Location
newForm.ShowDialog()
Me.Close() <-- removed this