Close userform with escape button - vba

I have 2 questions.
When I pressed esc button then close Userform1
When I input open in TextBox1 then Userform2 should show. Also clear TextBox1 in Userform1 automatically.
I have tried the below code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If textbox1.value = "open" then
userform2.show
textbox1.value =""
End If
End Sub

Insert a new Command Button
Switch its Cancel property to True
You May Name it as cmdClose
Add next code:
Private Sub cmdClose_Click()
Unload Me
End Sub
5.Set height and widht of the button to 0
that's it

Close userform1 with Esc
If you don't have any controls on userform then simply use this code
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub
If you have say a TextBox and a Command Button then use this
Private Sub UserForm_Initialize()
CommandButton1.Cancel = True
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
If you have any other control that can take focus then you will have to use the KeyPress event of that control like I did for TextBox
when I input "open" to textbox1 then userform2 showed also clear textbox1 in userform1 automatically.
KeyPress will capture only one key. Use the Change event to compare what is there in the textbox.
Private Sub TextBox1_Change()
If LCase(TextBox1.Value) = "open" Then
TextBox1.Value = ""
UserForm2.Show
End If
End Sub

if you have a button the closes the form, just set the (Cancel) property to True and that will fire the cancel button on (Esc)..
Cheers.

Related

TextBox restricted to numbers

I have a question, is it possible to limit a field in Telbox to a number consisting of 5 digits, e.g.
11111
22222
I tried to use a macro but it limits you to numbers only.
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not IsNumeric(Chr(KeyAscii)) Then
MsgBox "Enter a number!"
KeyAscii = 0
End If
End Sub
You can respond to the Exit event in addition to the KeyPress event to accomplish this task:
Option Explicit
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not IsNumeric(Chr(KeyAscii)) And Not KeyAscii = 8 Then 'allow for backspace
KeyAscii = 0
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Val(TextBox1.Text) < 10000 Or Val(TextBox1.Text) > 99999 Then
MsgBox "The number must be 5 digits"
Cancel = True
End If
End Sub
I tried this and it worked for me.
Rather than the .change Method i used the "beforeupdate" Method
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Text) < 5 Then
MsgBox "Please enter Values between 10000 and 99999"
End If
End Sub
Private Sub UserForm_Initialize()
TextBox1.MaxLength = 5
End Sub
Maybe this helps

Cant change active textbox and set focus after mouse click on it

After second click on textboxes i cant write nothing after click on button "1". Setfocus is working only at first click (img).
Can You help?
Greetings
Private activeTextbox As Control
Private Sub Textbox_sr_Enter()
Set activeTextbox = Controls("Textbox_sr")
End Sub
Private Sub CommandButton1_Click()
If Not activeTextbox Is Nothing Then
activeTextbox.Text = activeTextbox.Text & "1"
activeTextbox.SetFocus
End If
End Sub
Private Sub TextBox1_Enter()
Set activeTextbox = Controls("TextBox1")
End Sub
Private Sub TextBox2_Enter()
Set activeTextbox = Controls("TextBox2")
End Sub
Private Sub TextBox3_Enter()
Set activeTextbox = Controls("TextBox3")
End Sub

vb.net detect keypress on a form

