Solved Very odd caret and text box behavior (1 Viewer)

EdFred

knows enough, dangerous
Local time
Today, 05:06
Joined
Mar 2, 2007
Messages
129
I have a form with 2 Combo and 1 text box. It sometimes requires repetitive mostly-but-not-quite-duplicate entries so I wrote some vba code so I don't have to manually key in the same info over and over (sometimes up to 10 times, but often it's 1 and done)

When I manually key in certain information, to the first two ComboBoxes it writes a prefix to the LotNumber text box and moves the caret to after the prefix. This it does. Never had an issue.


Non-pertinent code <np code> removed from the code snippets below


Code:
Public Sub writePrefix()
If Condition 1 Then
    <np code removed >
    With Me.LotNumber
        .Value = strPrefix
        .SetFocus
        Me.LotNumber.SelStart = len(strPrefix)
    End With
End If
End Sub

Code:
Private Sub Invoice_Number_AfterUpdate()
<np code removed>
If condition 1 Then
    <np code removed Autofills Product_ID >
    writePrefix
End If
End Sub
This code above works as it should - writes the prefix to LotNumber and the caret is waiting at the end of the text entry

Code:
Private Sub Product_ID_Exit(Cancel As Integer)
<np code removed - re-enters information from previous record if empty>
writePrefix
End Sub
This code above works as it should - writes the prefix to LotNumber and the caret is waiting at the end of the text entry


Code:
Private Sub Invoice_Number_Exit(Cancel As Integer)
If condition 1 Then
    <np code removed checks if previous record is empty>
    If condition 2 Then
        <np code removed writes previous record if not empty>
        If condition 3 Then
            <np code removed Autofills Product_ID>
            writePrefix
        End If
    End If
End If
End Sub

This code is what is causing much cursing and searching with no solution. Everything works right up until the End Sub. I put a 2 second delay/timer and a MsgBox with before the End Sub to test. While the timer is ticking and before the message box triggers the caret is blinking at the end the box after the prefix like it is supposed to. As soon as the End Sub is triggered it selects what is written in the text box and moves the carat to position 0. So as soon as I start manually entering information it overwrites the prefix.

I've moved the Public Sub to the on Enter for the LotNumber text box. No Joy.
I've done Exit Sub right after calling writePrefix. No Joy.
I've done more iterations of moving code around, but have zero solution.
I've moved to another focus, done delays, moved it back, and I always get the same result - highlighted text that get overwritten when I manually enter.


Anyone ever run into this and if so, have a solution?
 

jdraw

Super Moderator
Staff member
Local time
Today, 05:06
Joined
Jan 23, 2006
Messages
15,379
I recommend you post a copy of your database or a part of the database showing the issue so readers can review it.
You might also tell us in simple, plain English what the related business process(es) is/are and the significance of the
"repetitive mostly-but-not-quite-duplicate entries".
 

EdFred

knows enough, dangerous
Local time
Today, 05:06
Joined
Mar 2, 2007
Messages
129
Way too much sensitive information in tables that are being referenced to post even part of it.

Plain English Process:
I have lot tracing that is required by many of my customers. And in my field, the customers always push that responsibility off on suppliers rather than do it themselves. This handles that in this way:

I get the retained packing slips from shipping that have the lot numbers for certain products written on them after the product is pulled and shipped out. I enter/document the following information in the following tab order:
Invoice Number
Product ID
Lot Number

Most entry consists of 1 Product ID per Invoice and 1 Lot Number per Product ID. I used to have it so my keystrokes would be:
12345 [TAB] Product1 [TAB] AB24681012 [TAB] <move to new record>
12346 [TAB] Product2 [TAB] CD5555 [TAB] <move to new record>
12347 [TAB] Product3 [TAB] EF9876 [TAB] <move to new record>
etc...
No vba other than a requery of product ID after updating Invoice Number

I got tired of moving back and forth between keyboard and number pad
So I wrote a small bit of code that auto filled the Product and the AB/CD/EF got written and the caret was waiting for me to enter 24681012 and it made it so my key entry would be:

12345 [TAB] 24681012 [TAB] <move to new record>
12346 [TAB] 5555 [TAB] <move to new record>
12347 [TAB] 9876 [TAB] <move to new record>

This worked great, and still does.

Then I would get the crazy invoice that would have multiple lot numbers per product
So rather than my key entry be

12345 [TAB] 12345678 [TAB] <move to new record>
12345 [TAB] 12358132 [TAB] <move to new record>
12345 [TAB] 12468101 [TAB] <move to new record>
12345 [TAB] 12481632 [TAB] <move to new record>
etc...

I wrote the last bit of code so my key entry was SUPPOSED to be

12345 [TAB] 12345678 [TAB] <move to new record>
[TAB] 12358132 [TAB] <move to new record>
[TAB] 12468101 [TAB] <move to new record>
[TAB] 12481632 [TAB] <move to new record>

12345 and Product1 does get autofilled when I exit an empty Invoice Number Field when I hit the first tab on the new record. And it does write the AB/CD/EF in the LotNumber text box, but when the End or Exit Sub line hits for
Private Sub Invoice_Number_Exit(Cancel As Integer)

the caret moves to the beginning of the LotNumber text box and rather than seeing
AB|

I see

|AB

and the AB is highlighted, so any key entry overwrites it.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:06
Joined
Sep 21, 2011
Messages
14,301
Easy enough to obfuscate the data, plus only a subset is required to see the problem.?

@moke123 Could you post your DB here please, so I do not have to keep referencing another site? :)
Sample DB sub forum would be good?, don't you think?
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:06
Joined
Sep 21, 2011
Messages
14,301
Anything to do with this?
1692297546046.png
 

moke123

