格式
if 条件 then 语句组1 [else 语句2] 例如: if x>0 then print x Else print –x; (如果饿了,吃饭;[不饿,不吃,反正就是不吃饭]。
(2)块if语句(注意格式) If 条件then
[语句组1] [Else [语句组2] End if
先判断条件是否成立,成立,执行语句组1,不成立,执行语句组2.</span> If x>0 then Print x Else Print-x End if
(个人认为,块if比行if区别在,块if较复杂,要分段,有end if 。在块if中又可以嵌套,可以添加行if 或者再添加块if,块if 是一个框架。)
<span style="font-size:24px;">If 条件1 then [语句组1] [Elseif 条件2 then [语句组2] [Elseif 条件3] then [语句组3] …… [else <[语句组n]] End if </span>
例如
<span style="font-family: FangSong_GB2312; font-size: 18px; background-color: rgb(255, 255, 255);">等同于</span>
<span style="font-size:24px;">Sub form_click() <span style="white-space:pre"> </span>Dim I as intenger <span style="white-space:pre"> </span>If i=1 then <span style="white-space:pre"> </span>Form1.show <span style="white-space:pre"> </span>Elseif i=2 then <span style="white-space:pre"> </span>Form2.show <span style="white-space:pre"> </span>Elseif i=3 then <span style="white-space:pre"> </span>Unload me <span style="white-space:pre"> </span>End if (只有一个if 对应着end if ) End sub </span>
<span style="font-size:24px;">Sub form_click() If i=1 then <span style="white-space:pre"> </span>Form1.show <span style="white-space:pre"> </span>Else <span style="white-space:pre"> </span>If i=2 then <span style="white-space:pre"> </span>Form2.show <span style="white-space:pre"> </span>Else <span style="white-space:pre"> </span>If i=3 then <span style="white-space:pre"> </span>Unload me <span style="white-space:pre"> </span>End if <span style="white-space:pre"> </span>End if <span style="white-space:pre"> </span>End if(很多嵌套,比elseif复杂了很多) End sub </span>
<span style="font-size:24px;">select case [case 表达式列表1 [语句块1]] [case 表达式列表2 [语句块2]] …… Case else [语句块] End select </span>
Select case 语句执行时,首先计算测试表达式的值,然后用该值依次测试各个表达式列表,如果在某个列表中找到匹配的表达式,则执行该case语句之后的语句块,然后执行end select语句之后的语句。如果所有的表达式都不能匹配,执行case else后的句子。
<span style="font-size: 18px; font-family: FangSong_GB2312; background-color: rgb(255, 255, 255);">(1)</span><span style="font-size: 18px; font-family: FangSong_GB2312; background-color: rgb(255, 255, 255);"></span><span style="font-size: 18px; font-family: FangSong_GB2312; background-color: rgb(255, 255, 255);">单独的常量。 Case1,3,5,”a”,”b”</span>(2) 用to 制定范围。例如,case 2 to 10,”abc”to “xyz”
<span style="font-size:24px;">Select Case SSTab.Tab Case 0 '购卡 txtsql = "select * from student_info where UserID='" & Trim(cbuserid.Text) & "'and Ischeck='未结账'" Set mrc = ExecuteSQL(txtsql, msgtext) With salecardms .Rows = 1 .CellAlignment = 4 Do While Not mrc.EOF .Rows = .Rows + 1 .TextMatrix(.Rows - 1, 0) = mrc.Fields(1).Value mrc.MoveNext Loop End With </span>
<span style="font-size:24px;">Case 1 '充值 txtsql = "select * from Recharge_info where UserID='" & Trim(cbuserid.Text) & "'and status='未结账'" Set mrcc = ExecuteSQL(txtsql, msgtext) With rechargems .Rows = 1 .CellAlignment = 4 Do While Not mrcc.EOF .Rows = .Rows + 1 .TextMatrix(.Rows - 1, 0) = mrcc.Fields(1).Value mrcc.MoveNext Loop End With</span>
<span style="font-size:24px;">Case 2 '退卡 txtsql = "select * from CancelCard_info where UserID='" & Trim(cbuserid.Text) & "'and status='未结账'" Set mrccc = ExecuteSQL(txtsql, msgtext) With cancelcardms .Rows = 1 .CellAlignment = 4 Do While Not mrccc.EOF .Rows = .Rows + 1 .TextMatrix(.Rows - 1, 0) = mrccc.Fields(0).Value mrccc.MoveNext Loop End With</span> <span style="font-size:24px;">Case 3 '临时用户 等等 end select </span>