为什么'On Error'总是触发,就好像键为空?

Rom*_*nus 2 ms-access vba

我正在尝试在连接到SQL Server后端的Access应用程序中设置错误处理,以防止Access将空主键传递到服务器.

我正在使用BeforeUpdate和基于VBA中正确处理错误的代码(Excel)

如果用户将S_ID设置为null(或者在创建新记录时将其保留为null),则我的代码应该运行ErrHandler代码.但它始终运行ErrHandler代码.

Private Sub S_ID_BeforeUpdate(Cancel As Integer)

Dim trigger As Integer

On Error GoTo ErrHandler

If Me.S_ID Is Null Then
    trigger = 1 / 0
End If

Exit Sub

ErrHandler:
    MsgBox ("Key was null, fix it")
Resume Resume_spot

Resume_spot:

End Sub
Run Code Online (Sandbox Code Playgroud)

我还没有为错误发生时我想要Access做的代码编写代码,因此之后无用MsgBox且缺乏任何东西Resume_spot.在我达到目标之前,我希望错误处理能够正确触发.

Thu*_*ame 5

你需要使用 IsNull

更改:

If Me.S_ID Is Null Then
Run Code Online (Sandbox Code Playgroud)

至:

If IsNull(Me.S_ID) Then
Run Code Online (Sandbox Code Playgroud)