How to always set focus on the control on parent form after entering data in the subform (4 Viewers)

nectorch

Member
Local time
Today, 17:40
Joined
Aug 4, 2021
Messages
61
Hi

I thought this was an easy task, but has proved not. The people scan the barcode on the parent form on the control called txtProductCode that works fine no issues data is transfered to the subform, but the second scan for the next line item the cursor does not set focus on txtProductCode users have to click the control txtProductCodesor for the scanner to scan in the right box. Is there a way to make sure that the cursor is always set focus on txtProductCode.

I have tried the following nothing is working

form current event

Me.txtProductCode.setfus

It does not work
 
> data is transferred to the subform
Please show us that code. It appears that this code is setting focus away from the parent form.
 
Code:
Public Sub txtProductCode_AfterUpdate()
Dim lngProdID As Long
Dim sReturn$, varValue As Variant
    If Not (IsNull(mID)) Then
        'DoCmd.GoToControl "sfrmPosLineDetails Subform"
              
        With Me![sfrmPosLineDetails Subform].Form
            .Recordset.AddNew
            
            ![ProductID] = mID
            ![QtySold] = 1
            
            sReturn = DLookup("ProductName & '|' & vatCatCd & '|' & dftPrc & '|' & VAT", _
                            "tblProducts", _
                            "BarCode = '" & Me.txtProductCode & "'")
            varValue = Split(sReturn, "|")
            
            ![ProductName] = varValue(0)
            ![TaxClassA] = varValue(1)
            ![SellingPrice] = varValue(2)
            ![Tax] = varValue(3)
                      
            ![RRP] = 0
            
            If Me.Dirty = True Then
        ' Save the current record
        Me.Dirty = False
    End If
        End With
    'DoCmd.GoToRecord , , acNewRec
        
    End If
    Me.sfrmPosLineDetails_Subform.Requery
      
    Me.txtProductCode = Null
    Me.txtProductCode.SetFocus
End Sub
 
The set focus on that control in the after update event didn't work because the subform became active when your code executed With Me![sfrmPosLineDetails Subform].Form
 
Last edited:
Try changing:
Me.txtProductCode.SetFocus
To
Docmd.GotoControl "txtProductCode"
At the end of the procedure.
 
Me.Parent.txtProductCode.SetFocus
That is not going to work since this code is obviously being called from the main form and in fact from txtProductCode
Code:
txtProductCode_AfterUpdate()
 
That is not going to work since this code is obviously being called from the main form and in fact from txtProductCode
Code:
txtProductCode_AfterUpdate()
So then how about making the subform active and setting focus on the desired control?
 
As far as I can tell TxtProductCode is on the main form so why would they set focus first to the subform?
My guess is that this after update event is not even firing. The OP needs to test. If the OP is using a barcode reader to update txtProductCode the afterupdate never fires. After update events do not fire when the control is updated via code vice user action.

I don't understand why OP cannot set focus back on the barcode control in the main form after scanning a barcode. I have a form that opens a document in an external web browser and I am abble to set focus back on a date control in the calling form immediately after the document opens with

Forms!frmPopupAddDocument!DateOfService.SetFocus statement.

See that instruction at the end of the second sub:

Code:
'------------------------------------------------------------

'This sub opens the file picker

Private Sub cmdGetFile_Click()

 Dim fDialog As Object
 Dim varFile As Variant
 Dim S As String
 
   'Set up the File Dialog.
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    fDialog.InitialFileName = Environ("USERPROFILE") & "\Documents\"
    
    fDialog.AllowMultiSelect = False
  
   'Show the dialog box. If the .Show method returns True, the
    'user picked at least one file. If the .Show method returns
    'False, the user clicked Cancel.
    If fDialog.Show = True Then
        SelectedFileName = fDialog.SelectedItems(1)
    Else
       MsgBox "You clicked Cancel in the file dialog box."
    
    End If

'used to open file
    If Not IsNull(Me.SelectedFileName) Then
      Call OpenFileBringToFront(Me.SelectedFileName)
    End If
  
End Sub

'--------------------------------------------------------------

'This sub opens the document the user selected with the file picker and sets focus back on the DocumentDateOfService control in the calling form.

Public Sub OpenFileBringToFront(FilePath As String)
    Dim ret As LongPtr
    Dim winTitle As String
    Dim waitTime As Single
  
    If Len(Dir(FilePath)) = 0 Then
        MsgBox "File not found: " & FilePath, vbExclamation
        Exit Sub
    End If
  
    ' Open the file with its default application
    ret = ShellExecute(0, "open", FilePath, vbNullString, vbNullString, 1)
  
    ' Pause ~2 seconds (replacement for Application.Wait)
    waitTime = Timer + 2
    Do While Timer < waitTime
        DoEvents
    Loop
  
    ' Try to activate the app window based on the filename
    winTitle = Dir(FilePath)   ' e.g. "Report.pdf"
    On Error Resume Next
    AppActivate winTitle
    On Error GoTo 0
  
    Forms!frmPopupAddDocument!DateOfService.SetFocus

End Sub

ReturnFocusToDateControl1.PNG
 
Last edited:
My guess is that this after update event is not even firing. The OP needs to test. If the OP is using a barcode reader to update txtProductCode the afterupdate never fires. After update events do not fire when the control is updated via code vice user action.

