Control the Text Box' Behavior on Clicking Enter

The following example uses the EnterKeyBehavior property to control the effect of ENTER in a TextBox. In this example, the user can specify either a single-line or multiline TextBox.

To use this example, copy this sample code to the Script Editor of a form. To run the code you need to open the form so the Open event will activate. Make sure that the form contains:

  • A TextBox named TextBox1.

  • Two ToggleButton controls named ToggleButton1 and ToggleButton2.

Dim TextBox1 
Dim ToggleButton1 
Dim ToggleButton2 
 
Sub Item_Open() 
 set TextBox1 = Item.GetInspector.ModifiedFormPages.Item("P.2").Controls("TextBox1") 
 set ToggleButton1 = Item.GetInspector.ModifiedFormPages.Item("P.2").Controls("ToggleButton1") 
 set ToggleButton2 = Item.GetInspector.ModifiedFormPages.Item("P.2").Controls("ToggleButton2") 
 TextBox1.EnterKeyBehavior = True 
 ToggleButton1.Caption = "EnterKeyBehavior is True" 
 ToggleButton1.Width = 70 
 ToggleButton1.Value = True 
 
 TextBox1.MultiLine = True 
 ToggleButton2.Caption = "MultiLine is True" 
 ToggleButton2.Width = 70 
 ToggleButton2.Value = True 
 
 TextBox1.Height = 100 
 TextBox1.WordWrap = True 
 TextBox1.Text = "Type your text here. If EnterKeyBehavior is True,"& _ 
 " press Enter to start a new line. Otherwise, press SHIFT+ENTER." 
End Sub 
 
Sub ToggleButton1_Click() 
 If ToggleButton1.Value = True Then 
 TextBox1.EnterKeyBehavior = True 
 ToggleButton1.Caption = "EnterKeyBehavior is True" 
 Else 
 TextBox1.EnterKeyBehavior = False 
 ToggleButton1.Caption = "EnterKeyBehavior is False" 
 End If 
End Sub 
 
Sub ToggleButton2_Click() 
 If ToggleButton2.Value = True Then 
 TextBox1.MultiLine = True 
 ToggleButton2.Caption = "MultiLine TextBox" 
 Else 
 TextBox1.MultiLine = False 
 ToggleButton2.Caption = "Single-line TextBox" 
 End If 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.