Weird GotFocus and .SelStart bug. (1 Viewer)

Royce

Access Developer
Local time
Today, 08:41
Joined
Nov 8, 2012
Messages
99
The following code always works if I set a break point, and traces through as I would expect it to.

It never works the first, and only the first, time I click on the "BudgetYear" control IF the record is a new record.

Anybody seen this? Am I missing something?
Code:
Private Sub BudgetYear_GotFocus()
    If Me.NewRecord Then
        Me.BudgetYear.SelStart = 0
        Me.BudgetYear.SelLength = 0
        
    ElseIf Nz(Me.BudgetYear, vbNullString) = vbNullString Then
        Me.BudgetYear.SelStart = 0
    Else
        Me.BudgetYear.SelLength = 1
    End If
    
End Sub
The budget year is a four digit year that may have a letter at the end to indicate "Proposed" (Input mask is: 0000?;;

Desired behavior is to move to the start of the field if a new record. If it's not a new record, then select the character the user clicked on. (Usually the last, or next to the last, character.)
 

JHB

Have been here a while
Local time
Today, 15:41
Joined
Jun 17, 2012
Messages
7,732
The following code always works if I set a break point, and traces through as I would expect it to.
Then it could maybe help to place a DoEvents, just before the If statement!
Code:
Private Sub BudgetYear_GotFocus()
    [B][COLOR=Red]DoEvents[/COLOR][/B]
    If Me.NewRecord Then
        Me.BudgetYear.SelStart = 0
        Me.BudgetYear.SelLength = 0
    ElseIf Nz(Me.BudgetYear, vbNullString) = vbNullString Then
        Me.BudgetYear.SelStart = 0
    Else
        Me.BudgetYear.SelLength = 1
    End If
End Sub
 

Royce

Access Developer
Local time
Today, 08:41
Joined
Nov 8, 2012
Messages
99
DoEvents did not help.
 

JHB

Have been here a while
Local time
Today, 15:41
Joined
Jun 17, 2012
Messages
7,732
In which code line do you place the break point?
Do you know which line(s) in the code that doesn't work?
 

Royce

Access Developer
Local time
Today, 08:41
Joined
Nov 8, 2012
Messages
99
I've placed the break point in several spots. Same results every time.
All of the code "works". None of the code "works". No errors, no warnings. It's only with a new record.

The field is a part of a multi-field primary key.

I can duplicate the problem in a clean database with one table and one form.

I have not been able to find a decent workaround.
 

JHB

Have been here a while
Local time
Today, 15:41
Joined
Jun 17, 2012
Messages
7,732
..
I can duplicate the problem in a clean database with one table and one form.
..
Then post that clean database, (zip it) + how to reproduce the problem you've.
 

Royce

Access Developer
Local time
Today, 08:41
Joined
Nov 8, 2012
Messages
99
Here's the sample database. It has a few minor differences from the original post, as I've tried a lot of things, and left some stuff in that gives me visual hints that I'm hitting the lines I intend to hit.
 

Attachments

  • GotFocusBug.zip
    33 KB · Views: 53

JHB

Have been here a while
Local time
Today, 15:41
Joined
Jun 17, 2012
Messages
7,732
Try to add the below, remember also to set the event handler in the property sheet:
Code:
Private Sub BudgetYear_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Me.NewRecord Then
    Me.BudgetYear.SelStart = 0
    Me.BudgetYear.SelLength = 2
  End If
End Sub
 

Royce

Access Developer
Local time
Today, 08:41
Joined
Nov 8, 2012
Messages
99
That did the trick. I found numerous posts various forums that indicate _GotFocus is buggy. I don't use it much, and have always used MouseUp for drag and drop stuff.
 

Users who are viewing this thread

Top Bottom