How to avoid sudden stopped in MS Access? - vba

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.

Related

Access runs function in control's RowSource when form opened in Design View

I have an Access 2007 form which contains a combo box with the following Rowsource:
SELECT qryProjectsIHaveAccessTo.projID, qryProjectsIHaveAccessTo.projName
FROM qryProjectsIHaveAccessTo
WHERE (((qryProjectsIHaveAccessTo.projSupportTracker)=False));
The query qryProjectsIHaveAccessTo uses a user-defined function as the criteria for one of the columns. This function detects if a Startup routine has been run, and if not runs it.
The problem I have is this: if I SHIFT+open the database and open the form in DESIGN mode, for some reason the user-defined function starts running. This then causes errors as the Startup routine it calls tries to open a form (presumably Access cannot open a form while it is in the process of opening another form in Design mode) and sometimes I am not able to CTRL+BREAK out of it.
The same thing happens when I go to save the form in Design mode.
If I remove the RowSource string from the combo box, this no longer happens. But why would a function which is called in a query included in a control RowSource get fired when the form opens in Design Mode?
Any ideas anyone? Thanks for reading!
When you open a form in design view, Access validates that the form record source is still there, and that all the query fields that are bound to form controls are still there.
If some of these checks fail, Access shows the small green triangle on the bound controls with problems.
While doing this check, it runs the UDF in the query.
As was said in the comments, calling a startup function does very much not belong into UDFs that are called from a query. Put it into a function that is called by an AutoExec macro.

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.

Loading new form within a navigation tab in ms Access 2010

I have created the following navigation structure in a Microsoft access 2010 database:
You can see that there are two tabs, and the data for the tabs is populated with information linked to the ClientID, which is stored in an un-editable textbox at the top of the form. When the Communications Forms tab is selected, a list of communications forms that have been completed for the specific ClientID is shown. And there is a button to create a new form. My question is how do I write the macro so that clicking on the Create New Form button will cause a blank new form to be loaded in the space that is currently occupied by the List of Forms?
Below is what I have so far. It sends the user to a new form instead of embedding the new form underneath the Communication Forms tab in the current form. How can I change the below so that the blank new form is loaded under the Communication Forms tab in the current form, so that all the navigation controls remain visible/usable?
EDIT:
To address HK1's assumptions below, I am adding the following description of the steps I took to create the form in the screenshot above:
1.) I created a blank form in design view.
2.) I added a listbox to list client fullname and id, and a textbox to filter the listbox.
3.) I added the clientid and fullname textboxes to the form, and set them to change based on
what the user selects from the listbox
4.) I dragged a navigation control onto the form next to the listbox
5.) I dragged a form called "ListOfForms" onto a new tab in the navigation control to create the tab
6.) I added the CreateNewForm command button to the ListOfForms form while embedded in the main form
Here is the result of HK1's suggested code:
While I appreciate it, it does not do what I need. You can see that it just adds an additional row to the list in ListOfForms. Instead, I need the code to place a blank MyForm in the place of MainForm where ListOfForms is currently located. Thus, under the CommunicationsForms tab, all the user would see would be a blank MyForm object, which is a different form than ListOfForms.
When I click on the place where ListOfForms is located in Layout View, I see that it is called NavigationSubForm in the Property Sheet. Thus, NavigationSubForm would swap in MyForm in place of ListOfForms when the user clicks on the Create New Form command button. But if the user clicks on CommunicationForms tab again, ListOfForms would again be placed in NavigationSubForm.
I tried the following:
Private Sub cmdCreateNewForm_Click()
Forms!MainForm!NavigationSubform = MyForm
End Sub
But it generates a Runtime Error '438': Object doesn't support this property or method.
Next, I tried:
Private Sub cmdCreateNewForm_Click()
Forms!MainForm.NavigationSubform.SourceObject = MyForm
End Sub
This causes the NavigationSubForm to go blank, so that ListOfForms disappears. This seems like I am on the correct track, but what do I need to do to get it to put a blank MyForm in the NavigationSubForm instead of just an empty space?
The form you have labeled as "List of Forms" appears to be what's called a continuous form, and I'm guessing it's inside a subform control located on a another form. I'm also guessing that your Create New Form button is probably located on the header section of your continuous form. I'm also making the assumption that your continuous form/subform is bound to an editable recordset/recordsource but there's no way I can tell this by looking at the screenshot.
With these assumptions in place, the code for your Create New Form button would probably be something like this:
Private Sub cmdCreateNewForm_Click()
Me.AllowAdditions = True
DoCmd.GoToRecord, , acNewRec
End Sub
If any of my assumptions above are incorrect than it's likely this code won't work as expected.
Be aware that New Records in continuous forms (and datasheet forms) always appear at the bottom of the list. I'm not aware of any easy way of making them appear anywhere else.

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

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.

