PRIVATE SUB COMMANDBUTTON1_CLICK() IF TEXTBOX1 = "" OR TEXTBOX3 = "" OR TEXTBOX4 = "" THEN MSGBOX "三项数据必须完整" IF TEXTBOX1.VALUE = "" THEN TEXTBOX1.SETFOCUS IF TEXTBOX3.VALUE = "" THEN TEXTBOX3.SETFOCUS IF TEXTBOX4.VALUE = "" THEN TEXTBOX4.SETFOCUS ELSE END IF A = MSGBOX("结果是:" & TEXTBOX1.VALUE TEXTBOX3.VALUE TEXTBOX4.VALUE, VBOKCANCEL, "计算结果") END SUB 当我明明有一个文本框内为空时,它虽然也弹出了提示窗口,但是它还是继续生成了计算结果的窗口。这是怎么回事呢?我想让它必须在三项都填有数据的情况下才能产生计算结果,应该怎么样修改程序呢?

设置一个中间变量k来记录是否三项都填有数据,如果填了,k=0,如果没有填,k=1,通过判断k的值来决定是否继续生成计算结果的窗口
private sub commandbutton1_click()
dim k as integer
k = 0
if textbox1 = "" or textbox3 = "" or textbox4 = "" then
k = 1
msgbox "三项数据必须完整"
if textbox1.value = "" then textbox1.setfocus
if textbox3.value = "" then textbox3.setfocus
if textbox4.value = "" then textbox4.setfocus
else
end if
if k = 0 then
a = msgbox("结果是:"& textbox1.value + textbox3.value + textbox4.value, vbokcancel, "计算结果")
end if
end sub

相关常见问题