得到xinfresh的赞同很荣幸,荣幸之余顺手贴上我手头正在写的一段代码,虽然不一定严格按照VB规范书写,但是起码要整齐,要有自己的风格(或者说习惯),这样就方便了以后的代码修改、代码移植或者代码共享,希望能对楼主有用
Private Function IsInputOk(ByVal Index As Integer) As Boolean
Const LowLimit = 300
Const UpLimit = 1100
Dim StartW(2) As Double
Dim EndW(2) As Double
Dim Interval As Double
Dim Speed As Integer
Dim MeasureTimes As Integer
Dim DelayTime As Double
Dim RepeatTimes As Double
Dim IntervalTime As Double
Dim WlPoint As Double
On Error GoTo Err_Hand
If ChkGrating1(Index).Value <> 1 And ChkGrating2(Index).Value <> 1 And ChkGrating3(Index).Value <> 1 Then
Call MsgBox("请选择使用的光栅号!", vbInformation, "警告!")
ChkGrating1(Index).SetFocus
GoTo Err_Hand
End If
If ChkGrating1(Index).Value = vbChecked Then
If Not IsNumeric(TxtStart1(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtStart1(Index).SetFocus
GoTo Err_Hand
End If
If Not IsNumeric(TxtEnd1(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtEnd1(Index).SetFocus
GoTo Err_Hand
End If
StartW(0) = Val(TxtStart1(Index).Text)
EndW(0) = Val(TxtEnd1(Index).Text)
If StartW(0) >= EndW(0) Then
Call MsgBox("起始波长不能大于或者等于终止波长!", vbInformation, "警告!")
TxtStart1(Index).SetFocus
GoTo Err_Hand
End If
If StartW(0) < LowLimit Then
Call MsgBox("起始波长不能小于" & LowLimit, vbInformation, "警告!")
TxtStart1(Index).SetFocus
GoTo Err_Hand
End If
If EndW(0) > UpLimit Then
Call MsgBox("终止波长不能大于" & UpLimit, vbInformation, "警告!")
TxtEnd1(Index).SetFocus
GoTo Err_Hand
End If
End If
If ChkGrating2(Index).Value = vbChecked Then
If Not IsNumeric(TxtStart2(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtStart2(Index).SetFocus
GoTo Err_Hand
End If
If Not IsNumeric(TxtEnd2(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtEnd2(Index).SetFocus
GoTo Err_Hand
End If
StartW(1) = Val(TxtStart2(Index).Text)
EndW(1) = Val(TxtEnd2(Index).Text)
If StartW(1) >= EndW(1) Then
Call MsgBox("起始波长不能大于或者等于终止波长!", vbInformation, "警告!")
TxtStart2(Index).SetFocus
GoTo Err_Hand
End If
If StartW(1) < LowLimit Then
Call MsgBox("起始波长不能小于" & LowLimit, vbInformation, "警告!")
TxtStart2(Index).SetFocus
GoTo Err_Hand
End If
If EndW(1) > UpLimit Then
Call MsgBox("终止波长不能大于" & UpLimit, vbInformation, "警告!")
TxtEnd2(Index).SetFocus
GoTo Err_Hand
End If
End If
If ChkGrating3(Index).Value = vbChecked Then
If Not IsNumeric(TxtStart3(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtStart3(Index).SetFocus
GoTo Err_Hand
End If
If Not IsNumeric(TxtEnd3(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtEnd3(Index).SetFocus
GoTo Err_Hand
End If
StartW(2) = Val(TxtStart3(Index).Text)
EndW(2) = Val(TxtEnd3(Index).Text)
If StartW(2) >= EndW(2) Then
Call MsgBox("起始波长不能大于或者等于终止波长!", vbInformation, "警告!")
TxtStart3(Index).SetFocus
GoTo Err_Hand
End If
If StartW(2) < LowLimit Then
Call MsgBox("起始波长不能小于" & LowLimit, vbInformation, "警告!")
TxtStart3(Index).SetFocus
GoTo Err_Hand
End If
If EndW(2) > UpLimit Then
Call MsgBox("终止波长不能大于" & UpLimit, vbInformation, "警告!")
TxtEnd3(Index).SetFocus
GoTo Err_Hand
End If
End If
If IsNumeric(CboInterval(Index).Text) Then '扫描间隔0.01到800nm
Interval = Val(CboInterval(Index).Text)
If Interval < 0.01 Or Interval > 800 Then
Call MsgBox("扫描间隔不能小于0.01nm或者大于800nm!", vbInformation, "警告!")
CboInterval(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboInterval(Index).SetFocus
GoTo Err_Hand
End If
If IsNumeric(CboSpeed(Index).Text) Then '扫描速度0到255
Speed = Val(CboSpeed(Index).Text)
If Interval < 0 Or Interval > 255 Then
Call MsgBox("扫描速度不能小于0或者大于255!", vbInformation, "警告!")
CboSpeed(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboSpeed(Index).SetFocus
GoTo Err_Hand
End If
If IsNumeric(CboMeasureTimes(Index).Text) Then '测量次数1到100次
MeasureTimes = Val(CboMeasureTimes(Index).Text)
If MeasureTimes < 1 Or MeasureTimes > 100 Then
Call MsgBox("测量次数不能小于1或者大于100!", vbInformation, "警告!")
CboMeasureTimes(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboMeasureTimes(Index).SetFocus
GoTo Err_Hand
End If
If IsNumeric(CboDelayTime(Index).Text) Then '延迟时间0.001到100秒
DelayTime = Val(CboDelayTime(Index).Text)
If DelayTime < 0.001 Or DelayTime > 100 Then
Call MsgBox("延迟时间不能小于0.001或者大于100!", vbInformation, "警告!")
CboDelayTime(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboDelayTime(Index).SetFocus
GoTo Err_Hand
End If
If IsNumeric(CboRepeatTime(Index).Text) Then '重复次数1到100次
RepeatTimes = Val(CboRepeatTime(Index).Text)
If RepeatTimes < 1 Or RepeatTimes > 100 Then
Call MsgBox("重复次数不能小于1或者大于100!", vbInformation, "警告!")
CboRepeatTime(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboRepeatTime(Index).SetFocus
GoTo Err_Hand
End If
If IsNumeric(CboIntervalTime(Index).Text) Then '间隔时间0.001到100秒
IntervalTime = Val(CboIntervalTime(Index).Text)
If IntervalTime < 0.001 Or IntervalTime > 100 Then
Call MsgBox("间隔时间不能小于0.001或者大于100!", vbInformation, "警告!")
CboIntervalTime(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboIntervalTime(Index).SetFocus
GoTo Err_Hand
End If
If Index < 2 Then
If IsNumeric(TxtWlPoint(Index).Text) Then
WlPoint = Val(TxtWlPoint(Index).Text)
If WlPoint < LowLimit Or WlPoint > UpLimit Then
If Index = 0 Then
Call MsgBox("激发波长不能小于" & LowLimit & "或者大于" & UpLimit, vbInformation, "警告!")
Else
Call MsgBox("发射波长不能小于" & LowLimit & "或者大于" & UpLimit, vbInformation, "警告!")
End If
TxtWlPoint(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtWlPoint(Index).SetFocus
GoTo Err_Hand
End If
End If
IsInputOk = True
Exit Function
Err_Hand:
IsInputOk = False
End Function