How to Repaint Microsoft Chart in Access form - vba

I have a subform which sets the RowSource of a Microsoft Chart 5.0 object in its parent form.
EDIT: The Row Source Type for the graph is Value List.
The graph does not paint itself, however, but any action which would generally generate a repaint (drag another Access window over it, minimize it, loss and regain of focus sometimes) does indeed repaint it. In other words, the graph doesn't show or change automatically.
How do I force a repaint of the chart after a subform action?
These have had no effect:
parent.Referesh
parent.Repaint
parent.TheChart.Refresh
And this does not seem to exist, unfortunately:
parent.TheChart.Repaint
Using: Access 2003

The problem isn't the repainting of the graph. Most likely, the subform that you are using is retrieving data from a query or table.
You must update the values on the "datasource" object, which is providing the subform data (either a query or a table). Then, re-query the graph object in the mainform.
I made a very simple example in my MS-Acess 2000 and it worked great:
A table (t01_fruits) with three columns (frt_id, frt_name, frt_qty).
A subform based in the t01 table.
A mainform with the previous subform inside, a pie-chart based on table t01_fruits and a button. In the OnClick event of the button, I put just me.graph1.requery.
When I update the fruits quantity in subform, inside mainform, nothing happens with the graph. When I click the button, the graph is updated correctly.

I also faced same problem in refreshing chart after updating information in form. I got it resolved by re-queering the chart after update of the inbox linking to change of data.
Private Sub txtFinAgreeEnacDt_BeforeUpdate(Cancel As Integer)
Me.graphCRMStatus.Requery
end sub
Private Sub txtFinAgreeEnacDt_AfterUpdate()
Me.Requery
End Sub
Me is the Form holding graph....

I had similar problem like this, having main page with sub-forms, including 2 with graphs - and solution to refresh it was like this:
me.subForm.Requery
me.subForm!Graph1.Requery
Only when you execute commands in this order, it would update graph properly.
I know it's been a while since question was asked, but in case anyone else has issue like this.

Related

Subform showing up blank after code executed

My goal is to change the SourceObject of a subform. I can do this manually by going into design view selecting the subform object and changing SourceObject and removing the master and child links. I save changes and reload into form view and all works well.
When I try doing this via VBA it does not seem to work and gives me a blank subform.
Here is the code that I am currently running:
Private Sub ondacapBtn_Click()
Me!DACAPRosterQ_subform.SourceObject = "ondacap_subform"
Me!DACAPRosterQ_subform.LinkMasterFields = ""
Me!DACAPRosterQ_subform.LinkChildFields = ""
Me!DACAPRosterQ_subform.Requery
I find this even crazier because this is code I've used before except the Me!subform is a different name, and it works perfectly elsewhere! I am complete baffled at how this works in the other subform but not this one.
I've tried changing the name, the tab order, made sure the subform I am changing to works, made sure the code matches the other subform shown below, I am out of ideas and I made this account just so I can ask this question. It's really upsetting me.
Private Sub FilterBtn_Click()
Me!Retrain_subform.SourceObject = "UpcomingRetrain_subform"
Me!Retrain_subform.LinkMasterFields = ""
Me!Retrain_subform.LinkChildFields = ""
Me!Retrain_subform.Requery
EDIT - 26SEP22
I found out that one of the fields in my RecordSource is causing this issue.
My subform has a RecordSource called DACAPRosterQ and in that query I've input a field that is called "SearchTerms:" [with] & [other fields] & [listed] & [to use as search bar criteria]. The table PersonnelT that the query DACAPRosterQ pulls from does not have the "SearchTerms:" field naturally. Only the query.
When I remove this field from the query the subform can be switched with no issue via VBA.
Something I noticed when trying to open Ondacap_subform individually without the parent form is it asks for input "Forms!DACAPRosterF!DummySearchTextbox" which is a control on the parent form DACAPRosterF alongside the subform.
I have a feeling I must input some code to reinitialize that field as it is not done so via VBA. I'm just not sure what it is.
I think this would also explain why I am able to switch SourceObject manually via design view because it it already being reinitialized when reopening the form in form view.
Anyone have any ideas?
I figured it out... I couldn't tell you why it works or why it didn't work without this solution.
All I had to do was enter into the subforms OrderBy property, I ordered it by [Field1], [Field2]. Works like a charm now. Search bar still works and everything functions as normal.
If anyone could provide insight as to why this occurred, that would be amazing. Otherwise, problem is fixed.

