How to determine which subform has focus - vba

I have a Form that contains several subforms. On one of the subforms, I need to run an update query if you click off to another subform (you finished with the controls, if anything changed it must update). This part works great. The trouble I am having is I don't want to bog everything down by running the update (its a loop of update queries) every time you click on any subform.
If you are on a very specific subform (PageIndex 2) then it should run the updates. Below is the code I have to determine which tab/page/subform I am on. PlanSpecs is the name of the Page.
The problem is that no matter what tab I am current on, if always says "I'm on that subform." Any idea why that might be, or any suggestions as to what I can do instead?
If (Me.PlanSpecs.PageIndex = 2) Then
MsgBox ("I'm on that subform")
Else
MsgBox ("Different Subform, move along")
End If

I don't think you can refer to them by that index, unless they're in a tab control, then you'd have the PageIndex property.
You can however do it like this,
If (Screen.ActiveControl.Parent.Name = "SubFormNameHere") Then
MsgBox ("I'm on that subform")
Else
MsgBox ("Different Subform, move along")
End If
But this code may need additional conditions, for instance if you add a tab control to that subform, then it'll return the name of the TabControl and not the parent(form).
For additional reference

Related

find parent of a subform

I need to be able to tell what's opening an object where a control is sitting.
I have a procedure that refreshes comboboxes but I will never know whether the combobox is sitting on a form, a subform or inside another subform (2 levels of subforms)
Now, on opening of the form, I hardcode that info to a hidden control and it's passed to a procedure, but I was wondering if there's a way to do something like this, instead of me hardcoding.
it will be either or, depending on where the combobox is sitting
VMainForm = Me.Parent.Name
VMainForm = Me.Name
VMainForm = Me.Parent.Name '(? not even sure how to do this one,
'this would be where the combo is sitting inside another subform,
' so, there's Main!Subform1#Subform2)
Even better solution would be to get the full path to the subform.
So, if I'm on the 2nd subform, I'd get mainform!subform1!subform2
If I'm on a subform, I'd get mainform!subform1
If I'm on main, I'd get mainform.
And you didn't even try?
VMainFormName = Me.Parent.Name
This will, of course, fail if not a subform. So either catch that error or loop the Forms collection for having the form of the control.

Macro in form stops working after opened with vba

