大家来帮忙看下这个程序哪里出错了

来源:百度知道 编辑:UC知道 时间:2024/07/02 06:38:27
if month(text1.text) =1 or month(text1.text)=3 or month(text1.text)=5 or month(text1.text)=7 or month(text1.text)=8 or month(text1.text)=10 or month(text1.text)=12 then text2.text=31
elseif month(text1.text)=4 or month(text1.text)=6 or month(text1.text)=9 or month(text1.text)=11 then text2.text=30
elseif month(text1.text)=2 then text2.text=28
else then text2.text=null
end if

错误原因:这是vb 的固定格式问题,如果要用else 就不能把 then 后面的语句和它写成一行,写在一行里的只适用于非选择语句的情况,且后面不需要加"end if".

现修改成如下就可以:
Private Sub Command1_Click()
If Month(Text1.Text) = 1 Or Month(Text1.Text) = 3 Or Month(Text1.Text) = 5 Or Month(Text1.Text) = 7 Or Month(Text1.Text) = 8 Or Month(Text1.Text) = 10 Or Month(Text1.Text) = 12 Then
Text2.Text = 31
ElseIf Month(Text1.Text) = 4 Or Month(Text1.Text) = 6 Or Month(Text1.Text) = 9 Or Month(Text1.Text) = 11 Then
Text2.Text = 30
ElseIf Month(Text1.Text) = 2 Then
Text2.Text = 28
Else
Text2.Text = Null
End If
End Sub

'------------------------------
'------------------------------
'另外:
'建议用selece case,方便好多,更简洁啊。
Select Case Month(Text1.Text)
Case 1, 3, 5, 7, 8, 10, 12
Text2.Text = 31
Case 4, 6, 9, 11
Text2.Text = 30
Case 2
Text2.Text = 28
Case Else
Text2.Text = Null
End Select