Error appears, when event On Current is triggered - vba

When I reference to subform error occurs. I have 1 main form and 2 subforms on the main one. One of subform has On Current event which trigger code:
Me.Parent![1ChildEquipFilter].Form.RecordSource = StrSQL
(it changes second subform's RecordSource)
When the main form is opened the first time, the error message appears
'you entered an expression that has an invalid reference to the property form/report'.
But when I click on the debug and then reset, all work properly. What's the matter?

When you open the form, first the two subforms are opened, then the main form, and then the two subforms again.
The simple workaround is to eat the error:
On Error Resume Next
Me.Parent![1ChildEquipFilter].Form.RecordSource = StrSQL
On Error GoTo 0

Related

Open Access form with pointer on record

I have a form with a tabular subform and want to set the record pointer to be on a particular record (on or near current date) and this record highlighted.
Following code is used to set the pointer after the desired record is found:
DoCmd.RunCommand acCmdSelectRecord
Unfortunately, this works only when the form/subform is already open: then the record marker is highlighted as desired.
HOWEVER, when the same code is run from Form_current() upon opening the form, the code line runs (I checked this !) but has no effect at all: when the form displays, THE HIGHLIGHT IS ON THE FIRST FIELD (in the subform) !
Similarly, the attempt to deselect the field contents works only after the form is already open, but not upon opening the form :
With Forms![MyForm]![MySubform]![MyHighlightedField]
On Error Resume Next
.SelStart = 0
.SelLength = 0
On Error GoTo 0
End With
Is there any way to enforce the desired record marker to be highlighted on opening the form/subform ?

Form button intermittently returns runtime error 2101

I have a simple form on which the user selects a value from a list in a combo box, then clicks a button to open another form filtered by the selection. In development and testing it works fine, but in Prod users are occasionally receiving runtime error 2101: 'The setting you entered isn't valid for this property'. If the user restarts their machine the error doesn't persist, at least for a while.
This happens when the user clicks the 'Ok' button, which closes the selection form and opens the main interface form. The code for the button is just:
Private Sub btnOK_Click()
DoCmd.OpenForm "CC_Tracker_from_form", acNormal, , , acFormEdit
DoCmd.CLOSE acForm, "frmCoord_Selector"
End Sub
When the user closes the error popup, the selection form remains visible on top of the main interface, which sort of makes sense if the failure is in the DoCmd.CLOSE line, since Access would have already opened the main form.
Why would error 2101 trigger only some of the time, when the user performs exactly the same action (even the same selection from the combo box)?
I don't think this error has anything to do with the underlying Record Source for the main form, but just in case here's that code:
SELECT
[Bunch of columns],
IIf(dbo_CC_Tracker.RISK_LVL='Low',Null,dbo_CC_Tracker.CHRA+365) AS CHRA_Next,
IIf(dbo_CC_Tracker.RISK_LVL='High',dbo_CC_Tracker.[ICP/Review]+29,
IIf(dbo_CC_Tracker.RISK_LVL='Medium',dbo_CC_Tracker.[ICP/Review]+89,Null)) AS ICP_Next,
IIf((dbo_CC_Tracker.RISK_LVL='Low' Or dbo_CC_Tracker.RISK_LVL='Medium'),Null,dbo_CC_Tracker.F2F+179) AS F2F_Next,
IIf(dbo_CC_Tracker.RISK_LVL='Low',Null,dbo_CC_Tracker.ICTCont+89) AS ICT_Next,
dbo_CC_Tracker.HTR_Letter +30 AS Final_Follow,
dbo_CC_Tracker.ASSIGNED +59 AS Deadline_1,
dbo_CC_Tracker.ASSIGNED +89 AS Deadline_2
FROM dbo_CC_Tracker
WHERE
(((dbo_CC_Tracker.ASSIGNED_CARE_COORDINATOR)=Forms!frmCoord_Selector!cmbCoords)
And dbo_CC_Tracker.[CLOSE] is null)
Or Forms!frmCoord_Selector!cmbCoords Is Null;
It's possible that for some reason the form you are trying to close hasn't been able to save any updates.
I would make sure that if the form has any data changed it is saved, so maybe add
If Me.Dirty Then Me.Dirty = False
To force a save before closing the existing form.
Based on the idea that the first form may be closing before passing the dropdown selection to the second form I've changed from closing the form, DoCmd.CLOSE acForm, "frmCoord_Selector", to hiding the form, Me.Visible = False which should keep the selection active despite the first form no longer displaying.

How to requery form opened by another user

this may seem like an odd question, but I am wanting to requery a form that is opened by another user. Basically I have two different main menus, that pertain to the role of the employee. On these main forms I display a menu with the counts of clients in each status. I then have a main form that holds the clients data. After I update the status field I am requery both forms. Now this requerys the menu that is open on the current user that changed the status, but it doesn't requery the other menu that is opened by the other user unless they close the menu and reopen. Is this possible to requery another subform of another user's form.
Here is my code below:
Private Sub status_ID_AfterUpdate()
On Error GoTo Problems
DoCmd.RunCommand acCmdSaveRecord
Forms!frmNursesMenu!frmStatusCount_Nurses.Form.Requery
Forms!frmNursesMenu!frmStatusCount_Nurses.Form.Repaint
Forms!frmNursesMenu.Requery
Forms!frmNursesMenu.Repaint
Forms!frmAdminMenu!frmStatusCount.Form.Requery
Forms!frmAdminMenu!frmStatusCount.Form.Repaint
Forms!frmAdminMenu.Requery
Forms!frmAdminMenu.Repaint
Exit Sub
Problems:
Err.Clear
Resume Next
End Sub
You will need to use the form's timer event to requery the form every so often or look to see if the form needs a requery.
If your tables include a column with the time of the last edit then you could use that to see if the form needs a requery.

Delete multiple items from a ToolStrip

I have make a ToolStrip works like windows task bar. When user opens a new form, a label with form's icon appears on ToolStrip as new label item. I also have a button that closes all forms at once. But I want to remove every relevant label too, so I added this into the click event...
For Each mdichildlabel As ToolStripLabel In Me.BottomToolStrip.Items
If mdichildlabel.Name = "NewLabel" Then
BottomToolStrip.Items.Remove(mdichildlabel)
End If
Next
But I get this error: An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Doesn't work doesn't tell us what you are seeing that is not working.
Generally, you can't modify a collection while for-eaching it because of the changing index positions. Try iterating over it backwards:
For i As Integer = Me.BottomToolStrip.Items.Count - 1 to 0 Step -1
If Me.BottomToolStrip.Items(i).Name = "NewLabel" Then
Me.BottomToolStrip.Items.RemoveAt(i)
End If
Next

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