I am new to vb.net and am trying to detect a KeyPress on a Form
I have accomplished this in JavaFX by creating a listener that when the ESC Key is press the application close
I have not found any code examples that use a listener in vb.net
I have found code that Handles a KeyPress for a TextBox but the same code for a Form FAILS
For this function to close the application from any Form I am wondering if it needs to be declared in a Module? While that part of the question would be nice to know Call it a Bonus
My question is why is this code not detecting a keypress on frmOne ?
The code to detect a keypress in the txtBoxOne runs as expected
Public Class frmOne
Private Sub frmOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
'frmOne Property FixedToolWindow
'frmOne is the Start Up Form
If Asc(e.KeyChar) > 1 Then
MessageBox.Show("You Pressed " & e.KeyChar)
End If
'If Asc(e.KeyChar) > 1 Then txtBoxOne.Text = "You Pressed"
End Sub
Private Sub txtBoxOne_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtBoxOne.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
MsgBox("Error.")
Else
e.Handled = False
End If
End Sub
Private Sub btnToFormTwo_Click(sender As Object, e As EventArgs) Handles btnToFormTwo.Click
Dim i As Integer
i = txtBoxOne.Text.Length
If i = 0 Then
'txtBoxOne.Text = "Enter"
MessageBox.Show("Enter Data")
txtBoxOne.Select()
Return
End If
Dim OBJ As New frmTwo
OBJ.SPass = txtBoxOne.Text
OBJ.Show()
'MyTextBox_Enter()
txtBoxOne.Clear()
Me.Hide()
'Me.Close()'R Click project PassVar Set Start Up Form
'Best Solution is to have Splash Form as Start Up Form
End Sub
Public Sub MyTextBox_Enter()
txtBoxOne.Clear()
End Sub
'Private Sub frmOne_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Use code below if pre populated text in txtBoxOne
'Me.ActiveControl = txtBoxOne
'txtBoxOne.Select(txtBoxOne.Text.Length, 0)
'txtBoxOne.Select()
'End Sub
End Class
The same code will work for a form but a form will not raise keyboard events by default if a child control has focus. You need to set the form's KeyPreview property to True, in which case the form will raise those keyboard events before the active child control does.

Call sub not working but code works when placed in sub that is doing the calling

Should be a quick and easy one for you folks, why does this not work?
Code restricts user to enter only text in a textbox.
It works fine, but I have about 50 textbox's so would be cleaner and easier to Call instead.
However, done like this the restriction no longer works
Private Sub OnlyAcceptText()
'Forces the textbox to only accept text
If (KeyAscii < 65 Or KeyAscii > 90) And (KeyAscii < 97 Or KeyAscii > 122)Then KeyAscii = 0
End Sub
Code in textbox by KeyPress:
Private Sub AgencyContactTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Calls sub that only allows text in texctbox
Call OnlyAcceptText
End Sub
Using event sinking, youd have a class, with an event sunk text box inside, something like so
Private WithEvents t As MSForms.TextBox
Public Sub INITIALISE(tb As MSForms.TextBox)
Set t = tb
End Sub
Private Sub t_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If (KeyAscii < 65 Or KeyAscii > 90) And (KeyAscii < 97 Or KeyAscii > 122) Then KeyAscii =0
End Sub
Then you'd need to make a collection, to hold the 50 classes, which would mimic me.controls as a collection, but just for the custom text boxes.
Public colCustomTextboxes As New Collection
Private Sub UserForm_Initialize()
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
set t=new clsCustomTextBox
t.INITIALISE c
colCustomTextboxes.Add t
End If
Next c
End Sub
Following #Nathan_Sav comment, you can also add a second parameter to your OnlyAcceptText Sub, that identifes the Caller TextBox.
Private Sub AgencyContactTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
' use can also add the TB as indentifier
OnlyAcceptText AgencyContactTextBox, KeyAscii
End Sub
'========================================================================
Private Sub OnlyAcceptText(ByRef TB As MSForms.TextBox, ByVal KeyAscii As MSForms.ReturnInteger)
With TB
If (KeyAscii < 65 Or KeyAscii > 90) And (KeyAscii < 97 Or KeyAscii > 122) Then KeyAscii = 0
End With
End Sub

Textbox Setfocus VBA

I'm on a Powerpoint VBA now, and trying to create a simple log in system
And I want to set focus to TextBox2 when I click TAB key on my keyboard if I'm focused on TextBox1.
This is my code.
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
If KeyCode = 9 Then
TextBox2.SetFocus
End If
End Sub
Private Sub TextBox2_Change()
End Sub
What's the problem?