Detecting a press of tab button or a change of textbox with VBA

Fire_Chief

Well-known Member
Joined
Jun 21, 2003
Messages
633
I have a user form ("Participants_Info" is the name of the user form). When this form is brought up I want to detect when the user either presses the Tab button
or uses the mouse to change the cursor to a different box. There are 2 comboboxes and 3 textboxes. The second name is if there are two participants.
I want to be sure there is something in the box when they move to the next one. I know there is code to do this but I can't figure it out.
I think I have tried all of the choices available but can't get it to detect both.

I have posted before with no answers.
Capture.Png

Thank You
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,372
Office Version
  1. 2019
Platform
  1. Windows
Hi,
one idea, you could enable the controls in sequence so that users can only tab once data has been entered in each required control

something like following maybe

VBA Code:
Private Sub ComboBox1_Change()
    EnableControls
End Sub

Private Sub ComboBox2_Change()
    EnableControls
End Sub

Private Sub TextBox1_Change()
    EnableControls
End Sub

Private Sub TextBox2_Change()
    EnableControls
End Sub


Sub EnableControls()
    Me.TextBox1.Enabled = Me.ComboBox1.ListIndex <> -1
    Me.ComboBox2.Enabled = IsEmail(Me.TextBox1.Text)
    Me.TextBox2.Enabled = Me.ComboBox2.ListIndex <> -1 And Me.TextBox1.Enabled
    Me.TextBox3.Enabled = Me.ComboBox2.Enabled
End Sub

Private Function IsEmail(ByVal Text As String) As Boolean
IsEmail = InStr(1, Text, ".") <> 0 And InStr(1, Text, "@") <> 0
End Function

Private Sub UserForm_Initialize()
    EnableControls
End Sub

I have included a very simple function to check text entered in textbox looks like an email address

This is just an idea, you will need to adjust code as required to meet specific project need

Dave
 
Solution

Fire_Chief

Well-known Member
Joined
Jun 21, 2003
Messages
633
Hey Dave
I played with your code some and I think I can get it to work.

Can't get at it for a couple days but will let you know.

Thanks again
 

Watch MrExcel Video

Forum statistics

Threads
1,122,471
Messages
5,596,342
Members
414,060
Latest member
hermanseck

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