How to Update the Values of a Dropdown in a Subform when the Main Form Changes

I have two forms:
InterviewMaster and InterviewDetail
InterviewDetail opens up as a subform in InterviewMaster and these two forms are linked through a common field called InterviewID
In InterviewDetail I have a textbox called Questiontype as well as combobox called InterviewDropdown.
The data in the dropdown varies based on the data on in the textbox. To make this happen, I have a next button to move to the next question. Whenever I click on next the following runs:
Dim ctlCombo As Control
Set ctlCombo = Forms!InterviewDetail!cmbInterviewDropdown
ctlCombo.Requery
The Row Source setting for my combobox is set to look up the required answers, again this is based on the value as per the textbox:
SELECT [queryAnswerOptions].[Answer] FROM queryAnswerOptions ORDER BY [Answer];
So the options are determined by my query called queryAnswerOptions
So as I cycle through my questions using my next and previous buttons, the dropdown options are updated based on the value of my textbox. This works perfectly when I open the subform from the navigation pane. However, when I open the main form and click on the next button my dropdown does not have any values. I've tried requerying the subform with no luck. I've also tried opening the subform in full screen from my main form but this also does not work. I also don't want to go that route as it does not work well with the overall flow of my form.
Any assistance will be greatly appreciated.
Problem with the query WHERE criteria parameter is when form is installed as a subform, the form reference no longer works because it must use the subform container control name. I always name container control different from the object it holds, such as ctrDetails.
Option is to put SQL statement directly in combobox RowSource instead of basing combobox RowSource on a dynamic parameterized query object - then it will work whether form is standalone or a subform.
SELECT Answer FROM InterviewAnswers WHERE QuestionID = [txtQuestionID] ORDER BY Answer;

How to reference events on objects in a subform from inside the main form

Imagine a form that, for demontration purposes, contains only a subform and a textbox that need to mirror each other.
I tried to accomplish this by setting the Control Source on the textbox to the value of the field in the subform and this worked to make them mirror each other, but the textbox isn't editable so this is an unsuitable solution.
The next thing I decided to try was using the AfterUpdate event on both controls to run code that sets the value of the other control.
This is easy for the textbox:
'Set value of Notes field on subform whenever value of the corresponding textbox
Private Sub Notes_Textbox_AfterUpdate()
Me.subform.Form![Notes] = Me.Notes_Textbox.Text
End Sub
However, this isn't as simple for the subform field. I don't know how to reference an event on a field in a subform in a way that will still allow me to reference controls outside that subform.
As a demonstration, I need a way to do this:
Private Sub subform_Notes_AfterUpdate()
Me.Notes_Textbox.Text = Me.subform.Form![Notes]
End Sub
I can access the subform fields AfterUpdate event within the scope of the subform but if I do that, then I can't access the Textbox because it is in the main form, not the subform.
So I either need a way to define an event function on a field in a subform from within the scope of the main form, or a way to make the textbox part of the subform while maintaining the subforms ability to open in datasheet view.
Not sure I get the whole problem (how does datasheet view come into play?), but this should do what you want:
(in the subform)
Private Sub Notes_AfterUpdate()
Me.Parent!Notes_Textbox.Value = Me![Notes].Value
End Sub
Edit from comment: By far the easiest solution would be a continuous form instead of a datasheet in a subform.
You can have two textboxes, a one-liner in the Details section, and a large one in the form header or footer. Both have the same control source and are automatically updated when the other one is edited. No code required.
If it must be a subform-datasheet, you can use a similar approach: bind the main form to the same recordsource, and in On Current of the subform, move the main form to the same current record.

How to have subforms load after the the main form in access 2010

