Userform Textbox SetFocus - No Cursor Displayed

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,871
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this piece of code that checks the appropriateness of the value entered by the user in a userform textbox. It checks on whether a time value had been entered and happens things when a non time entry is made.

Code:
    If IsDate(Me.tb_s1_lwr.Value) Then
        ' enter code here for a properly provided time
    Else
        MsgBox "Please enter time as h:mm using 24 hour clock.", vbExclamation, "INVALID TIME ENTRY"
        'reset textbox to default
        Me.tb_s1_lwr.Value = ""
        tb_s1_lwr.BackColor = RGB(206, 234, 232)
        tb_s1_lwr.SetFocus
        mbevents = True
        Exit Sub
    End If

This woks for the better part except for one slight annoyance. I am trying to get the cursor to show up in the textbox tb_s1_lwr. The SetFocus command doesn't appear to be doing that for me.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
Which event is the code in?
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,871
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Oh hi Nori, it's in a textbox exit event ...

Code:
Private Sub tb_s1_lwr_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Stop
    Dim bkg_start As Double
    If Not mbevents Then Exit Sub
    mbevents = False
    If IsDate(Me.tb_s1_lwr.Value) Then
        ' code
    Else
        MsgBox "Please enter time as h:mm using 24 hour clock.", vbExclamation, "INVALID TIME ENTRY"
        Me.tb_s1_lwr.Value = ""
        tb_s1_lwr.BackColor = RGB(206, 234, 232)
        tb_s1_lwr.SetFocus
        mbevents = True
        Exit Sub
    End If
    tb_s1_upr.Enabled = True
    tb_s1_upr.BackColor = RGB(206, 234, 232)
    Label5.Enabled = True
    tb_s1_upr.SetFocus
    
    mbevents = True
End Sub
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
Have you tried setting Cancel to True?
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,871
Office Version
  1. 365
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

No. I don't know where in the code that would go. I'm assuming at the top?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
No, it would replace this.
Code:
tb_s1_lwr.SetFocus
Setting Cancel to true cancels the Exit event and should keep focus on the textbox.
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,871
Office Version
  1. 365
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

Hi Norie.
Yes, that suggestion seems to have worked!!! Thank you!
I've never really understood what Cancel does, so I suppose a bit more research on my part is needed.
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,871
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,
I still having somewhat of a related problem. I'm not sure what code is at fault ...
Here is my revised code that Norie kindly helped me out with.

Rich (BB code):
Private Sub tb_s1_lwr_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'Stop
    Dim svc_start As Double
    If Not mbevents Then Exit Sub
    mbevents = False
    If IsDate(Me.tb_s1_lwr.Value) Then
        tb_s1_lwr.Value = Format(Me.tb_s1_lwr.Value, "H:MMA/P")
        tb_s1_lwr.BackColor = RGB(255, 255, 255) 'white
        'is time within booking range
        svc_start = TimeValue(Me.tb_s1_lwr.Value)
        slwr_time = bkg_date + svc_start
        If slwr_time <= bkg_dst Or slwr_time >= bkg_det Then
            MsgBox "The service time entered is outside the booking time.", vbExclamation, "INVALID TIME ENTRY"
            Me.tb_s1_lwr.Value = ""
            Me.tb_s1_lwr.BackColor = RGB(206, 234, 232)
            Cancel = True 'Me.tb_s1_lwr.SetFocus
            mbevents = True
            Exit Sub
        End If
    Else
        MsgBox "Please enter time as h:mm using 24 hour clock.", vbExclamation, "INVALID TIME ENTRY"
        Me.tb_s1_lwr.Value = ""
        tb_s1_lwr.BackColor = RGB(206, 234, 232)
        Cancel = True 'Me.tb_s1_lwr.SetFocus
        mbevents = True
        Exit Sub
    End If
    tb_s1_upr.Enabled = True
    tb_s1_upr.BackColor = RGB(206, 234, 232)
    Label5.Enabled = True
    tb_s1_upr.SetFocus
   
    mbevents = True
End Sub

If everything checks out with the user's entry in this textbox, the next textbox (tb_s1_upr) becomes accessible to the user for similar input. The code set up some default formatting, and then sets focus to it (highlighted line in blue). The desired result is to have the blinking cursor in that textbox. This isn't happening. I have a "beforeupdate" event associated with textbox tb_s1_upr in case that matters (this is also causing me problems ... new post to come ... would welcome help with it also!)
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,985
I also have that issue on my Mac. Whenever I send the focus to a TextBox with code, the focus gets there, but the cursor doesn't blink or show at all. Its been that way with a couple generations of Mac Excel. I have assume it has something to do with the implementation on a Mac.
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,203
Office Version
  1. 2016
Platform
  1. Windows
You could use a hacky workaround by delaying setting the focus until after the exit event is finished

Something along these lines :

In a Standard Module :
VBA Code:
Option Explicit

Public OCtrl As Control

Public Sub DelayedSetFocus()
    OCtrl.SetFocus
End Sub

And then add this Private routine in the UserForm Module:
VBA Code:
Private Sub ForceFocus(ByVal Ctrl As Control)
    Set OCtrl = Ctrl
    Application.OnTime Now, "DelayedSetFocus"
End Sub

Now back to the TextBox Exit event, Use ForceFocus to set the focus back to the TextBox (instead of using the focus the normal way via the SetFocus Method)

So, for example this :
tb_s1_lwr.SetFocus
Will become thhis :
ForceFocus tb_s1_lwr
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,130,129
Messages
5,640,296
Members
417,135
Latest member
zeusmining

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