0% found this document useful (0 votes)
9 views

code 3

Uploaded by

Fadrul96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

code 3

Uploaded by

Fadrul96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Dim previousComboSelection As String

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


' Allow only numbers (0-9), one decimal point, and backspace
Select Case KeyAscii
Case 8 ' Backspace
' Allow
Case 46 ' Decimal point
If InStr(1, TextBox1.Text, ".") > 0 Then
KeyAscii = 0 ' Disallow more than one decimal point
End If
Case 48 To 57 ' Numbers 0-9
' Allow
Case Else
KeyAscii = 0 ' Disallow all other characters
MsgBox "Please enter only numbers or a decimal point. Alphabets and
special characters are not allowed.", vbExclamation, "Invalid Input"
End Select
End Sub

Private Sub TextBox1_Change()


Dim decimalPlaces As Integer
Dim textValue As String
Dim tolerance As String
Dim diameterSymbol As String
Dim currentComboData As String

' Get the value from TextBox1


textValue = TextBox1.Text

' Count the number of decimal places


If InStr(textValue, ".") > 0 Then
decimalPlaces = Len(Mid(textValue, InStr(textValue, ".") + 1))
Else
decimalPlaces = 0 ' No decimal places
End If

' Set tolerance based on decimal places


Select Case decimalPlaces
Case 0
tolerance = "± 0.5"
Case 1
tolerance = "± 0.25"
Case 2
tolerance = "± 0.1"
Case 3
tolerance = "± 0.05"
Case Else
tolerance = "" ' No tolerance if more than 3 decimal places
End Select

' Display the tolerance in TextBox3


TextBox3.Text = tolerance

' Calculate sum for TextBox4 (addition)


If tolerance <> "" Then
' Extract the numerical part from TextBox3 (removing "± " prefix)
Dim toleranceValue As Double
toleranceValue = Val(Mid(tolerance, 3))
' Perform the calculation (TextBox1 + TextBox3 value)
TextBox4.Text = TextBox1.value + toleranceValue
Else
' If TextBox3 is empty, clear TextBox4
TextBox4.Text = ""
End If

' Calculate difference for TextBox5 (subtraction)


If tolerance <> "" Then
' Perform the calculation (TextBox1 - TextBox3 value)
TextBox5.Text = TextBox1.value - toleranceValue
Else
' If TextBox3 is empty, clear TextBox5
TextBox5.Text = ""
End If

' Retain "Ø " if it exists at the start of TextBox2


diameterSymbol = "Ø"
If Left(TextBox2.Text, Len(diameterSymbol) + 1) = diameterSymbol & " " Then
TextBox2.Text = diameterSymbol & " " & TextBox1.Text
Else
' Retain ComboBox1 data (if any) while updating TextBox2 with TextBox1 data
If previousComboSelection <> "" Then
currentComboData = " " & previousComboSelection
Else
currentComboData = ""
End If
TextBox2.Text = TextBox1.Text & currentComboData
End If
End Sub

Private Sub ComboBox1_Change()


Dim currentSelection As String
Dim textBoxContent As String

currentSelection = ComboBox1.value

' Remove the previous ComboBox1 selection from TextBox2 (if any)
If previousComboSelection <> "" Then
textBoxContent = Replace(TextBox2.Text, " " & previousComboSelection, "")
Else
textBoxContent = TextBox2.Text
End If

' Append the new ComboBox1 selection at the end with a space
TextBox2.Text = textBoxContent & " " & currentSelection

' Update the previous selection


previousComboSelection = currentSelection
End Sub

Private Sub CommandButton1_Click()


Dim diameterSymbol As String
diameterSymbol = "Ø"

' Check if TextBox2 already starts with "Ø" followed by a space


If Left(TextBox2.Text, Len(diameterSymbol) + 1) = diameterSymbol & " " Then
MsgBox "Data already added", vbExclamation, "Duplicate Symbol"
Else
' Add "Ø" with a space at the beginning of the current data in TextBox2
TextBox2.Text = diameterSymbol & " " & TextBox2.Text
End If
End Sub

Private Sub ToggleButton1_Click()


