Textbox Not Taking "Focus"

Ark68

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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,730
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Instead of Setfocus, just make CANCEL = True.
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,871
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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)?
 

Watch MrExcel Video

Forum statistics

Threads
1,130,139
Messages
5,640,343
Members
417,140
Latest member
bdmprasenjit

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