Userform textbox entry with touchscreen

pjmsimmons

Board Regular
Joined
Dec 13, 2011
Messages
80
Hi All,
Bit of a newbie so apologies if this is trivial.

I have a userform with several textboxes. The userform is used with a touchscreen and I have setup X10 buttons labelled 0-9 in the form. I want the user to select a textbox (either textbox1; textbox9 or textbox10) and press eg button '9' so that '9' is entered into the chosen textbox. Below is what I currently have but it isnt working. Can anyone help?

regards

Paul

Code:
Private Sub CommandButton10_Click()
If "TextBox10" = activecontrol.Name Then
    TextBox10.value = TextBox10.value + "9"
ElseIf "TextBox9" = activecontrol.Name Then
    TextBox9.value = TextBox9.value + "9"
    
ElseIf "TextBox1" = activecontrol.Name Then
    TextBox1.value = TextBox1.value + "9"
End If
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
A simple approach would be,

Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]Option Explicit

Public gTextBox As Byte

Private Sub CommandButton1_Click()
    Addvalue 1
End Sub

Private Sub CommandButton2_Click()
    Addvalue 2
End Sub

[COLOR="Green"]' Continue until CommandButton10_Click()[/COLOR]

Private Sub TextBox1_Enter()
    gTextBox = 1
End Sub

Private Sub TextBox2_Enter()
    gTextBox = 2
End Sub

[COLOR="green"]' Continue until TextBox10_Enter()[/COLOR]

Sub Addvalue(ByVal bButtonNumber As Byte)

    Select Case gTextBox
        Case 1
            TextBox1.Value = Val(TextBox1.Value) + bButtonNumber
        Case 2
            TextBox2.Value = Val(TextBox2.Value) + bButtonNumber

[COLOR="Green"]' Add until Case 10[/COLOR]

    End Select

End Sub
[/COLOR][/SIZE][/FONT]
 
Upvote 0
Hi Mohammad
Just having a problem implementing the code you suggest. Nothing seems to happen so I am posting the form code in full. Could you have a quick look and see where I am going wrong? Its quite long so no worries if you havent got the time

regards

Paul

Code:
Option Explicit
Public gTextBox As Byte
 
'maximise screen for window
Private Sub UserForm_Initialize()
With Application
.WindowState = xlMaximized
Zoom = Int(.Width / Me.Width * 80)
Width = .Width
Height = .Height
End With
 
End Sub
 
'unload form
Private Sub CancelButton_Click()
Unload UserForm1
End Sub
 
'only one of checkbox 1,2,3, or 4 at one time
Private Sub CheckBox1_Click()
If CheckBox1.value = True Then CheckBox2.value = False
If CheckBox1.value = True Then CheckBox3.value = False
If CheckBox1.value = True Then CheckBox4.value = False
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.value = True Then CheckBox1.value = False
If CheckBox2.value = True Then CheckBox3.value = False
If CheckBox2.value = True Then CheckBox4.value = False
End Sub
Private Sub CheckBox3_Click()
If CheckBox3.value = True Then CheckBox1.value = False
If CheckBox3.value = True Then CheckBox2.value = False
If CheckBox3.value = True Then CheckBox4.value = False
End Sub
Private Sub CheckBox4_Click()
If CheckBox4.value = True Then CheckBox1.value = False
If CheckBox4.value = True Then CheckBox2.value = False
If CheckBox4.value = True Then CheckBox3.value = False
End Sub
 
'only one of checkbox 6,7, or 8 chosen
Private Sub CheckBox6_Click()
If CheckBox6.value = True Then CheckBox7.value = False
If CheckBox6.value = True Then CheckBox8.value = False
End Sub
Private Sub CheckBox7_Click()
If CheckBox7.value = True Then CheckBox6.value = False
If CheckBox7.value = True Then CheckBox8.value = False
If CheckBox6.value = False And CheckBox8.value = False Then CheckBox7.value = True
End Sub
 
Private Sub CheckBox8_Click()
If CheckBox8.value = True Then CheckBox6.value = False
If CheckBox8.value = True Then CheckBox7.value = False
End Sub
 
'clear form
Private Sub CommandButton1_Click()
Dim Z As Control, Y As Control
For Each Z In UserForm1.Controls
If TypeName(Z) = "TextBox" Then
Z.value = ""
End If
Next Z
For Each Y In UserForm1.Controls
If TypeName(Y) = "CheckBox" Then
Y.value = False
End If
Next Y
TextBox1.SetFocus
End Sub
 
 
Private Sub CommandButton2_Click()
If TextBox7.Text <> "" Then ActiveWorkbook.Sheets(TextBox7.Text).Activate
Unload UserForm1
End Sub
 
 
Private Sub CommandButton10_Click()
Addvalue 9
End Sub
Private Sub CommandButton11_Click()
Addvalue 7
End Sub
Private Sub CommandButton12_Click()
Addvalue 8
End Sub
Private Sub CommandButton13_Click()
Addvalue 5
End Sub
Private Sub CommandButton14_Click()
TextBox10.Text = TextBox10.Text & "."
End Sub
Private Sub CommandButton15_Click()
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "IE"
End Sub
 
