Set focus not working (1 Viewer)

john_gringo

Registered User.
Local time
Today, 09:42
Joined
Nov 1, 2011
Messages
87
Hi I have a form with some textboxes and a subform. I use a text box to enter a number and when press enter I need to run some code and then return to the same textbox. Anything I try I am not able to do so. It is always put the cursor to another text box. I update the properties of all text boxes to TabStop = no but didn't solve the issue.
I refresh the form and after that set the focus to the text box but still nothing.
I use the after update, onenter, onexit and other methods but not able to solve the issue.

Any help would be greatly appreciated
Thanks
John
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:42
Joined
Oct 29, 2018
Messages
21,473
Just a thought but you might end up using a timer for this one.
 

Mike Krailo

Well-known member
Local time
Today, 02:42
Joined
Mar 28, 2020
Messages
1,044
Anything I try I am not able to do so. It is always put the cursor to another text box.
This could be a timing issue where the code you are running takes more time to run. You could verify this by simply stepping through the code in debug mode. Just open the VBE window and put a stop on the code just before the set focus line. If everything works when stepping through or delaying the set focus line of code, then you need to ensure the code completes first before issuing the setfocus. You can also post the code in question to allow us to see what you are doing.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:42
Joined
May 21, 2018
Messages
8,529
you may also try adding DoEvents prior to the focus call.
 

john_gringo

Registered User.
Local time
Today, 09:42
Joined
Nov 1, 2011
Messages
87
Code:
Private Sub TextScaner_AfterUpdate()
    Dim StrSql As String
    Dim criteria As String
    Dim recordCount As Long

    ' Build the criteria string
    criteria = "([IDPARHEAD] = " & Forms![OrdersOpen]![ParheadID] & ") AND " & _
               "([STR1] = '" & Forms![OrdersOpen]![TextScaner] & "') AND " & _
               "([INT1] Is Null)"

    ' Use DCount to count records
    recordCount = DCount("*", "dbo_PARITEMS", criteria)

    ' Display or use the record count as needed
   ' MsgBox "Number of records: " & recordCount

    StrSql = "UPDATE dbo_PARITEMS SET dbo_PARITEMS.INT1 =  1 WHERE (((dbo_PARITEMS.IDPARHEAD)=[Forms]![OrdersOpen]![ParheadID]) AND ((dbo_PARITEMS.STR1)=[Forms]![OrdersOpen]![TextScaner]) AND ((dbo_PARITEMS.INT1) Is Null));"

    DoCmd.SetWarnings False
    DoCmd.RunSQL StrSql
    DoCmd.SetWarnings True

    Me.TextScaner = ""
    Me.Refresh

    ' Set focus back to TextScaner
    Forms!OrdersOpen![TextScaner].SetFocus
    DoCmd.CancelEvent ' Prevents further processing of the event
End Sub
 

john_gringo

Registered User.
Local time
Today, 09:42
Joined
Nov 1, 2011
Messages
87
This is my code.
I tried to stop the code but the results were the same.
I found a solution, don'tknow if it is correct but seems to work.
I set all other objects' Tab Stop property to No.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:42
Joined
Oct 29, 2018
Messages
21,473
This is my code.
I tried to stop the code but the results were the same.
I found a solution, don'tknow if it is correct but seems to work.
I set all other objects' Tab Stop property to No.
Looks like maybe you're using the textbox as a scratch pad and it's probably not bound to a field in your table. If so, I wonder if using the Before Update event would work better for you.
 

john_gringo

Registered User.
Local time
Today, 09:42
Joined
Nov 1, 2011
Messages
87
You are correct. It is not bound to anything. I am entering a barcode value with a scanner and updating my record accordingly.
Then I need to return to the same textbox, to scan for the next product.
I was wondering if is better to use a combo box instead.
I need a form to be opened with a subform with the products that need to be sent and scan its product barcode to update its quantity value.
 

Mike Krailo

Well-known member
Local time
Today, 02:42
Joined
Mar 28, 2020
Messages
1,044
I set all other objects' Tab Stop property to No.
If that's what you want and there is no other fields to be updated, then that's good. Your Forms!OrdersOpen![TextScaner].SetFocus line of code will execute but move to the next control when the after update event triggers. So you would have to do what you already did, or use a LostFocus event to force it back to the TextScaner box. Another way is to make a Clear button besides the TextScaner box and put the SetFocus line of code in the OnClick event. The advantage of the button is that you can clear the contents of the TextScaner box or clear the any filters you have on before executing the click or keyboard hot key to do the clear. It doesn't look like you are using filters though.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:42
Joined
May 7, 2009
Messages
19,243
use TimerEvent of your form.
run Form1 (open the VBA to see the code)
 

Attachments

  • scanner.accdb
    544 KB · Views: 43

apr pillai

AWF VIP
Local time
Today, 12:12
Joined
Jan 20, 2005
Messages
735
I think the Exit Event (Sub TextScanner_Exit(Cancel As Integer)) will work. At the end of the Process Set the Cancel Parameter to True ( Cancel = True). The Focus will stay in the TextScanner TextBox.
 

Users who are viewing this thread

Top Bottom