Moving focus from Subfrm to Mainfrm and back?

caferacer

Registered User.
Local time
Today, 19:28
Joined
Oct 11, 2012
Messages
96
Hi All,

The code below is behind a combo box on a sub form.

I need this to change some formatting on parent form when certain changes are made to records in the subform to alert the user. The code for the formatting changes is in current event of the main form, so the code below is used to get the main form current event to run, but stay at the same parent record. Otherwise requery goes back to first record in the main form.

I have tried to get the formatting changes to work from the subfrm combo after update, but nothing happens? The requery is probably overkill, but is the closest thing I have found to reaching the solution.

It all works fine, however the focus remains on the parent form. What I would like is to return focus to the subform and to the record which was updated.

I have tried combining this with various different GoToRecord code I have found on the net, but with no success. The subform I am trying to return focus to is called [frm Vendors Primary].

Any ideas?

Many thanks.


Private Sub VendorID_AfterUpdate()

Dim frmMain As Form, sfrm As Form, hldMainID As Long
Set frmMain = Forms![frm Equipment Primary]
hldMainID = frmMain![EquipmentID]

frmMain.Requery
frmMain.Recordset.FindFirst "[EquipmentID] = " & hldMainID

Set frmMain = Nothing
 
Check this link for the correct syntax for referring to Forms, Sub-Forms and their controls from various relative locations.

Also, from memory, I think you will need to set the focus to the sub form holder before you then set the focus to a control on the subform.
 
Hi John,

I did find this link via another thread. After some more digging this morning I have found another variation of code (posted below for any others) which I adapted, which has finally worked - after two days of tinkering!!

Thanks for trying to help though, much appreciated.

Regards

Mark.

Private Sub VendorID_AfterUpdate()
Dim record As Integer
Dim ParentRecord As Integer
record = Me.CurrentRecord
ParentRecord = Me.Parent.CurrentRecord
Forms![frm Equipment Primary].Form.Requery
Forms![frm Equipment Primary].SetFocus
DoCmd.GoToRecord , , acGoTo, (ParentRecord)
Forms![frm Equipment Primary]![frm Vendors Secondary].Form.Requery
Forms![frm Equipment Primary]![frm Vendors Secondary].SetFocus
VendorID.SetFocus
DoCmd.GoToRecord , , acGoTo, (record)
End Sub
 
You can run code i another form on this way, (see attached picture), but the sub has to be public, (remove the word "Private").
Form name is "Formular1", the Sub name is "Form_Current"
Code:
Call Forms("Formular1").Form_Current


attachment.php
 

Attachments

  • Sub.jpg
    Sub.jpg
    22 KB · Views: 149
I did think about that, but wasn't sure how best to go about it. That is worth knowing as I have another problem elsewhere which may well benefit from this.
Many Thanks
 

Users who are viewing this thread

Back
Top Bottom