Private Sub Text2_KeyPress(KeyAscii As Integer) Dim Ancien As String Ancien = Me.Text2 If KeyAscii = 46 And Text2.Tag <> "R" Then KeyAscii = 0: Exit Sub If IsNumeric(Me.Text2 & Chr(KeyAscii)) Then Else KeyAscii = 0 End IfEnd Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii >= 32 And KeyAscii <= 45) Or KeyAscii = 47 Or _ (KeyAscii >= 58 And KeyAscii <= 126) Then KeyAscii = 0 Else If InStr(1, Text1.Text, ".", vbTextCompare) > 0 And KeyAscii = 46 Then KeyAscii = 0 End If End IfEnd Sub
Private Sub Text1_Change() If IsNumeric(Text1.Text) = False Then Beep '不是数字就有声音了 End If End Sub
将下面代码贴入窗体中即可(画个Textbox ) Private Sub Form_Load() Text1.Text = "" End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) If Not (Chr(KeyAscii) > "0" And Chr(KeyAscii) < "9" Or Chr(KeyAscii) = "." Or KeyAscii = vbKeyBack) Then
Dim Ancien As String
Ancien = Me.Text2
If KeyAscii = 46 And Text2.Tag <> "R" Then KeyAscii = 0: Exit Sub
If IsNumeric(Me.Text2 & Chr(KeyAscii)) Then
Else
KeyAscii = 0
End IfEnd Sub
If (KeyAscii >= 32 And KeyAscii <= 45) Or KeyAscii = 47 Or _
(KeyAscii >= 58 And KeyAscii <= 126) Then
KeyAscii = 0
Else
If InStr(1, Text1.Text, ".", vbTextCompare) > 0 And KeyAscii = 46 Then
KeyAscii = 0
End If
End IfEnd Sub
If IsNumeric(Text1.Text) = False Then
Beep '不是数字就有声音了
End If
End Sub
Private Sub Form_Load()
Text1.Text = ""
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
If Not (Chr(KeyAscii) > "0" And Chr(KeyAscii) < "9" Or Chr(KeyAscii) = "." Or KeyAscii = vbKeyBack) Then
Else
KeyAscii = 0
End If
End Sub