AWF VIP
Local time
Today, 05:06
Joined
Jan 11, 2013
Messages
3,920
Could you post your DB here please, so I do not have to keep referencing another site?
this one? It's probably on here somewhere.
 

Attachments

  • DataRandomizer.accdb
    436 KB · Views: 67

EdFred

knows enough, dangerous
Local time
Today, 05:06
Joined
Mar 2, 2007
Messages
129
Anything to do with this?
View attachment 109496

I was about to stab myself if that was the case.

I will see what I can do to minimize the amount of forms/tables/queries/other code that's needed to run this.
For now, I'm just dealing with an extra tab. It will selstart to the end of product ID no problem, and then running the writePrefix OnEnter of Lot Number so that's what I'm doing for now
 

jdraw

Super Moderator
Staff member
Local time
Today, 05:06
Joined
Jan 23, 2006
Messages
15,379
1 Lot Number per Product ID is the rule BUT
you can get/have multiple lot numbers per product

This seems to be a logic error.
Glad to see that the randomizer was suggested.

Perhaps you could mock-up a sample. We don't need the whole database, nor the actual data. Just some data and form with some instructions.
You have shown rec1, rec2 and rec3 with AB/CD/EF prefix on Lot number.
Do these repeat or is the next Lot Number prefix GH?

Does any of the other responders understand the issue? Sufficient to layout some process logic?
 

EdFred

knows enough, dangerous
Local time
Today, 05:06
Joined
Mar 2, 2007
Messages
129
1 Lot Number per Product ID is the rule BUT
you can get/have multiple lot numbers per product

This seems to be a logic error

How is that a logic error? A customer orders 50 of a Product ID. The material I have here was manufactured at maybe 3 different times. 3 different lot numbers.

Customer gets 50 units.
12 units of lot 1234
25 units of lot 3456
13 units of lot 7890

All the same product ID all the same invoice number.
 

jdraw

Super Moderator
Staff member
Local time
Today, 05:06
Joined
Jan 23, 2006
Messages
15,379
"Most entry consists of 1 Product ID per Invoice and 1 Lot Number per Product ID. I used to have it so my keystrokes would be:

12345 [TAB] Product1 [TAB] AB24681012 [TAB] <move to new record>
12346 [TAB] Product2 [TAB] CD5555 [TAB]<move to new record>
12347 [TAB] Product3 [TAB] EF9876 [TAB] <move to new record>
etc...
"
So the 12345,12346... are not Invoices, but Product ID.

I saw your rule:
"1 Product ID per Invoice and 1 Lot Number per Product ID."

But I did not see anything saying Multiple Products per Invoice.
 

Mike Krailo

Well-known member
Local time
Today, 05:06
Joined
Mar 28, 2020
Messages
1,044
To me this is a caret? ^
This is my version of a carrot, maybe 3: 💎

@EdFred
OK, we know what you mean though, that's called the cursor BTW.

Just place this command in your On Enter for the LotNumber text box.
Code:
Private Sub LotNumber_Enter()
   SendKeys "{F2}"
End Sub
 

EdFred

knows enough, dangerous
Local time
Today, 05:06
Joined
Mar 2, 2007
Messages
129
Sorry on going dark, 3 day weekend.

To answer questions and what I've found:
It seems it only gives the issue I am having when it's run from the OnExit Subs, running it from AfterUpdate works as it should.
The SendKeys "{F2}" sort of solved this problem when I put it in the OnExit Subs, except it caused the NumLock being turned off bug that seems to be prevalent.
Putting SendKeys "{F2}" in the OnEnter would cause it to highlight the whole field when the code ran from AfterUpdate, but do it correctly from the OnExit, but still got the NumLock bug

There can be multiple product ids on a single invoice number
There can be multiple lot numbers per product ids (dependent on what is written on the paperwork, sometimes up to 6, but in theory infinite)
There can be multiple lot numbers per product ID and multiple products per invoice

Oh, and one last thing, I am running this on 2007, yes, 2007. Until it absolutely doesn't work, we aren't upgrading to 365, and early retirement is on the horizon. So maybe this bug won't happen in newer versions - don't have 'em, haven't tested.


(removed screenshots and database attachment)
 
Last edited:

Mike Krailo

Well-known member
Local time
Today, 05:06
Joined
Mar 28, 2020
Messages
1,044
The SendKeys "{F2}" sort of solved this problem when I put it in the OnExit Subs, except it caused the NumLock being turned off bug that seems to be prevalent.
After the sendkeys command to press F2, you need to test if the numlock key value is false and if so, turn it back on again. I found this stackoverflow solution for the Numlock issue HERE. Scroll down to the advice given by user SendETHToThisAddress and see if that works for you.
 

EdFred

knows enough, dangerous
Local time
Today, 05:06
Joined
Mar 2, 2007
Messages
129
After the sendkeys command to press F2, you need to test if the numlock key value is false and if so, turn it back on again. I found this stackoverflow solution for the Numlock issue HERE. Scroll down to the advice given by user SendETHToThisAddress and see if that works for you.
I used
Code:
Set WshShell = CreateObject("WScript.Shell")
            WshShell.SendKeys "{F2}", True

I put it in the OnExit subs for invoice number and product id, and this solves the issue.
Cursor to end
No Numlock change.

Code I ran into as a solution for the numlock/sendkey was way more than I wanted to look into.

Thank you!
 

EdFred

knows enough, dangerous
Local time
Today, 05:06
Joined
Mar 2, 2007
Messages
129
Great job getting it to work for you.
Thank you for directing me to that other thread. It did not show up in my searches for the SendKeys issue. The solutions I found involved a bunch of API calls and such.
 

Users who are viewing this thread

Top Bottom