.Setfocus being ignored?

fat controller

Slightly round the bend..
Local time
Today, 15:02
Joined
Apr 14, 2011
Messages
758
I have a small piece of code (below), which generally does exactly what I want it to do, however the .setfocus line seems to be being ignored no matter where I put it?

The code was originally:

Code:
Dim DatePicked
DatePicked = txtPerStartDate.Value
If Weekday(DatePicked) <> vbSaturday Then
MsgBox ("All of our four week periods begin on a Saturday, and the date you entered is not a Saturday" & vbLf & vbLf & "Please check the period start date and try again"), vbOKOnly, "Oops!  Wrong date entered..."
Me.txtPerStartDate.Value = Null
Me.txtPerStartDate.Setfocus
Else
DoCmd.RunCommand acCmdSaveRecord
Me.cboW1MonDuty.SetFocus
End If
End Sub

To see if shifting the code further on did anything, I tried the following

Code:
Dim DatePicked
DatePicked = txtPerStartDate.Value
If Weekday(DatePicked) <> vbSaturday Then
MsgBox ("All of our four week periods begin on a Saturday, and the date you entered is not a Saturday" & vbLf & vbLf & "Please check the period start date and try again"), vbOKOnly, "Oops!  Wrong date entered..."
Me.txtPerStartDate.Value = Null
GoTo Whatnext
Else
DoCmd.RunCommand acCmdSaveRecord
Me.cboW1MonDuty.SetFocus
End If
Whatnext:
Me.txtPerStartDate.SetFocus
End Sub

However the end result is the same - - regardless of whether the Update event is triggered by tabbing out of the field, or by clicking on another field, the code executes with the exception of the line to set focus back to the original field, and the focus will go on to the field that was clicked (or the next one in the tab order)

Any clues?
 
I have suffered the same problem in the last couple of days with a simple unbound form with very few controls. This checks the input from a Barcode scanner and warns if the scanned data doesn't match.

The code is very straight forward. The set focus seems to be completely ignored.
Code:
Private Sub Text2_AfterUpdate()
'Invoice number
    Dim sMsg As String
    Dim iInvNo As Long
    Dim iVal As Integer
    
    If IsNull(Me.Text2) Then Exit Sub
        
    If Left(Me.Text2, 1) = "D" Then
        If Me.Text2 <> Me.txtExistInvNo Then
            sMsg = "Scanned in Invoice number does NOT agree with DIS-generated Invoice number for this RMA 1."
            MsgBox sMsg, vbCritical, "Data Entry Error?"
            Beep
            Me.Text2.SetFocus
        End If
        Me.Text4.SetFocus
        Exit Sub
        
    Else
        
        iInvNo = DLookup("InvoiceNumber", "DIMSBatch_InvoiceLookup", "[BatchNumber] = " & Me.Text2)
        If IsNull(iInvNo) Then
            sMsg = "Scanned in Batch Number NOT found in Dimensions ! 2."
            MsgBox sMsg, vbCritical, "Data Entry Error?"
            Beep
            GoTo EndHere
        Else
            Me.txtDIMchk = iInvNo
            If iInvNo <> Me.txtExistInvNo Then
                sMsg = "Invoice number does NOT agree with Dimensions Invoice number for this RMA."
                MsgBox sMsg, vbCritical, "Data Entry Error?"
                Beep
                GoTo EndHere
            End If
            Me.Text4.SetFocus
        End If
    End If
    Exit Sub

EndHere:

With Me.Text2
    .SetFocus
    iVal = Len(.Text)
    Debug.Print iVal
    .SelStart = 0
    .SelLength = iVal
End With

The EndHere: bit is me getting desperate...
 
You know how we love nondescript gripes like

. The set focus seems to be completely ignored.

WHich one of them? Did you check that the code actually passes that bit?
 
what event is this code in?

Its an after update event, but I have also tried it before update - that was even more of an issue, as it threw up an error (something along the lines that the record couldn't be saved due to a procedure in the before update event)

All I am trying to do is set the field value back as null, and have the focus go back to that field as a sort of prompt to the user to try again.
 
WHich one of them? Did you check that the code actually passes that bit?
Ooops - posted in a hurry - the one sets it back to text2, I noticed that the code I posted wasn't quite correct but I had corrected that afterwards. This bit here in red; And yes it does run hence the debug.
Code:
...
Me.txtDIMchk = iInvNo
            If iInvNo <> Me.txtExistInvNo Then
                sMsg = "Invoice number does NOT agree with Dimensions Invoice number for this RMA."
                MsgBox sMsg, vbCritical, "Data Entry Error?"
                Beep
               [COLOR="Red"] GoTo EndHere[/COLOR]
            End If
            Me.Text4.SetFocus

EndHere:
With Me.Text2
    .SetFocus
    iVal = Len(.Text)
    Debug.Print iVal
    .SelStart = 0
    .SelLength = iVal

End With
End Sub
The With Me bit originally was simply me.text2.setfocus
This was not working. It actually ended up with Text1 with the focus.
 
Maybe it is a timing thing when it goes in the afterupdate.

I would use the before update, but you cannot change the value in the before update event. This is generally how to cancel an entry in the before update event.

Code:
Sub Txtdate_beforeupate(cancel as integer)
If weekday(txtdate)<>vbsaturday then
     Msgbox msg
     Cancel = true
     Me.undo  'if necessary
      ' note that you cannot set txtdate to a specific value.
     Exit sub
End if
 

Users who are viewing this thread

Back
Top Bottom