TabControl.SelectedIndex being changed, but SelectedIndexChanged even not firing

All,
I have a TabControl in an application that started behaving strangely. Some background...
This program was converted from VB6 to VB .NET 2008, and used to refer to forms using their class names. In other words, I might have a form class called frmFoo. In the code for the program you might see:
frmFoo.Show()
or
frmFoo.UserDefinedProperty = True
During some recent changes, I created variables to represent instances of my forms much like these:
Public MyForm as frmFoo
MyForm = New frmFoo
MyForm.Show()
In doing so, I also removed code from the form's Load event handler and put it in the form's constructor.
When the form loads, or when a document is loaded and should influence the TabControl's selected index, something like the following will not necessarily fire the SelectedIndexChanged event.
MyForm.tbsForm.SelectedIndex = ValueReadFromFile
...or...
MyForm.tbsForm.Tabs(ValueReadFromFile).Select
Sorry to be so wordy, but there's more. If I open the form and look at the TabControl to verify that it's been set properly, everything works like it's supposed to. The misbehaving TabControl is contained within another TabControl, so I have to click the parent TabControl to see it. If I can see it, and run a test, the test always works. If I can't see it, and run a test, the first test I run will not fire the event. ...paging Dr. Heisenberg...
It's almost as if the control has to be initialized first by changing the value or making it visible onscreen...I'm totally lost on this one. It's the most unusual behavior I've ever seen. And everything worked perfectly before I began using variables to represent forms and placed the Load event code into the form constructors.
Can anyone help, or at least put me out of my misery?
SH
-------------------------------------------------------------- Edit #2
I just performed a test after having attempted to eliminate some of the variability in the behavior. But I wanted to confirm the previously-stated behavior.
I opened the program and read a file. This file contained a value that should have triggered the event handler. Without making the control visible, I can change the SelectedIndex property of the tab control without the event firing.
I closed the program down again, and reopened it. This time, selected the parent tab that allowed the child tab (the one whose event I'm concerned with) to become visible. I then selected a different tab in the parent control, meaning that the child control was no longer visible. When I opened the same file as before, it fired the event.
I'm tempted to implement a flag that confirms that the control has been repainted or whether the parent tab has been displayed. I may have to fire the event in code if the flag isn't set.
I want to reiterate that everything worked when the program referred to the forms by their class names and much of the arrangement of controls on the forms was done in the load event. Now the program creates variables and the arrangement of the controls is done in the form's constructor. I'm sure this has something to do with the problem I'm having, but I can't understand how. Any wisdom to share?
MyForm.tbsForm.SelectedIndexChanged = ValueReadFromFile
doesn't make a lot of sense. Is tha trying to assign a handler to the SelectedIndexChanged event? or is ValueReadFromFile the name of the tab?
What you're saying is that you have two tab controls, say, A and B. Tab control B is contained within a tab of A, and unless A has the tab page selected that contains the tab control B, the SelectedIndexChanged event of B will not fire if you change its tab programatically?
In which different ways have you tried to select a tab within the child tab control, and when is this code being executed?