如何在我的代码中添加错误陷阱以忽略从我的 MS Access 数据库中提取的空值?
How to add error trap on my code that would ignore a null value extracted from my ms Access Database?
我如何创建一个错误陷阱来忽略从 MS Access 数据库中提取的任何空值)?
我希望我的程序忽略返回的任何空值并继续,因为我不希望在我的表单上显示空白数据。
在此代码中,错误突出显示
.SubItems(6) = rs3!Regularization_Date
因为 Regularization_Date 在我的数据库中是空白的。我希望我的程序忽略这个并继续用要显示的所有数据填充我的列表视图。
Private Function SearchData()
Result.Show
Result.ListView1.ListItems.Clear
Sql = "SELECT * FROM All_Employees WHERE ID LIKE '" & (Text1.Text) & "'"
Set rs3 = New ADODB.Recordset
rs3.Open Sql, con3, adOpenDynamic, adLockOptimistic
If Not rs3.EOF Then
Do Until rs3.EOF
Set lst1 = Result.ListView1.ListItems.Add(, , rs3!ID)
With lst1
.SubItems(1) = rs3!Lastname
.SubItems(2) = rs3!FirstName
.SubItems(3) = rs3!Position
.SubItems(4) = rs3!Date_hired
.SubItems(5) = rs3!Employment_Status
*.SubItems(6) = rs3!Regularization_Date*
.SubItems(7) = rs3!Office_email
.SubItems(8) = rs3!Shift_Start
.SubItems(9) = rs3!Shift_End
End With
rs3.MoveNext
Loop
End If
Set rs3 = Nothing
End Function
我只想要一个忽略所有空值的错误陷阱。
您可以使用 IsNull 函数检查字段中的值。
它看起来像这样:
.SubItems(1) = rs3!Lastname
.SubItems(2) = rs3!FirstName
.SubItems(3) = rs3!Position
.SubItems(4) = rs3!Date_hired
.SubItems(5) = rs3!Employment_Status
if not IsNull(rs3!Regularization_Date) then
.SubItems(6) = rs3!Regularization_Date
end if
.SubItems(7) = rs3!Office_email
.SubItems(8) = rs3!Shift_Start
.SubItems(9) = rs3!Shift_End
您可以在此处找到更多信息IsNull Docs on Microsoft
如果你真的想在错误处理程序中使用它,你可以这样做:
Private Function SearchData()
on error goto ErrHandler:
Result.Show
Result.ListView1.ListItems.Clear
Sql = "SELECT * FROM All_Employees WHERE ID LIKE '" & (Text1.Text) & "'"
Set rs3 = New ADODB.Recordset
rs3.Open Sql, con3, adOpenDynamic, adLockOptimistic
If Not rs3.EOF Then
Do Until rs3.EOF
Set lst1 = Result.ListView1.ListItems.Add(, , rs3!ID)
With lst1
.SubItems(1) = rs3!Lastname
.SubItems(2) = rs3!FirstName
.SubItems(3) = rs3!Position
.SubItems(4) = rs3!Date_hired
.SubItems(5) = rs3!Employment_Status
.SubItems(6) = rs3!Regularization_Date
.SubItems(7) = rs3!Office_email
.SubItems(8) = rs3!Shift_Start
.SubItems(9) = rs3!Shift_End
End With
rs3.MoveNext
Loop
End If
Cleanup:
Set rs3 = Nothing
exit function
ErrHandler:
dim intErrNo as integer
dim strErrMsg as string
intErrNo = Err.Number
strErrMsg = Err.Description
if intErrNo = 94 then
'Null Value continue with the next line of the code
resume next
else
MsgBox "Error Number: " & intErrNo & vbCrLF & "Description : " & strErrMsg
GoTo CleanUp
end if
End Function
您修改错误处理以显示您想要的内容,或者只记录错误。
作为建议,您可以将函数更改为子函数,因为您不会返回任何内容。
如果只想忽略错误,可以使用 On Error Resume Next
Private Function SearchData()
Result.Show
Result.ListView1.ListItems.Clear
Sql = "SELECT * FROM All_Employees WHERE ID LIKE '" & (Text1.Text) & "'"
**On Error Resume Next**
Set rs3 = New ADODB.Recordset
rs3.Open Sql, con3, adOpenDynamic, adLockOptimistic
If Not rs3.EOF Then
Do Until rs3.EOF
Set lst1 = Result.ListView1.ListItems.Add(, , rs3!ID)
With lst1
.SubItems(1) = rs3!Lastname
.SubItems(2) = rs3!FirstName
.SubItems(3) = rs3!Position
.SubItems(4) = rs3!Date_hired
.SubItems(5) = rs3!Employment_Status
*.SubItems(6) = rs3!Regularization_Date*
.SubItems(7) = rs3!Office_email
.SubItems(8) = rs3!Shift_Start
.SubItems(9) = rs3!Shift_End
End With
rs3.MoveNext
Loop
End If
Set rs3 = Nothing
End Function
我如何创建一个错误陷阱来忽略从 MS Access 数据库中提取的任何空值)?
我希望我的程序忽略返回的任何空值并继续,因为我不希望在我的表单上显示空白数据。
在此代码中,错误突出显示
.SubItems(6) = rs3!Regularization_Date
因为 Regularization_Date 在我的数据库中是空白的。我希望我的程序忽略这个并继续用要显示的所有数据填充我的列表视图。
Private Function SearchData()
Result.Show
Result.ListView1.ListItems.Clear
Sql = "SELECT * FROM All_Employees WHERE ID LIKE '" & (Text1.Text) & "'"
Set rs3 = New ADODB.Recordset
rs3.Open Sql, con3, adOpenDynamic, adLockOptimistic
If Not rs3.EOF Then
Do Until rs3.EOF
Set lst1 = Result.ListView1.ListItems.Add(, , rs3!ID)
With lst1
.SubItems(1) = rs3!Lastname
.SubItems(2) = rs3!FirstName
.SubItems(3) = rs3!Position
.SubItems(4) = rs3!Date_hired
.SubItems(5) = rs3!Employment_Status
*.SubItems(6) = rs3!Regularization_Date*
.SubItems(7) = rs3!Office_email
.SubItems(8) = rs3!Shift_Start
.SubItems(9) = rs3!Shift_End
End With
rs3.MoveNext
Loop
End If
Set rs3 = Nothing
End Function
我只想要一个忽略所有空值的错误陷阱。
您可以使用 IsNull 函数检查字段中的值。 它看起来像这样:
.SubItems(1) = rs3!Lastname
.SubItems(2) = rs3!FirstName
.SubItems(3) = rs3!Position
.SubItems(4) = rs3!Date_hired
.SubItems(5) = rs3!Employment_Status
if not IsNull(rs3!Regularization_Date) then
.SubItems(6) = rs3!Regularization_Date
end if
.SubItems(7) = rs3!Office_email
.SubItems(8) = rs3!Shift_Start
.SubItems(9) = rs3!Shift_End
您可以在此处找到更多信息IsNull Docs on Microsoft
如果你真的想在错误处理程序中使用它,你可以这样做:
Private Function SearchData()
on error goto ErrHandler:
Result.Show
Result.ListView1.ListItems.Clear
Sql = "SELECT * FROM All_Employees WHERE ID LIKE '" & (Text1.Text) & "'"
Set rs3 = New ADODB.Recordset
rs3.Open Sql, con3, adOpenDynamic, adLockOptimistic
If Not rs3.EOF Then
Do Until rs3.EOF
Set lst1 = Result.ListView1.ListItems.Add(, , rs3!ID)
With lst1
.SubItems(1) = rs3!Lastname
.SubItems(2) = rs3!FirstName
.SubItems(3) = rs3!Position
.SubItems(4) = rs3!Date_hired
.SubItems(5) = rs3!Employment_Status
.SubItems(6) = rs3!Regularization_Date
.SubItems(7) = rs3!Office_email
.SubItems(8) = rs3!Shift_Start
.SubItems(9) = rs3!Shift_End
End With
rs3.MoveNext
Loop
End If
Cleanup:
Set rs3 = Nothing
exit function
ErrHandler:
dim intErrNo as integer
dim strErrMsg as string
intErrNo = Err.Number
strErrMsg = Err.Description
if intErrNo = 94 then
'Null Value continue with the next line of the code
resume next
else
MsgBox "Error Number: " & intErrNo & vbCrLF & "Description : " & strErrMsg
GoTo CleanUp
end if
End Function
您修改错误处理以显示您想要的内容,或者只记录错误。 作为建议,您可以将函数更改为子函数,因为您不会返回任何内容。
如果只想忽略错误,可以使用 On Error Resume Next
Private Function SearchData()
Result.Show
Result.ListView1.ListItems.Clear
Sql = "SELECT * FROM All_Employees WHERE ID LIKE '" & (Text1.Text) & "'"
**On Error Resume Next**
Set rs3 = New ADODB.Recordset
rs3.Open Sql, con3, adOpenDynamic, adLockOptimistic
If Not rs3.EOF Then
Do Until rs3.EOF
Set lst1 = Result.ListView1.ListItems.Add(, , rs3!ID)
With lst1
.SubItems(1) = rs3!Lastname
.SubItems(2) = rs3!FirstName
.SubItems(3) = rs3!Position
.SubItems(4) = rs3!Date_hired
.SubItems(5) = rs3!Employment_Status
*.SubItems(6) = rs3!Regularization_Date*
.SubItems(7) = rs3!Office_email
.SubItems(8) = rs3!Shift_Start
.SubItems(9) = rs3!Shift_End
End With
rs3.MoveNext
Loop
End If
Set rs3 = Nothing
End Function