Textbox Not Taking "Focus"

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Considering this code:

Code:
Private Sub tb_r1_sru_BeforeUpdate(ByVal CANCEL As MSForms.ReturnBoolean)
    'Stop
    On Error GoTo badtime
    Me.tb_r1_sru.Value = format(Me.tb_r1_sru.Value, "h:mm AM/PM")
    sru1 = CDate(tb_r1_sru.Value)
    chk1 = sru1
    time_range1 chk1
    mbevents = false 
    If e = 1 Then
        Me.tb_r1_sru.Value = ""
        Me.tb_r1_sru.SetFocus
    Else
        Me.tb_r1_srl.Value = format(sru1 + TimeSerial(0, 30, 0), "h:mm AM/PM")
        buv = CDate(tb_r1_srl.Value)
    End If
    mbevents = true
    Exit Sub
    
badtime:
    
    MsgBox "Invalid time."
    Me.tb_r1_sru.Value = ""
    Me.tb_r1_sru.SetFocus

End Sub

If the user enters a bad value in textbox "tb_r1_sru", it triggers the badtime: event. Within that code, a msgbox appears and the value is cleared. The intention is to return the user back to that textbox to allow them to reenter a value. I thought that's what the "SetFocus" would accomplish for me.

What ends up happening is the cursor appears in textbox 'tb_r1_srl' in that same form . The user has to click back into "tb_r1_sru" in order to reenter a value. I added mbevents = false to prevent the code from the BeforeUpdate event of 'Me.tb_r1_srl' from running in case that may be why the cursor insists of going there.

Is anyone able to provide a reason why this may be happening?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Instead of Setfocus, just make CANCEL = True.
 
Upvote 0
Thanks Rory, such an easy solution to what is now a trivial question.

I notice though, and it's not a big deal, but the user still ends up having to click in the textbox to get the cursor. Just for convenience, is there a way that I can have the textbox ready for the user to enter a value (ie. cursor in box without having to first click on it)?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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
Back
Top