.SetFocus Not Working

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,480
I have a userform that performs some validation checks on the data entered.

The validation executes on the _AfterUpdate event in the textboxes

Where the data entered is incorrect I am clearing the entry and trying to set the focus back to the textbox but it just isn't working.

On one for it doesn't seem to set the focus anywhere on the form and on another it is setting the focus to a textbox that is a few TabIndex values away from the one in use.

It's been a while since I built a userform but I recall this being a very simple action to perform.

The code from the form where it isn't setting the focus anywhere is below
Code:
Private Sub txtJobNo_afterupdate()

Sheets("Data").Range("Data_JobNo") = txtJobNo

If Sheets("Data").Range("Data_ValidJobNo") = "Yes" Then
   Else
   MsgBox ("The 'Formatt Job No' entered already exists in the stored jobs." & vbCr & vbCr & _
      "Enter a different 'Formatt Job No'"), vbExclamation, "Invalid Formatt Job No"
   
   Sheets("Data").Range("Data_JobNo") = ""
   
   txtJobNo = ""
   txtJobNo.SetFocus
   Exit Sub
End If

Sheets("Data").Range("Data_JobNo") = ""

txtClient.Enabled = True
txtContactNo.Enabled = True
txtContact.Enabled = True
txtEmail.Enabled = True
txtClientAddress.Enabled = True
txtInstallAddress.Enabled = True
txtClientRef.Enabled = True
txtJobName.Enabled = True
txtQuoteNo.Enabled = True
txtNotes.Enabled = True

End Sub
This is the code where is is setting the focus to several TabIndex values away
Code:
Private Sub txtStartDate_afterupdate()

Dim mbConfirm

If IsDate(CDate(txtStartDate)) Then
   If CDate(txtStartDate) < dtFormDate Then
      mbConfirm = MsgBox("The 'Start Date' is prior to todays date." & vbCr & vbCr & _
         "Click 'OK' to confirm or 'Cancel' to enter another date.", vbOKCancel, "Confirm Date")
      
      If mbConfirm = vbOK Then
         Else
         txtStartDate = ""
         txtStartDate.SetFocus
      End If
      
      Else
   End If
End If

End Sub
 

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Watch MrExcel Video

Forum statistics

Threads
1,122,224
Messages
5,594,916
Members
413,952
Latest member
JGer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top