setfocus function not working...

albertc30

Well-known Member
Joined
May 7, 2012
Messages
1,091
Office Version
  1. 2019
Platform
  1. Windows
Hello you all, good people.

It has been a few years last time I have been around.

I have been dwelling with the code bellow and the setfocus function is driving me nuts, as it's not working.

Instead, the cursor is moving to the next field in the form.

What am I doing wrong please?


Code:
Private Sub tbnctel_AfterUpdate()


    If Len(Me.tbnctel.Value) < 11 Then
        MsgBox "Error! Contact number not long enough."
        Me.tbnctel.Value = Left(Me.tbnctel.Value, 11)
        
        tbnctel.Value = "" 'Does clear the correct field.
        
        tbnctel.SetFocus 'Does not set focus on the correct field.
    
        Exit Sub
    
    End If
    
    tbnctel = Format(tbnctel, "00000 000 000")
    
End Sub

As always, I am very much appreciated for all of your help.

Many thanks.
Albert

It's good to be back.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
It's because your code is in afterupdate. Just the way it works i'm afraid. Try:

Code:
Private Sub tbnctel_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    Application.EnableEvents = False
    If Len(tbnctel.Value) < 11 Then
        Cancel = True
        MsgBox "Error! Contact number not long enough.", vbCritical
        tbnctel.Value = Left(Me.tbnctel.Value, 11) '<--Not sure why this is even here!
        tbnctel.Value = ""
        tbnctel.SetFocus
    Else
        tbnctel = Format(tbnctel, "00000 000 000")
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
Hi trunten, again.

I have replied three times now and for some reason I can't see them in here.

The code you provided works a treat.

Much appreciated.

On another note what would you add to the code to say landlines must start 01?

Thanks for your time.

Cheers.
Albert
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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