Set Focus Not Working

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
209
Office Version
  1. 365
Platform
  1. Windows
I am attempting to create a simple form to input and search for details using date records. I am aware of the complexities or using date formats when using text boxes and the DTPicker poses additional problems when I try to use VLOOKUP to search a table where dates are the key.

To avoid this I have created the dates in the lookup table in text format (dd/mm/yyyy).

I have created simple code which works as I want when the date in entered into the txtDate textbox in the required format. The sub-routine code auto tabs to the next field and the correct information is gathered from the look up table.

I have a message box that appears when the details input into the txtDate is not in the correct format and have limited this box to 10 characters. However, when incorrectly formatted information is input into the txtDate box I want this field to clear AND the cursor reappear in this textbox when I click on the Message Box. Unfortunately, the cursor appears in the next text box.

VBA Code:
Private Sub txtDate_AfterUpdate()
    
    If Len(Me.txtDate.Text) <> 10 Then
        MsgBox ("Input must be in the format dd/mm/yyyy")
        txtDate.Value = ""
        txtDate.SetFocus
        Exit Sub
    End If
    
    With Me
    
    .txtPayMonth = Application.WorksheetFunction.VLookup(Me.txtDate, Sheet2.Range("A2:B370"), 2, False)
    End With
       
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
If you use the BeforeUpdate event, you can just use Cancel = True instead of trying to set focus.
 
Upvote 0
If you use the BeforeUpdate event, you can just use Cancel = True instead of trying to set focus.
Sorry Rory
Please ignore my inexperience here but I created a BeforeUpdate Sub-routine which is not recognised by VBA

VBA Code:
Private Sub txtDate_BeforeUpdate()
    If Len(Me.txtDate.Text) <> 10 Then
        MsgBox ("Input must be in the format dd/mm/yyyy")
        txtDate.Value = ""
        'txtDate.SetFocus
        Cancel = True
        Exit Sub
    End If
End Sub
 
Upvote 0
I am attempting to create a simple form to input and search for details using date records. I am aware of the complexities or using date formats when using text boxes and the DTPicker poses additional problems when I try to use VLOOKUP to search a table where dates are the key.

To avoid this I have created the dates in the lookup table in text format (dd/mm/yyyy).

I have created simple code which works as I want when the date in entered into the txtDate textbox in the required format. The sub-routine code auto tabs to the next field and the correct information is gathered from the look up table.

I have a message box that appears when the details input into the txtDate is not in the correct format and have limited this box to 10 characters. However, when incorrectly formatted information is input into the txtDate box I want this field to clear AND the cursor reappear in this textbox when I click on the Message Box. Unfortunately, the cursor appears in the next text box.

VBA Code:
Private Sub txtDate_AfterUpdate()
  
    If Len(Me.txtDate.Text) <> 10 Then
        MsgBox ("Input must be in the format dd/mm/yyyy")
        txtDate.Value = ""
        txtDate.SetFocus
        Exit Sub
    End If
  
    With Me
  
    .txtPayMonth = Application.WorksheetFunction.VLookup(Me.txtDate, Sheet2.Range("A2:B370"), 2, False)
    End With
     
End Sub
Resolved after some investigation
VBA Code:
Private Sub txtDate_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.txtDate.Text) <> 10 Then
        MsgBox ("Input must be in the format dd/mm/yyyy")
        txtDate.Value = ""
        'txtDate.SetFocus
        Cancel = True
        Exit Sub
    End If
End Sub
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,222,626
Messages
6,167,157
Members
452,099
Latest member
Auroraaa

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