我要在text1中输入年份,在text2中输入月份,即不能出现溢出,而且只能输入日期,该怎么办,

解决方案 »

  1.   


    '首先保证只能输入数字:
    Private Sub text1_KeyPress(KeyAscii As Integer) 'text1:年份输入文本框
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        KeyAscii = 0
    End If
    End SubPrivate Sub text2_KeyPress(KeyAscii As Integer) 'text2:月份输入文本框
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        KeyAscii = 0
    End If
    End Sub
    '然后在lostfocus事件中判断:
    Private Sub Text1_LostFocus()'判断年份
    If Val(Text1) < 0 Or Val(Text1) > 9999 Then
        MsgBox ("输入年份非法!")
    End If
    Text1.SetFocus
    End SubPrivate Sub Text2_LostFocus()'判断月份
    If Val(Text2) < 0 Or Val(Text2) > 12 Then
        MsgBox ("输入月份非法!")
    End If
    Text2.SetFocus
    End Sub
      

  2.   

    Val(Text2) < 1
      

  3.   


    '重发一次'首先保证只能输入数字:
    Private Sub text1_KeyPress(KeyAscii As Integer) 'text1:年份输入文本框
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        KeyAscii = 0
    End If
    End SubPrivate Sub text2_KeyPress(KeyAscii As Integer) 'text2:月份输入文本框
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        KeyAscii = 0
    End If
    End Sub
    '然后在lostfocus事件中判断:
    Private Sub Text1_LostFocus()'判断年份
    If Val(Text1) < 0 Or Val(Text1) > 9999 Then
        MsgBox ("输入年份非法!")
    End If
    Text1.SetFocus
    End SubPrivate Sub Text2_LostFocus()'判断月份
    If Val(Text2) < 1 Or Val(Text2) > 12 Then 
        MsgBox ("输入月份非法!")
    End If
    Text2.SetFocus
    End Sub
      

  4.   

    MonthView 控件的Year 属性可以设置为从 1601 到 9999 的任意整数。
      

  5.   

    实在不好意思,没做测试'首先保证只能输入数字:
    Private Sub text1_KeyPress(KeyAscii As Integer) 'text1:年份输入文本框
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        KeyAscii = 0
    End If
    End SubPrivate Sub text2_KeyPress(KeyAscii As Integer) 'text2:月份输入文本框
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        KeyAscii = 0
    End If
    End Sub
    '然后在lostfocus事件中判断:
    Private Sub Text1_LostFocus() '判断年份
    If Val(Text1) < 1 Or Val(Text1) > 9999 Then
        MsgBox ("输入年份非法!")
        Text1.SetFocus
    End IfEnd SubPrivate Sub Text2_LostFocus() '判断月份
    If Val(Text2) < 1 Or Val(Text2) > 12 Then
        MsgBox ("输入月份非法!")
        Text2.SetFocus
    End IfEnd Sub
      

  6.   

    问题解决了就结帖吧,把分给 chinayuppie (学无止境)