I have an Access 2010 database, with document information in a primary table.
I have forms that display specific document type information (credit card statements, invoices, etc.) from the table.
I have a main form, with separate subforms, each subform representing a specific document type.
I want to be able to filter from the main form, so that each document type subform displays only the documents that fall within a user-specified dollar amount range.
I'm thinking, that if I can get the sub forms to open after the main form (the reverse of this is the default order), I can set the filter in the open event of each sub form, from the main form, and get my desired results.
What I have found to cause the sub forms to open after the main form, is to remove the SourceObject from the Data tab of the Properties sheet of the sub form; and then assign the sub form name to the SourceObject property in vba in the open event of the main form.
The example I have is Me.MySubForm.Form.SourceObject = "frmSubFormName", where everything on the left is verbatim, and frmSubFormName is the name of my subform.
This isn't getting past the compiler - it's complaining about MySubForm, and unfortunately the post/blog with the example doesn't indicate what "MySubForm" is, in assigning the SourceObject property to my sub form name.
Any thoughts on this approach to the filtering?
Can anyone shed light on the syntax of setting the SourceObject; or perhaps provide another way of having the sub forms load after the main form?
Thanks in advance.
Remove the Form class object reference. Recommend naming the subform container control different from the object it holds, such as ctrSomething
Me.ctrSomething.SourceObject = "frmSubFormName"

Can't set focus on my Subform in Access - error 2465 (can not find the field '|1' referred to in your expression)

Recently I am messing ( a lot) around with setting focus to my subform. For some reason I can't set the focus to my subform and it keeps giving me the error: can not find the field '|1' referred to in your expression I tried multiple ways to set the focus but all of them won't work.
What I tried so far:
Forms("frmArtikelSubInkoopHistorie").SetFocus (Set focus directly to the subform)
[Forms]![frmArtikelen].[frmArtikelSubInkoopHistorie].SetFocus (Set focus to the subform with reference to it's main form)
[Forms]![frmArtikelen].[frmArtikelSubInkoopHistorie].SetFocus
[Forms]![frmArtikelen].[frmArtikelSubInkoopHistorie].[Form].[Tekst33].SetFocus (First set focus to the subform itself and then setting focus to the subforms control)
First I tried to set the focus on the Load event of the main form but this caused the error as well.
Then I tried to set the focus in the load event of the subform itself but this event never gets fired.
Lastly I tried (and this is where I left off) to set the focus in the change event of my tab control (the subform resides in one of the tabs):
Private Sub TabbestEl91_Change()
On Error GoTo eri
Select Case TabbestEl91
Case 3 'Inkoop Historie
'Forms("frmArtikelSubInkoopHistorie").SetFocus
[Forms]![frmArtikelen].[frmArtikelSubInkoopHistorie].SetFocus
[Forms]![frmArtikelen].[frmArtikelSubInkoopHistorie].[Form].[Tekst33].SetFocus
End Select
eri:
MsgBox (Err.Number)
MsgBox (Err.description)
End Sub
I guess the focusing failing because the subform isn't loaded yet on the moment it sets the focus, this is just an assumption though and I thought this would be fixed if I would set the focus in the tab change (the subform should be loaded by then).
P.s. found multiple posts on this subject but non of them fixed my problem or were totally different than mine.
Any ideas to get this fixed? Thanks in advance!
I would try the following syntax:
Forms![frmArtikelen]![frmArtikelSubInkoopHistorie].Form![Tekst33].SetFocus
Note the exclamation marks that have been added. Also, it may sound counter intuitive but I believe that when a form loads, the subform loads before the mainform.
I don't understand at what point you actually want to apply the setfocus, in order to understand where the event should go.
Are you aware that you don't need VBA to select the tab index of the subform to 0? and then set the tab index within the subform so that tekst33 is 0?
I tried to set focus on my subform date after a new record is created.
Sequential code to set focus on the subform then the subform field might work for you:
from parent form setfocus on the subform first
Me.SF_DRL_Scheduling.SetFocus
then point to last record of the subform (even if filter is active)
DoCmd.GoToRecord , , acLast '
finally refer to the field in subform
Me.SF_DRL_Scheduling.Form.DrScDte.SetFocus
On Error Resume Next
'For second time doesn't run code below and raises error, so we need On Error Resume Next above. but with one more time else again run it code, there is ok runing all codes and process below
Me.frmArtikelSubInkoopHistorie.SetFocus
Forms![frmArtikelen]![frmArtikelSubInkoopHistorie].Form![Tekst33].SetFocus