Private Sub CommandButton17_Click()
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & "UK"
End Sub
Private Sub CommandButton3_Click()
 
'save button with data validation and clear after save 
Dim lRow As Long, myDate As Date
Dim Z As Control, Y As Control
Dim res As VbMsgBoxResult
Dim length As Integer
Dim length1 As Integer
Dim length2 As Integer
 
If CheckBox1 = True And TextBox10.value = vbNullString Then
res = MsgBox("You haven't entered a cheque amount, Do you still want to save?", vbYesNo + vbQuestion)
 
If res = vbNo Then Exit Sub
End If
 
If TextBox3.value = vbNullString And TextBox5.value = vbNullString And CheckBox1 = False And CheckBox2 = False And CheckBox3 = False And CheckBox4 = False Then
res = MsgBox("No payment method entered, Do you still want to save?", vbYesNo + vbQuestion)
 
If res = vbNo Then Exit Sub
End If
If TextBox2.value = vbNullString Then
res = MsgBox("No herd number entered, Do you still want to save?", vbYesNo + vbQuestion)
 
If res = vbNo Then Exit Sub
End If
 
If TextBox9.value = vbNullString Then
res = MsgBox("No sample number entered, Do you still want to save?", vbYesNo + vbQuestion)
 
If res = vbNo Then Exit Sub
End If
 
length = Len(TextBox9.value)
If length = 1 Then TextBox9.value = "0" + TextBox9.value
 
length1 = Len(TextBox6.value)
If length1 = 1 Then TextBox6.value = "0" + TextBox6.value
 
length2 = Len(TextBox1.value)
If length2 <> 14 Then res = MsgBox("You haven't entered a complete tag number, Do you still want to save?", vbYesNo + vbQuestion)
 
If res = vbNo Then Exit Sub
 
 
If TextBox9.value > TextBox6.value And TextBox6.value <> vbNullString Then
res = MsgBox("Sample number greater than prepay balance, Do you still want to save?", vbYesNo + vbQuestion)
 
If res = vbNo Then Exit Sub
End If
 
With Workbooks("Batch Payment Record").Worksheets("Sheet1")
lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Cells(lRow + 1, "A") = Now()
.Cells(lRow + 1, "B") = TextBox2.Text
.Cells(lRow + 1, "C") = TextBox1.Text
.Cells(lRow + 1, "D") = TextBox9.Text
.Cells(lRow + 1, "E") = TextBox3.Text
.Cells(lRow + 1, "F") = TextBox6.Text
.Cells(lRow + 1, "G") = TextBox4.Text
.Cells(lRow + 1, "H") = TextBox5.Text
.Cells(lRow + 1, "I") = TextBox7.Text
.Cells(lRow + 1, "K") = TextBox10.Text
.Cells(lRow + 1, "L") = TextBox11.Text
 
If CheckBox1.value = True Then .Cells(lRow + 1, "J") = "Cheque"
If CheckBox2.value = True Then .Cells(lRow + 1, "J") = "Online"
If CheckBox3.value = True Then .Cells(lRow + 1, "J") = "Card"
If CheckBox4.value = True Then .Cells(lRow + 1, "J") = "Cash"
If CheckBox5.value = True Then .Cells(lRow + 1, "M") = "Blood"
 
End With
 
For Each Z In UserForm1.Controls
If TypeName(Z) = "TextBox" Then
Z.value = ""
End If
Next Z
For Each Y In UserForm1.Controls
If TypeName(Y) = "CheckBox" Then
Y.value = False
End If
Next Y
TextBox1.SetFocus
 
End Sub
Private Sub Image1_Click()
End Sub
Private Sub Image2_Click()
End Sub
Private Sub Image3_Click()
End Sub
Private Sub CommandButton4_Click()
Addvalue 1
End Sub
Private Sub CommandButton5_Click()
Addvalue 6
End Sub
Private Sub CommandButton6_Click()
Addvalue 3
End Sub
Private Sub CommandButton7_Click()
Addvalue 4
End Sub
Private Sub CommandButton8_Click()
Addvalue 0
End Sub
Private Sub CommandButton9_Click()
Addvalue 2
End Sub
Private Sub Frame3_Click()
End Sub
Private Sub Label10_Click()
End Sub
Private Sub Label11_Click()
End Sub
Private Sub Label13_Click()
End Sub
Private Sub Label3_Click()
End Sub
Private Sub OKButton_Click()
End Sub
Private Sub Label7_Click()
End Sub
Private Sub Label8_Click()
End Sub
Private Sub Label9_Click()
End Sub
 
'sarch excel form for data
Private Sub TextBox1_Change()
Dim x As Variant, Y As Variant, CompareRange1 As Variant, CompareRange2 As Variant
 