' Toggle visibility of TextBox6, TextBox7, Label10, and Label11 based on
ToggleButton1 state
Dim groupVisible As Boolean
groupVisible = ToggleButton1.value ' True when "On", False when "Off"

' Clear TextBox6 and TextBox7 when Group1 is turned on


If groupVisible Then
TextBox6.Text = "" ' Clear TextBox6
TextBox7.Text = "" ' Clear TextBox7
TextBox6.Visible = True ' Show TextBox6
TextBox7.Visible = True ' Show TextBox7
Label10.Visible = True ' Show Label10
Label11.Visible = True ' Show Label11
TextBox6.Enabled = True ' Enable TextBox6
TextBox7.Enabled = True ' Enable TextBox7
Else
' If ToggleButton1 is turned off, hide and disable the textboxes and labels
TextBox6.Visible = False ' Hide TextBox6
TextBox7.Visible = False ' Hide TextBox7
Label10.Visible = False ' Hide Label10
Label11.Visible = False ' Hide Label11
TextBox6.Enabled = False ' Disable TextBox6
TextBox7.Enabled = False ' Disable TextBox7
End If
End Sub

Private Sub UserForm_Initialize()


' Set ToggleButton1 to "Off" mode when the form starts
ToggleButton1.value = False

' Initialize visibility and functionality based on the initial state of


ToggleButton1
TextBox6.Visible = False
TextBox7.Visible = False
Label10.Visible = False
Label11.Visible = False
TextBox6.Enabled = False
TextBox7.Enabled = False

' Populate ComboBox1 in order: f1 to f12, F1 to F12, h1 to h12, H1 to H12


Dim i As Integer
For i = 1 To 12
ComboBox1.AddItem "f" & i
Next i
For i = 1 To 12
ComboBox1.AddItem "F" & i
Next i
For i = 1 To 12
ComboBox1.AddItem "h" & i
Next i
For i = 1 To 12
ComboBox1.AddItem "H" & i
Next i

' Lock TextBox2 to prevent user input


TextBox2.Locked = True

' Initialize the previous selection to an empty string


previousComboSelection = ""
End Sub

Private Sub TextBox6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


' Allow only numbers (0-9), one decimal point, backspace, and + or - at the
start
If KeyAscii = 43 Or KeyAscii = 45 Then ' + or - symbol
If Len(TextBox6.Text) = 0 Then
' Allow if the symbol is at the start
Else
KeyAscii = 0 ' Prevent symbol after number
MsgBox "Please input + or - symbol at the beginning.", vbExclamation,
"Invalid Input"
End If
ElseIf KeyAscii = 8 Then
' Allow backspace
ElseIf (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Then
' Allow numbers and decimal point only if the symbol is already input
If Len(TextBox6.Text) = 0 And KeyAscii <> 43 And KeyAscii <> 45 Then
KeyAscii = 0 ' Prevent input if there's no + or - at the start
MsgBox "Please input + or - symbol at the beginning.", vbExclamation,
"Invalid Input"
End If
Else
KeyAscii = 0 ' Disallow all other characters
MsgBox "Please input numbers or decimal point, and start with + or -.",
vbExclamation, "Invalid Input"
End If
End Sub

Private Sub TextBox7_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


' Allow only numbers (0-9), one decimal point, backspace, and + or - at the
start
If KeyAscii = 43 Or KeyAscii = 45 Then ' + or - symbol
If Len(TextBox7.Text) = 0 Then
' Allow if the symbol is at the start
Else
KeyAscii = 0 ' Prevent symbol after number
MsgBox "Please input + or - symbol at the beginning.", vbExclamation,
"Invalid Input"
End If
ElseIf KeyAscii = 8 Then
' Allow backspace
ElseIf (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Then
' Allow numbers and decimal point only if the symbol is already input
If Len(TextBox7.Text) = 0 And KeyAscii <> 43 And KeyAscii <> 45 Then
KeyAscii = 0 ' Prevent input if there's no + or - at the start
MsgBox "Please input + or - symbol at the beginning.", vbExclamation,
"Invalid Input"
End If
Else
KeyAscii = 0 ' Disallow all other characters
MsgBox "Please input numbers or decimal point, and start with + or -.",
vbExclamation, "Invalid Input"
End If
End Sub

You might also like