VB 错误处理(陷阱),跳出本次循环进入下次循环后,不能再次处理错误
发布网友
发布时间:2022-05-05 11:39
我来回答
共1个回答
热心网友
时间:2023-10-28 07:07
用goto的可读性很差,而且,你只在开头 On Error GoTo err_law_info_ceshi,跳一次就不再跳了.
你不如换种方法
on error resume next
err.clear
do.....
if err.number =0 then 没错
else 出错
end if 这样处理好些吧.
-------------------------以下为我帮你修改的代码,手写没有测试的,不知道能不能用.
Private Sub Command8_Click()
Dim i As Integer
For i = 0 To lst_file.ListCount - 1
lst_file.ListIndex = i
DBPath = "DBQ=" & lst_file.Text
strName_xls = lst_file.Text 'EXCEL文件名
strSheetName_xls = "sheet2" 'EXCEL表名
Dim rtnStr As String
rtnStr = ""
Dim cnstr As String
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strName_xls & ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"
If TryConnExcel(cnnstr, strSheetName_xls, rtnStr) Then
'这里是你的成功代码,
Exit For '如果你要退出
Else
'如果不成功你再处理别的吧.
lst_err.AddItem rtnStr & strName_xls
End If
Next
MsgBox "完成law_info"
End Sub
Function TryConnExcel(ByVal cnnstr As String, ByVal strSheetName_xls As String, ByRef strRtn As String) As Boolean
Dim conn_xls As New ADODB.Connection
Dim rs_xls As New ADODB.Recordset
On Error Resume Next
Dim rtn As Boolean
rtn = False
Err.Clear
conn_xls.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"
If Err.Number = 0 Then
rs_xls.Open "select * from [" & strSheetName_xls & "$]", conn_xls, adOpenForwardOnly, adLockOptimistic
low_name = rs_xls(0)
low_number = rs_xls(1)
low_info = rs_xls(2)
rs_xls.Close
If Err.Number = 0 Then
rtn = True
strRtn = ""
End If
End If
If Err.Number <> 0 Then
strRtn = "(" & Err.Number & ")" & Err.Description & " "
End If
conn_xls.Close
Set conn_xls = Nothing
Set crs_xls = Nothing
Err.Clear
TryConnExcel = rtn
End Function追问能说的详细点吗。。。小弟没看懂撒