With Workbooks("Prepayment File").Worksheets("Summary")
TextBox1.value = LTrim(TextBox1.value)
TextBox2.value = Left(TextBox1.value, 9)
 
Set CompareRange1 = Range("A2:A2000")
 
For Each x In CompareRange1
 
If TextBox2.Text = x Then TextBox3 = "Prepaid Account"
 
If TextBox2.Text = x Then TextBox7 = x.Offset(0, 2)
 
If TextBox2.Text = x Then TextBox6.value = x.Offset(0, 1)
 
If TextBox1.Text = "" Then TextBox3.Text = ""
 
If TextBox1.Text = "" Then TextBox6.Text = ""
 
Next x
 
 
Set CompareRange2 = Range("I2:I10000")
 
For Each Y In CompareRange2
 
If TextBox2.Text = Y Then TextBox4.Text = Y.Offset(0, 1)
If TextBox2.Text = Y Then TextBox5.Text = Y.Offset(0, 2)
 
 
Next Y
End With
 
gTextBox = 1
 
End Sub
 
Private Sub TextBox10_Change()
 
End Sub
Private Sub TextBox11_Change()
 
End Sub
Private Sub TextBox2_Change()
End Sub
Private Sub TextBox3_Change()
End Sub
Private Sub TextBox4_Change()
End Sub
Private Sub TextBox5_Change()
End Sub
Private Sub TextBox6_Change()
Dim entry As Integer
If TextBox6.Text <> vbNullString Then
entry = Int(TextBox6.value)
End If
If TextBox6.value <= 0 Then
TextBox6.ForeColor = &H80000008
TextBox6.BackColor = &HFF&
Else
TextBox6.ForeColor = &H80000008
TextBox6.BackColor = &HFF00&
End If
End Sub
Private Sub TextBox7_Change()
End Sub
Private Sub TextBox8_Change()
End Sub
Private Sub TextBox9_Change()
Dim entry As Integer
If TextBox9.Text <> vbNullString Then
entry = Int(TextBox9.Text)
End If
Option Explicit
gTextBox = 9
End Sub
Private Sub UserForm_Click()
End Sub
 
Private Sub TextBox9_Enter()
gTextBox = 9
End Sub
Private Sub TextBox10_Enter()
gTextBox = 10
End Sub
Sub Addvalue(ByVal bButtonNumber As Byte)
Select Case gTextBox
Case 1
TextBox1.value = Val(TextBox1.value) + bButtonNumber
Case 2
TextBox9.value = Val(TextBox9.value) + bButtonNumber
Case 3
TextBox10.value = Val(TextBox10.value) + bButtonNumber
End Select
End Sub
 
Upvote 0
Hi Paul,

There are few things to consider:

1. You have defined gTextBox for TextBox1 in the Change event. Clicking (selecting) a textbox without editing will not fire the Change event. Therefore, you will get nothing if you just clicked on TextBox1. If you want to consider clicking the textbox, use the Enter event.

2. Addvalue shall be as follows
Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]Sub Addvalue(ByVal bButtonNumber As Byte)
    Select Case gTextBox
        Case 1
            TextBox1.Value = Val(TextBox1.Value) + bButtonNumber
        Case 9
            TextBox9.Value = Val(TextBox9.Value) + bButtonNumber
        Case 10
            TextBox10.Value = Val(TextBox10.Value) + bButtonNumber
    End Select
End Sub[/COLOR][/SIZE][/FONT]
 
Upvote 0
Cheers Mohammad,
Sorry for not replying sooner but I was away for a while. I spent some time on implementing your solution and have it working now. Thanks for your help.

I probably wasn't clear on what I was looking for but the form now adds the button values together rather than as a string on consecutive presses e.g. entering the cursor in textbox1 and pressing button 1 followed by button 3 displays 4 rather than '13'. So it seems to be adding their value rather than treating them as a string.

Would you be able to suggest any quick change to make it work as I need? Apologies again for the delay in thanking you. I have learnt quite a bit from following your suggestions.

Regards,

Paul
 
Upvote 0
Hi Paul,

Sorry, I thought you want to add. Change AddValue as shown below.

Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]Sub AddValue(ByVal bButtonNumber As Byte)
    Select Case gTextBox
        Case 1
            TextBox1.Value = TextBox1.Value & bButtonNumber
        Case 2
            TextBox2.Value = TextBox2.Value & bButtonNumber
        Case 3
            TextBox3.Value = TextBox3.Value & bButtonNumber
    End Select
End Sub[/COLOR][/SIZE][/FONT]
 
Upvote 0
Hi Mohammad,
Go raibh maith agat!

Many thanks for your help. Each time I use the board I learn quite a lot.

Best Regards,

Paul
 
Upvote 0

Forum statistics

Threads
1,215,203
Messages
6,123,627
Members
449,109
Latest member
Sebas8956

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