【问题标题】:MS Access: Why is my code not being reached?MS Access:为什么无法访问我的代码?
【发布时间】:2021-10-06 09:25:36
【问题描述】:

我正在尝试对我的代码进行一些错误处理,并且我希望在用户尝试输入已经存在的记录时显示我的自定义错误消息。 Access 提供了自己的标准错误消息,指示重复记录,但我希望显示我的。问题是我没有到达我的自定义错误消息的代码部分,因此给了我默认消息。

文本框的名称是“DepartmentCode”,从中提取的表的名称是“tDepartment”,列的名称是“DepartmentCode”

我的代码是这个...

Private Sub bAddDepartment_Click()
On Error GoTo bAddDepartment_Click_Err

   Dim OKToSave As Boolean
    OKToSave = True

    If Not SomethingIn(Me.DepartmentCode) Then          ' Null
        Beep
        MsgBox "A department code is required", vbOKOnly, "Missing Information"
        OKToSave = False
   
    Else
        Dim myDepartmentCode As String
        myDepartmentCode = "DepartmentCode = " + Chr(34) + Me.DepartmentCode + Chr(34)
        If DLookup("DepartmentCode", "tDepartment", myDepartmentCode) <> Null Then
            MsgBox "Department already on file", vbOKOnly, "Department already on file."
            OKToSave = False
        End If
    End If
    If OKToSave Then
        ' If we get this far, all data is valid and it's time to save
        Me.Dirty = False
        DoCmd.GoToRecord , "", acNewRec
        
       
    End If
bAddDepartment_Click_Exit:
    Exit Sub

bAddDepartment_Click_Err:
   
    Resume bAddDepartment_Click_Exit

End Sub

未到达的部分是If DLookup("DepartmentCode", "tDepartment", myDepartmentCode) &lt;&gt; Null Then

为什么会这样?

【问题讨论】:

    标签: sql vba ms-access debugging


    【解决方案1】:

    Debugging VBA Code

    If DLookup("DepartmentCode", "tDepartment", myDepartmentCode) <> Null Then
    

    你无法像这样与Null 相比。在即时窗口中试试这个:

    ? ("foo" <> Null)
    Null
    

    使用IsNull()

    If Not IsNull(DLookup("DepartmentCode", "tDepartment", myDepartmentCode)) Then
    

    或者如果也可以使用空字符串,请使用Nz()

    If Nz(DLookup("DepartmentCode", "tDepartment", myDepartmentCode), "") <> "" Then
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2014-04-29
      • 2015-09-08
      • 2022-06-21
      • 1970-01-01
      • 2020-08-27
      相关资源
      最近更新 更多