Place the 2 Textbox objects in your form with name Textbox1 and Textbox2.
And then paste the code below into your .frm file.
Private Sub Text1_KeyPress(KeyAscii As Integer)
Call KeyPressNumeric(KeyAscii)
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
Call KeyPressNumeric(KeyAscii, True, Text2.Text, True, Text2.SelStart)
End Sub
Private Sub Text1_GotFocus()
Text1.Text = Replace(Replace(Text1.Text, ",", ""), ".", "")
End Sub
Private Sub Text1_LostFocus()
Text1.Text = Format(Text1.Text, "###,###,##0")
End Sub
Private Sub Text2_GotFocus()
Text2.Text = Replace(Replace(Text2.Text, ",", ""), ".", "")
End Sub
Private Sub Text2_LostFocus()
Text2.Text = Format(Text2.Text, "###,###,##0.00")
End Sub
Create new module and place the code below inside your module file (.bas).
'****************************************************************************
' Name : KeyPressNumeric
' Author : Chandra Gunawan
' Date : 22-Jan-2009
' Description : For use with Textbox_KeyPress events
' Allows only numeric entries
' Note : If you set pAllowDecimal = True
' then you must set pTextBox_Text = TextBox.Text
' If you set pAllowMinus = True
' then you must set pTextbox_SelStart = TextBox.SelStart
'*****************************************************************************
Public Sub KeyPressNumeric( _
pKeyAscii As Integer, _
Optional pAllowDecimal As Boolean = False, _
Optional pTextBox_Text As String, _
Optional pAllowMinus As Boolean = False, _
Optional pTextbox_SelStart As Long = 0)
Dim strDecDlm As String
'Note: 8=backspace, 13=enter, 27=escape
strDecDlm = Trim(Format(0, "#.#"))
If Chr(pKeyAscii) = strDecDlm Then
If Not pAllowDecimal Or InStr(1, pTextBox_Text, strDecDlm) > 0 Then pKeyAscii = 0
ElseIf Chr(pKeyAscii) = "-" Then
If Not pAllowMinus Or pTextbox_SelStart <> 0 Then pKeyAscii = 0
ElseIf InStr(1, "0123456789", Chr(pKeyAscii)) < 1 And _
Not (pKeyAscii = 8 Or pKeyAscii = 13 Or pKeyAscii = 27) Then
pKeyAscii = 0
End If
End Sub 'KeyPressNumeric