Code from Form
Option Explicit
Private Sub Command1_Click()
If isInputBad(txtLoanAmt, txtIntRate, txtYears) Then
MsgBox "Bad input"
Else
MsgBox "good input"
End If
End Sub
Private Sub txtIntRate_KeyPress(KeyAscii As Integer)
Call checkKeyStroke(txtIntRate, KeyAscii)
End Sub
Private Sub txtLoanAmt_KeyPress(KeyAscii As Integer)
Call checkKeyStroke(txtLoanAmt, KeyAscii)
End Sub
Code from Module
Option Explicit
Public Sub checkKeyStroke(ByVal textB As TextBox, _
ByRef key As Integer)
'MsgBox Asc(".")
Select Case key
Case Asc("0") To Asc("9")
'do nothing, input ok
Case Asc(".")
If InStr(textB, ".") > 0 Then
key = 0
End If
Case Asc(vbBack)
'do nothing, backspace
Case Else
key = 0
End Select
End Sub
Public Function isInputBad(ByVal t1 As TextBox, _
ByVal t2 As TextBox, _
ByVal t3 As TextBox) As Boolean
Dim inputError As Boolean
inputError = False
Dim errorString As String
errorString = ""
If Val(t1) < 0 Then
inputError = True
errorString = t1.Tag & " must be positive"
End If
If Val(t2) < 0 Then
inputError = True
errorString = errorString & vbCrLf & _
t2.Tag & " must be positive"
End If
If Val(t3) < 0 Then
inputError = True
errorString = errorString & vbCrLf & _
t3.Tag & " must be positive"
End If
If inputError Then
MsgBox errorString
End If
isInputBad = inputError
End Function