Please help me explain, why this happens, after that the solution should be easy :)
I have two forms, showing different data.
Form_1: there is a combo box (with names in it), where you can choose which company you wanna see, and an after-update macro searches the record (an [ID] field), and shows the information. (To be more complicated, this [ID] field is hidden, and used for subforms, where the actual infos appear.)
Form_2: this is a continuous form, each record is in connection with the companies shown in Form_1, but several record can belong to one company. There is a button for every record to open Form_1 with the information connected to it. The vba code of the button is:
Private Sub Button_Click()
DoCmd.OpenForm "Form_1", , , "[ID] = " & Me![ID]
End Sub
In the code, the same [ID] field is used, as described above: hidden and used for subforms.
Both forms are working as needed, I am happy with them.
But after Form_1 is opened from Form_2 with the button, the combo box remains empty (actually I don't need it to be filled), and if I wanna use it to search for other items, it doesn't work, as if the macro wasn't loaded. The list of names appear, I can click on any of them, but the [ID] field is not refreshed (and of course neither the subforms). I have to close the form, and open it again from the side-list.
Why does the macro stop working?
What should I change, to make it work?
Thanks for your help!
Form1 has the filter turned on to a specific key value, so attempts to find and reposition the form's current record will fail without explicitly resetting the filter.
The Where condition of the OpenForm command does not change the form's Record Source property, nor does it perform a simple search/reposition. Rather, it applies a form filter:
DoCmd.OpenForm "Form_1", , , "[ID] = " & Me![ID]
This state is indicated in several ways
On the Home ribbon (i.e. toolbar): The Toggle Filter button is active... is highlighted by a different color.
The form's navigation bar at the bottom of the form show "Filtered" highlighted with a little funnel icon.
The Access status bar shows "Filtered" on the right near other indicators.
Of course it's possible that all of those indicators are hidden, so you just need to be aware of what each command and parameter does.
Possible solutions:
Form1's ComboBox.AfterUpdate macro should turn off the filter before searching for a new ID value.
Form2's Button_Click event opens the form without applying a filter and instead runs code that does the same thing as the ComboBox.AfterUpdate method--searches and repositions the form's record rather than filtering it.
This can be achieved in multiple ways and is largely beyond the scope of this answer, but a hint is to make a Public method in the Form1 module that performs the search. Both the ComboBox.AfterUpdate method and the other button call that same public method so they have the same behavior.

MS Access will occasionally remove checkboxes from my Yes/No fields and replace them with 0 and -1

My new database is built using a variety of queries and tables. These tables and queries have two checkbox fields that are set for "Test" and "Approved". The way that these come into play is that I have buttons on one form called "ApprovalForm" that open up another form called "ViewForm". The goal was that "ViewForm would change its subform's source object every time one of these buttons is clicked. ApprovalForm closes itself first, then ViewForm opens, then Subform.SourceObject is updated to the query specified by whatever button was clicked.
Private Sub View_NonSize_APL_Click()
On Error GoTo View_NonSize_APL_Click_Err
DoCmd.Close acForm, ("ApprovalForm")
DoCmd.OpenForm ("ViewForm")
Forms!ViewForm!View_Subform.SourceObject = "Query.NonSize - APL View"
Forms!ViewForm!View_Subform.Form.Requery
Forms!ViewForm.Form.Requery
Forms.ViewForm.Caption = "NonSize - APL View"
View_NonSize_APL_Click_Exit:
Exit Sub
View_NonSize_APL_Click_Err:
MsgBox Error$
Resume View_NonSize_APL_Click_Exit
End Sub
With the process finished, the final result should be that the fields are populated by the query, which is looking at a table off on the side. The user has no access to the table itself, just that specific select query. However, in rare occasions, the checkboxes will not load correctly, instead displaying as 0s and -1s. What is peculiar about this is that when you click the button on "ViewForm" that leads back to "ApprovalForm" and click the same exact button AGAIN, the checkboxes load correctly.
My guess is that it has something to do with how "ViewForm" loads in, and due to it being a business project, the computer that it is working on is less than optimal. I tried to remedy the situation by using "Requery" and "Refresh" in the code to hopefully reload the process. I have also tried the cumbersome route of having the form close after being loaded and immediately reloading again. (Open the form, load its query, close the form, then open the form again.) That actually proved to ruin the entire process.
Is there anything I can do to counter this problem?

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

Select next record on datasheet subform from separate form?

I have 2 forms. 1 subform.
The main form, MainFormF, has a subform which is hooked up to a query that takes data from a table and outputs it into a datasheet. Lets call this subform MainSubformF.
The 2nd form is loaded from a button on MainFormF. It has 2 buttons, previous and next. How do I attach these buttons to switch the next/previous record HIGHLIGHTED in the datasheet subform?
As you can see, that is what it looks like highlighted. It turns blue, if you didn't know.
The code I currently use but doesn't switch the selected records at all is this:
Private Sub Command65_Click()
On Error GoTo new_Err
Forms!MainformF!MainSubformF.SetFocus 'sets the focus to MainSubformF
DoCmd.RunCommand acCmdRecordsGoToNew
new_Err:
End Sub
For the button to open up the More Info form, it takes information from a text box (lets name it InfoTxt) with this as it's control source:
=[MainSubformF].[Form]![ProjectID]
I am thinking I can do something with that variable, like add +1 to it so when the button is clicked it adds +1 to the ProjectID? I don't think that will work now that I am typing this out, but I'll keep this here in case I am right... though I am thinking further into it and ProjectID's aren't always +1, as in case a record was deleted it may go 1,2,3,5,6 and if you get to 3 then click next it'll do nothing as it'll go to 4 not 5.
I simply don't have time to write out a complete answer, but Dev Ashish is a well-respected developer in the Access community and he devised a way to determine which records have been selected. My guess is you'll need to bookmark them somehow. Start with this Sub that Dev wrote:
Determine selected records in datasheet view