Setfocus strange behavior

mafhobb

Registered User.
Local time
Today, 04:19
Joined
Feb 28, 2006
Messages
1,249
Hi everyone

I have a textbox where the user enters a value. This value is them compared to a table in an afterupdate event and if the same value already exists, then it returns and error message and it requests that a new value is entered.

Code:
Private Sub txtSKU_AfterUpdate()

Dim UserCount As Long
Dim TestedSKU As String

' Check to see if text was entered
Me.txtSKU.SetFocus
    If Me.txtSKU.Text = "" Then
        MsgBox "Please enter a SKU#"
        Exit Sub
    End If

'if text was entered, assign the SKU value to a variable
TestedSKU = Me.txtSKU.Value

'Check to see if the SKU# already exists
UserCount = DCount("[SKU#]", "[TblSKUMessage]", "[SKU#] = '" & TestedSKU & "'")
    If UserCount > 0 Then
        MsgBox "This SKU# already exists. You cannot have two messages for the same SKU#. Please find and modify the existing SKU# message."
        Me.txtSKU.SetFocus
        Exit Sub
    End If
End Sub

It works fine until the last bit of code. The problem comes when I try to get the focus back to the box by using "Me.txtSKu.setfocus". It just does not do it. No matter what i try, the focus always goes to the next textbox.

Why? What am I missing?

Thanks

mafhobb
 
Use the Before Update event of the control if you want to force the user to validate the SKU before moving on to other fields.
The advantage of Before Update is that it allows you to cancel the update if you have a duplicate or whatever and that leaves the cursor back in the control.

However it is often much more user friendly to only force the user to enter an appropriate value using the before update event of the form (not for the control).
This allows users to enter data in whatever order they wish.

You can use the after update event of the control to just warn the user about the incorrect entry.
 
By simply copying the code above to a beforeupdate event I now get a runtime error "2108" on the first me.txtsku.setfocus..."You must save the field before you execute the GOTOControl action, teh GOTOControl method, or the SetFocus method"...
 
Last edited:
If I just copy the code above to the beforeupdate event, I get error 2108. You must save the field before you execute the GOTOControl action, the GOTOControl Method, or the SetFocus Method.

Removing all the "Me.txtSKU.Setfocus" returns the SKU error message I want, but it still moves the focus to the next box.

mafhobb
 
Well, that is what I was saying...Removing those lines (there are two of them) removes the 2108 error and I do in fact get the msgbox I added, but at the end of the code even when the message shows up, the focus moves to the next control. It does not stay in this control

mafhobb
 
I have this issue and my workaround is that it is down to using unbound controls. You cannot use BeforeUpdate on unbound controls with efficiency. So lets think about what needs to happen.

If the code already exists you give a warning message, then you need to set the original text box to "" (blank)

Me.Textbox = ""

Then on the control that is next in the tab index on the form use the GotFocus event

Code:
If Me.Textbox = "" then
   Me.Textbox.Setfocus
End If

In other words don't come here unless you have a valid entry in the previous control.
 

Users who are viewing this thread

Back
Top Bottom