That would be my natural conclusion too, but they say 'The people scan the barcode on the parent form on the control called txtProductCode that works fine no issues data is transfered to the subform', which suggests that the AfterUpdate event procedure's code is being executed. What surprises me is that the event procedure is being executed for the first scan, not that it is apparently not for subsequent scans.

Clearly they should set a breakpoint at the top of the procedure and step into the code to see what is being executed, and what isn't.
 
What surprises me is that the event procedure is being executed for the first scan, not that it is apparently not for subsequent scans.
That is a mistake on my part. The scanner emulates the keyboard so the after update does fire. I think that is the standard way to do it.
 
My next guess is that the focus is first set and then something causes the focus to move away. Such as a pending event or pending code. To test

Code:
Me.txtProductCode = Null
DoEvents
Me.txtProductCode.SetFocus
Msgbox "Check to see if focus is set"

One thing that can cause the main form to immediately lose focus is when a subfom is set to allowadditions = false and there are no current records in the subform. See discussion

That issue will drive you nuts because the active control executes the code but then says it is not the active control.

If your subform has allow additions to false, try your code with allow additions to true. Then if that works and you need to simulate "disallow additions " then just lock all the controls.
 
That issue will drive you nuts because the active control executes the code but then says it is not the active control.
If your subform has allow additions to false, try your code with allow additions to true. Then if that works and you need to simulate "disallow additions " then just lock all the controls.

Why jump through all those hoops when all that needs to be done is to set focus on the control in the main form with:

Code:
Private Sub YourSubformControl_AfterUpdate()
   Forms!frmMain!txtProductCode.SetFocus
End Sub
 
Why jump through all those hoops when all that needs to be done is to set focus on the control in the main form with:

Code:
Private Sub YourSubformControl_AfterUpdate()
   Forms!frmMain!txtProductCode.SetFocus
End Sub
I’m not sure either if that event fires if the subform’s underlying record source is updated through code.
 
Why jump through all those hoops when all that needs to be done is to set focus on the control in the main form with:
Does not makes any sense. We must be reading two different posts. You need to look at what the OP posted. There is no "YouSubFormControl" involved. His barcode reader is updating the main form txtProductCode which is then updating the subform.
What makes you believe that this is being called from the subform? The OP did not state that? The code does not suggest that?
 
Does not makes any sense. We must be reading two different posts. You need to look at what the OP posted. There is no "YouSubFormControl" involved.
That was a sample placeholder name for the OP's whatever subform name he used.
His barcode reader is updating the main form txtProductCode which is then updating the subform...What makes you believe that this is being called from the subform? The OP did not state that? The code does not suggest that?
Right, and by updating the subform it becomes active.

Why do you have to be so rude in your replies?
 
I’m not sure either if that event fires if the subform’s underlying record source is updated through code.
So then why doesn't set focus or DoCmd.GoToControl statement in the main form set focus on Me.txtProductCode?
In my Post #10, In my form I opened a file in an external web browser and had no problem immediately setting focus on a text control to continue entering data.
I think his subform became active when his parent form code executed 'With Me![sfrmPosLineDetails Subform].Form'.
 
Last edited:
So then why doesn't set focus or DoCmd.GoToControl statement in the main form set focus on Me.txtProductCode?
In my Post #10, In my form I opened a file in an external web browser and had no problem immediately setting focus on a text control to continue entering data.
Don’t think I can answer that question without being able to step through the OP’s code, so a demo file would probably help us figure out the actual cause of the problem.
 
Don’t think I can answer that question without being able to step through the OP’s code, so a demo file would probably help us figure out the actual cause of the problem.
I think his subform became active when his parent form code executed 'With Me![sfrmPosLineDetails Subform].Form'.
 
That was a sample placeholder name for the OP's whatever subform name he used
Thats the whole point. You seem unwilling to read the OP's post, or I do not know where you are coming up with this. There is no event being called from the subform. No such thing exist regardless of any placeholder name (I get that).
Code:
Private Sub YourSubformControl_AfterUpdate()

Clearly the OP shows this
Code:
Public Sub txtProductCode_AfterUpdate()
That clearly means the code is being called from the main form from txtProductCode

Why do you have to be so rude in your replies
I do not know. But when people try to correct me after I have provided the OP a sound idea and they provide a sensical counterpoint I am all for it. When they instead provide nonsense, i find it super annoying.
Why jump through all those hoops when all that needs to be done is to set focus on the control in the main form with

Let me make it easy for you by simplifying the code
1. The bar code reader triggers the after update in the main form
Code:
Public Sub txtProductCode_AfterUpdate()
    'code
    Me.sfrmPosLineDetails_Subform.Requery
    'code
    Me.txtProductCode.SetFocus  '(Not working as desired)
End Sub
2. The OP tries to set the focus to txtProductCode (in the main form and called from the main form)
3. For some reason the focus fails to set or my guess it gets set and then moves off

Notice there is no code being called from the subform.
Yes we all know that this should be possible.
So then why doesn't set focus or DoCmd.GoToControl statement in the main form set focus on Me.txtProductCode?
If the OP knew this, then they would not be here asking why it does not work as expected.
 

Users who are viewing this thread

  • Back
    Top Bottom