Textbox Not Taking "Focus"

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,214,908
Messages
6,122,186
Members
449,071
Latest member
cdnMech

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