Hello I would like
1. txtFrm5 to trigger the KeyPress Event with numerical values only
2. txtFrm7 to Automatically calculate discount of 10% in respective textbox Change event
i.e txtFrm7.value = txtFrm5.Value - (txtFrm5.Value*10/100)
3.TxtFrm7 should KeyAscii = 0 so no one enters the data
Here what happens all the textboxes with KeyPress event allows only numerical values
so How can i control the with class module the above mentioned respective textboxes with keypress and change event ?
https://www.dropbox.com/s/uqr00472qamkt3b/SpecificRequirementsTextBoxesClassEvent.xlsm?dl=0
In Class Module Class2AllTextboxes
in Module1
inUserForm1
In Userform2
Thanks NimishK
1. txtFrm5 to trigger the KeyPress Event with numerical values only
2. txtFrm7 to Automatically calculate discount of 10% in respective textbox Change event
i.e txtFrm7.value = txtFrm5.Value - (txtFrm5.Value*10/100)
3.TxtFrm7 should KeyAscii = 0 so no one enters the data
Here what happens all the textboxes with KeyPress event allows only numerical values
so How can i control the with class module the above mentioned respective textboxes with keypress and change event ?
https://www.dropbox.com/s/uqr00472qamkt3b/SpecificRequirementsTextBoxesClassEvent.xlsm?dl=0
In Class Module Class2AllTextboxes
Code:
Option Explicit
Public WithEvents AllTextboxesEvent As MSForms.TextBox
Private Sub AllTextboxesEvent_Change()
Dim i As Integer
Dim Ws As Worksheet
Set Ws = Worksheets("Sheet2")
Ws.Activate
If EditMode = True Then Exit Sub
For i = 1 To 7
Ws.Cells(curRow, i).Value = UserForm2.Controls("txtFrm" & i).Value
Next i
End Sub
Private Sub AllTextboxesEvent_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57
Exit Sub
Case Else
KeyAscii = 0
MsgBox "Only numbers allowed.", 48, "Numbers only please."
Exit Sub
End Select
End Sub
in Module1
Code:
Option Explicit
Public row As Long
Public Ws As Worksheet
Public Const StartRow As Long = 2
Public curRow As Long, curRec As Integer
Public newUf1txtBxFrm As MSForms.TextBox
Public lablFrm2 As Control
Public EditMode As Boolean
inUserForm1
Code:
Option Explicit
Private Sub cmdUF2_Click()
Dim Ws As Worksheet
EditMode = True
Load UserForm2
UserForm2.Show vbModeless
UserForm2.Caption = "Trial"
UserForm2.Top = 210
UserForm2.Left = 200
Set Ws = Worksheets("Sheet2")
Ws.Activate
GetRecord curRow
EditMode = False
End Sub
Private Sub UserForm_Activate()
UserForm1.Left = 245
End Sub
Private Sub UserForm_Initialize()
Dim Ws As Worksheet
Set Ws = Worksheets("Sheet2")
Ws.Activate
curRec = 1
curRow = 2
Rows(curRow).Select
End Sub
Public Sub GetRecord(ByVal row As Long)
Dim Ws As Worksheet
Dim i As Integer
Set Ws = Worksheets("Sheet2")
Ws.Activate
If row < StartRow Then row = StartRow
For i = 1 To 6 '2
UserForm2.Controls("txtFrm" & i).Value = Ws.Cells(row, i).Value
Next i
Rows(row).Select
curRec = curRow - 1
End Sub
In Userform2
Code:
Option Explicit
Public AllTextboxes As New Collection
Public Uf1txtBxFrm As New Class2AllTextboxes
Private Sub UserForm_Initialize()
Call designForm2
End Sub
Public Sub designForm2()
Dim allTxtBxes As Class2AllTextboxes
Dim Ws As Worksheet
Set Ws = Worksheets("Sheet2")
Ws.Activate
Dim i As Integer
Dim x As Integer
Dim y As Integer
y = 10
x = 10
Set AllTextboxes = New Collection
For i = 1 To 7
Set allTxtBxes = New Class2AllTextboxes
Set newUf1txtBxFrm = UserForm2.Controls.Add("Forms.TextBox.1")
Set allTxtBxes.AllTextboxesEvent = newUf1txtBxFrm
AllTextboxes.Add Item:=allTxtBxes
Set lablFrm2 = UserForm2.Controls.Add("Forms.Label.1")
With lablFrm2
.Name = "lblfrm2" '& nNames(i)
.Height = 30
.Width = 15 * 5
.Left = x
.Top = y
.BackStyle = 0
.Caption = Ws.Cells(1, i).Value & vbCrLf & "txtFrm" & i
End With
With newUf1txtBxFrm 'txtBxFrm2
.Name = "txtFrm" & (i) '& nNames(i)
.Height = 18
.Width = 116
.Left = x
.Top = y + 30
.Font.Name = "Calibri"
.Font.Size = "11"
End With
x = x + 142
If i = 2 Then
x = 10 '15
y = lablFrm2.Height + newUf1txtBxFrm.Height + y
End If
If i = 4 Then
x = 10 '15
y = lablFrm2.Height + newUf1txtBxFrm.Height + y
End If
If i = 6 Then
x = 10 '15
y = lablFrm2.Height + newUf1txtBxFrm.Height + y
End If
Next i
End Sub
Last edited: