TextBox restricted to numbers - vba

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

Related

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

How do I limit what characters can be typed

I want to make sure that only letters can be typed into this TextBox. I do not want characters like £, $ or even numbers being typed in. I know how to limit the amount of characters with MaxLength, but not what characters can be typed.
For VBA you can analyze what is entered in the keypress event. You can do this in VB.NET also, it will just be a bit different.
Private Sub Text4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'This will allow only numaric values in the Text4 text box.
If KeyAscii = 8 Then Exit Sub
If Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
KeyAscii = 0
End If
End Sub
You can also look at the keys in the KeyDown event also. If you get a key you don't want, set the KeyCode to 0 and Exit Sub or what you want.
Private Sub Text4_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
KeyCode = 0
DataGrid1.SetFocus
End If
End Sub
You will look at the decimal number for the characters represented here.
http://www.techonthenet.com/ascii/chart.php
To limit what the user can key in you can handle the KeyPress event of the textbox.
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Not (Asc(e.KeyChar) = 8) Then
If Not ((Asc(e.KeyChar) >= 97 And Asc(e.KeyChar) <= 122) Or (Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 90)) Then
e.KeyChar = ChrW(0)
e.Handled = True
End If
End If
End Sub
Alternatively, you can limit what the user enter's by adding a string of allowed characters. If it's not allowed, the event is not handled.
If Not (Asc(e.KeyChar) = 8) Then
Dim allowedChars As String = "abcdefghijklmnopqrstuvwxyz"
If Not allowedChars.Contains(e.KeyChar.ToString.ToLower) Then
e.KeyChar = ChrW(0)
e.Handled = True
End If
End If

Checklistbox Multiple Selection

I have been working on a application that uses a Checkedlistbox so I can allow the user to select multiple boxes.
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
For Each item As Object In Me.CheckedListBox1.CheckedItems
Dim text As String = Me.CheckedListBox1.GetItemText(item)
Next
If text = "Line 1" Then
CreateLine1()
End If
If text = "Line 2" Then
CreateLine2()
End If
If text = "Line 3" Then
CreateLine3()
End If
If Text = "Line 4" Then
CreateLine4()
End If
If Text = "Line 5" Then
CreateLine5()
End If
It goes all the way to "Line 10". When the app runs it use cmd.exe to connect to telnet and send commands. If I have Line 1 and Line 2 selected Line 1 has no problems, but when Line 2 run it opens a cmd does, nothing for a few seconds, open another cmd, and run just the commands while not connected to the telnet. Several more widows open afterwords and the four or fifth window connected to telnet.
How can I make it so if one line if selected after it has run telnet it separates out that line as "Has been ran" before going to the next line to avoid my problem.
Addition info:
This app has a select-all and deselect-all buttons so I can not have anything that will interfere with them.
I have try using socket to replace cmd.exe.....it did not go so well and I will pass on it.
Each sub the lines go to it basically the same except to the IP address and a few commands.
I hope the original code you posted isn't actually what you're using...it doesn't seem quite right.
Perhaps something like this might be more useful:
Imports System.Reflection
Public Class Form1
Private Methods As New List(Of MethodInfo)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim MI As MethodInfo
For i As Integer = 1 To 10
mi = Me.GetType.GetMethod("CreateLine" & i, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public)
If Not IsNothing(MI) Then
Methods.Add(MI)
End If
Next
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
For Each Index As Integer In Me.CheckedListBox1.CheckedIndices
Methods(Index).Invoke(Me, Nothing)
Next
End Sub
Private Sub CreateLine1()
Debug.Print("CreateLine1()")
End Sub
Private Sub CreateLine2()
Debug.Print("CreateLine2()")
End Sub
Private Sub CreateLine3()
Debug.Print("CreateLine3()")
End Sub
Private Sub CreateLine4()
Debug.Print("CreateLine4()")
End Sub
Private Sub CreateLine5()
Debug.Print("CreateLine5()")
End Sub
Private Sub CreateLine6()
Debug.Print("CreateLine6()")
End Sub
Private Sub CreateLine7()
Debug.Print("CreateLine7()")
End Sub
Private Sub CreateLine8()
Debug.Print("CreateLine8()")
End Sub
Private Sub CreateLine9()
Debug.Print("CreateLine9()")
End Sub
Private Sub CreateLine10()
Debug.Print("CreateLine10()")
End Sub
End Class
There are lots of other ways to do this as well...

Close userform with escape button

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.

One Textbox Value Greater then other, Avoid and Beep

I have two Textboxes in Visual Basic 2010
Textbox1
Textbox2
Now I want that if user enter value(integer) in Textbox2, if the value in Textbox2 is Greater than Textbox1, so this will do a beep and also avoid him to do like this
Example: If User write 5 in Textbox1 and now he is writing 8 in Textbox2 so as 8 is grater than 5, so i want that Textbox2 ignore with a beep.
I have this code but this is not working, please if some one help me:
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
Dim valx1 As Integer
Dim valx2 As Integer
valx1 = (Val(TextBox1.Text))
valx2 = (Val(TextBox2.Text))
If (valx1) > (valx2) Then
Beep()
e.Handled = True
End If
End Sub
This code will suits your requirement, By the way using a beeb sound for alerting the user, will make him/her disturbed.
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
if val(TextBox1.Text.trim) > val(TextBox2.Text.trim & e.keychar) then
Beep()
e.Handled = True
end if
End Sub